我已动态创建DataTable来绑定GridView。我有两个按钮,它们的可见性设置为false。我希望当我在按钮单击上添加新行时,我希望其中一个按钮在该新行中设置visibility = true,另一个按钮保持可见性= false。因此,该按钮应仅对用户添加的行可见,在DataTable的所有行中不可见。这是我的代码,我不知道如何解决这个问题。请帮忙
标记:
<asp:GridView ID="GridView2" runat="server" OnRowDataBound="GridView2_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button189" Visible="false" OnClick="Button189_Click" runat="server" Text="odzemi svez vrganj" />
<asp:Button ID="btnTest" Visible="false" runat="server" CommandName="odzemi" CssClass="button2" OnClick="btnTest_Click" Text="-" Width="100px" Font-Bold="True" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码背后:
protected void Button5_Click(object sender, EventArgs e)
{
MethodForAddFirstRow();
//Here i need somehow to set bnTest to be visible only for this row,other button to stay invisible
}
protected void Button5_Click(object sender, EventArgs e)
{
MethodForAddSecondRow();
//Here I need somehow to set Button189 to be visible only for this row,other button to stay invisible
}
答案 0 :(得分:1)
为了处理网格内按钮的事件,您需要使用OnRowCommand
,因此您需要将网格更新为如下所示
<asp:GridView ID="GridView2" runat="server" OnRowDataBound="GridView2_RowDataBound" onrowcommand="gv_RowCommand">
并确保每个按钮都具有CommandName
属性,如下所示
<asp:Button ID="Button189" Visible="false" OnClick="Button189_Click" runat="server" Text="odzemi svez vrganj" CommandName="Command1" />
<asp:Button ID="btnTest" Visible="false" runat="server" CommandName="Command2" CssClass="button2" OnClick="btnTest_Click" Text="-" Width="100px" Font-Bold="True" />
然后在
后面的代码中创建以下事件处理程序 void gv_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Command1")
{
}
else if(e.CommandName=="Command2")
{
}
}
要访问rowcommand事件处理程序中的按钮,您可以使用以下
GridView customersGridView = (GridView)e.CommandSource;
GridViewRow row = customersGridView.Rows[index];
Button btn = (Button)row.FindControl("Button189");
btn.Visible=false;