【问题标题】:How to disable "Edit" button in Gridview?如何禁用 Gridview 中的“编辑”按钮?
【发布时间】:2013-08-05 15:49:13
【问题描述】:

我正在使用我的网格视图中的项目模板字段来更新特定列中的值。 ItemTemplate 字段包含“标签”控件,EditItemTemplate 包含“DropDownList”。现在的问题是我需要根据“标签”的值禁用“编辑”按钮......附上编码行。谁能给我一个解决方案。

Home.Aspx:
**********
   <Columns>

                    <asp:BoundField DataField="Date" HeaderText="Date" ReadOnly="true" />
                    <asp:BoundField DataField="Type" HeaderText="Type" ReadOnly="true" />
                    <asp:BoundField DataField="Reason" HeaderText="Reason" ReadOnly="true" />
                    <asp:BoundField DataField="Request By" HeaderText="Request By" ReadOnly="true" />
                    <asp:TemplateField HeaderText="Status" HeaderStyle-HorizontalAlign="center">
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlState" AutoPostBack="false" runat="server">
                                <asp:ListItem Text="Approved" Value="Approved">  </asp:ListItem>
                                <asp:ListItem Text="Declined" Value="Declined">  </asp:ListItem>
                                <asp:ListItem Text="Pending" Value="Pending">  </asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblName" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
                        </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                    </asp:TemplateField>
                    <asp:CommandField ShowEditButton="True" />
                </Columns>

在我的编码中,“lblName”在 ItemTemplate 中具有状态值,而“ddlState”在 EditItemTemplate 中具有状态值。根据“lblName”值,必须启用“编辑”选项...

【问题讨论】:

    标签: asp.net gridview edititemtemplate


    【解决方案1】:

    将您的编辑CommandField 转换为TemplateField

    在新生成的Button中,添加如下标记:

    Enabled='&lt;%# IsEditEnabled(Eval("Status")) %&gt;'

    在您的代码隐藏中,创建一个新方法:

    protected bool IsEditEnabled(string statusValue)
    {
        // Here is where you determine the value
    }
    

    让我知道这对你有什么作用。

    【讨论】:

      【解决方案2】:

      另一种方法是使用 RowDataBound,这样您就可以直接使用 Status 的值。这假设您正在为数据源使用 DataTable 或其他 DataRow 集合。如果您使用不同的数据类型,则需要更新 DataItem 转换。

      <asp:GridView ID="ExampleGridView" runat="server" OnRowDataBound="ExampleGridView_RowDataBound">
          <Columns>
              <asp:TemplateField HeaderText="Status" HeaderStyle-HorizontalAlign="center">
                  <EditItemTemplate>
                      <asp:DropDownList ID="StateDropDownList" AutoPostBack="false" runat="server">
                          <asp:ListItem Text="Approved" Value="Approved" />
                          <asp:ListItem Text="Declined" Value="Declined" />
                          <asp:ListItem Text="Pending" Value="Pending" />
                      </asp:DropDownList>
                  </EditItemTemplate>
                  <ItemTemplate>
                      <asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Status") %>' />
                  </ItemTemplate>
              </asp:TemplateField>
              <asp:CommandField ShowEditButton="True" />
              <asp:TemplateField ShowHeader="False">
                  <ItemTemplate>
                      <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="Edit" Visible="true" />
                  </ItemTemplate>
                  <EditItemTemplate>
                      <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                      <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                  </EditItemTemplate>
              </asp:TemplateField>
          </Columns>
      </asp:GridView>
      

      从后面的代码中,您可以独立处理该行并可以访问大部分正在发生的事情:

      protected void ExampleGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow 
              && (
                  e.Row.RowState == DataControlRowState.Alternate
                  || e.Row.RowState == DataControlRowState.Normal
                  || e.Row.RowState == DataControlRowState.Selected
              ))
          {
              Button EditButton = (Button)e.Row.FindControl("EditButton");
              System.Data.DataRow dataRecord = (System.Data.DataRow)e.Row.DataItem;
              if (EditButton != null && dataRecord != null)
              {
                  if (dataRecord["Status"] == "ValueThatShowsEditButton")
                  {
                      EditButton.Visible = true;
                  }
              }
          }
      }
      

      【讨论】:

      • 谢谢@Chris Porter,它有效。但我不想让 ddlstate 可见为假,我需要将命令字段中的编辑按钮设为禁用....
      • @ognale88,抱歉下拉错误。我更新了我的示例以显示编辑按钮而不是下拉菜单。
      猜你喜欢
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      相关资源
      最近更新 更多