C# 对于每个gridview行,commandfield edit启用true或false
这里是我的代码,但全部在编辑列中启用了false,我希望false编辑按钮取决于状态值C# 对于每个gridview行,commandfield edit启用true或false,c#,asp.net,gridview,commandfield,C#,Asp.net,Gridview,Commandfield,这里是我的代码,但全部在编辑列中启用了false,我希望false编辑按钮取决于状态值 foreach (GridViewRow row in GridView1.Rows) { statuse = dt.Rows[0].ItemArray[6].ToString(); if (statuse != null || statuse != "") { if (control is LinkButton) { LinkB
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;
}
}
}
<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>
在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;
}
}
}
<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>
更新
<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>
如果编辑按钮是自动生成的,则必须根据列索引查找链接按钮
LinkButton btn = e.Row.Cells[i].Controls[0] as LinkButton;
<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>
//这里是我的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>
对
不
您在找这个吗btn.Enabled=statuse==null | | statuse==“”
我将为此使用RowDataBound事件。是的,我将使用RowDataBound。我在寻找:如果status value=“”,那么commandfieled enabled false。但是在我尝试之后,所有的命令编辑都变成了false,其中status已经或者没有更新我的答案。如果编辑按钮位于第一列,i
将是0
对象引用,但未设置为对象的实例。更新我的答案:LinkButton cmdField=e.Row.Cells[9]。控件[0]为LinkButton;cmdField.Visible=false;-->错误,则该列中没有控件。请注意,索引是从零开始的。也许发布你的gridview布局