foreach gridview row,commandfield edit启用true或false

时间:2017-10-25 08:39:13

标签: c# asp.net gridview commandfield

这里是我的代码,但是所有编辑列都启用了false,我希望false editbutton依赖于statuse值

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;
        }
    }
}

2 个答案:

答案 0 :(得分: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;
        }
    }
}

更新

如果自动生成编辑按钮,则必须根据列索引找到LinkBut​​ton。

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

答案 1 :(得分:0)

//这里是我的aspx gridview
                                  

                <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>