【问题标题】:foreach gridview row, commandfield edit enable true or falseforeach gridview 行,commandfield 编辑启用 true 或 false
【发布时间】:2018-04-06 06:25:18
【问题描述】:

这里是我的代码,但编辑列中的所有内容都启用了 false,我想要 false 编辑按钮取决于状态值

foreach (GridViewRow row in GridView1.Rows)
{
    statuse = dt.Rows[0].ItemArray[6].ToString();
    if (statuse != null || statuse != "")
    {
        if (control is LinkButton)
        {
            LinkButton btn = control as LinkButton;
            btn.Enabled = btn.CommandName.Equals("Edit");
            btn.Enabled = false;
        }
    }
}

【问题讨论】:

  • 你在找这个吗? btn.Enabled = statuse == null || statuse == ""
  • 我会为此使用 RowDataBound 事件。
  • 是的,我使用的是行数据绑定。我正在寻找:如果 statuse value = "" then commandfielded enabled false。但是在我尝试之后,所有命令编辑在状态有或没有值的地方都变为假

标签: c# asp.net gridview commandfield


【解决方案1】:

这通常是在 RowDataBound 事件中完成的。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the dataitem back to a row
        DataRowView row = e.Row.DataItem as DataRowView;

        //find the edit button in the row with findcontrol
        LinkButton btn = e.Row.FindControl("EditButtonID") as LinkButton;

        //check the status of the correct data field and set the button properties
        if (row["statuse"].ToString() == "Closed")
        {
            btn.Enabled = false;
        }
        else
        {
            btn.Enabled = true;
        }
    }
}

更新

如果编辑按钮是自动生成的,则必须根据列索引找到链接按钮。

LinkButton btn = e.Row.Cells[i].Controls[0] as LinkButton;

【讨论】:

  • 更新了我的答案。如果编辑按钮在第一列,i 将是0
  • 对象引用未设置为对象的实例。
  • 更新我的答案:LinkBut​​ton cmdField =e.Row.Cells[9].Controls[0] as LinkBut​​ton; cmdField.Visible = false; --> 这里有错误
  • 那么那一栏就没有控件了。请注意,索引是从零开始的。也许发布您的 gridview 布局
【解决方案2】:

//这里是我的 aspx 网格视图

                <asp:TemplateField HeaderText = "No" ItemStyle-Width="50"  ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:Label ID="lblRowNumberA" runat="server" />
    </ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="ProposedBy" HeaderText="Proposer"  ReadOnly="true"
            ItemStyle-Width="10%" />

        <asp:BoundField DataField="Date" HeaderText="Date" dataformatstring="{0:d}"
           ItemStyle-Width="100px" ReadOnly="True" />


        <asp:TemplateField HeaderText="Note" ItemStyle-Width="70%" ItemStyle-HorizontalAlign="Left">
           <ItemTemplate>
           <asp:Label ID="Label1ert" runat="server" Text='<%#Bind("Note") %>'></asp:Label>
           </ItemTemplate>
           <EditItemTemplate>
           <asp:TextBox ID="TextBox1edit" runat="server" TextMode="MultiLine" Text='<%#Bind("Note") %>' ></asp:TextBox>
           </EditItemTemplate>
         </asp:TemplateField>


         <asp:TemplateField HeaderText="Compere to standard(s)" ItemStyle-Width="20%">
           <ItemTemplate>
           <asp:Label ID="Label1sdf" runat="server" Text='<%#Bind("Justify") %>'></asp:Label>
           </ItemTemplate>
         </asp:TemplateField>

         <asp:TemplateField HeaderText="Recommendation" ItemStyle-Width="100px">
           <ItemTemplate>
           <asp:Label ID="Label1sdfqq" runat="server" Text='<%#Bind("Status") %>'></asp:Label>
           </ItemTemplate>
           <EditItemTemplate>
           <asp:DropDownList ID="DDStatus" runat="server">


<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
           </EditItemTemplate>
         </asp:TemplateField>

         <asp:BoundField DataField="HRStatus" HeaderText="Status"  ReadOnly="true"
            ItemStyle-Width="250px" />

     <asp:CommandField  ButtonType="Link" ShowEditButton="true" ItemStyle-Width="100px" />

    </Columns>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-18
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    相关资源
    最近更新 更多