如何在button_Click事件中设置gridView中按钮的可见性为false? 它一直给我一个错误:编译错误
编译器错误消息:CS0103:名称' btnTest'在当前上下文中不存在
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView2_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button189" OnClick="Button189_Click" runat="server" Text="odzemi svez vrganj" />
<asp:Button ID="btnTest" runat="server" CommandName="odzemi" CssClass="button2" OnClick="btnTest_Click" Text="-" Width="100px" Font-Bold="True" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
以下是我想要在gridview中隐藏按钮的按钮事件。
protected void Button10_Click(object sender, EventArgs e)
{
btnTest.Visible = true;
Button189.Visible = false;
}
答案 0 :(得分:2)
使用以下代码将按钮可见属性设置为 True 或 False 。将点击的gridview中的按钮将充当发件人。这样做会为您提供触发按钮的行ID,然后您可以找到其他控件并在该特定行上设置所需的属性。
protected void Button189_Click(object sender, EventArgs e)
{
Button Btn = sender as Button;
GridViewRow gvRow = Btn.NamingContainer as GridViewRow;
int rowId = gvRow.RowIndex;
Button Button189 = GridView2.Rows[rowId].FindControl("Button189") as Button;
Button btnTest = GridView2.Rows[rowId].FindControl("btnTest") as Button;
Button189.Visible = false;
btnTest.Visible = true;
}
答案 1 :(得分:1)
您需要遍历gridview行并找到控件
foreach (GridViewRow row in grid.Rows)
{
if(row.RowType == DataControlRowType.DataRow)
{
var control = ((Button)row.FindControl("btnTest"));
if (control != NULL)
{
((Button)control).Visible = false;
}
}
}