参考:
http://blog.csdn.net/insus/archive/2008/03/09/2159352.aspx
如要对Label,
那可以这样子:label lbl = (Label)gvr.FindControl("Label1");
lbl.visible= ...
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="EntityDataSource1" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
<asp:TemplateField><ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" /></ItemTemplate></asp:TemplateField>
<asp:TemplateField><ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" CommandName="ShowHide" /></ItemTemplate></asp:TemplateField>
</Columns>
</asp:GridView>
通过内置模板列,设置一个按钮的CommandName为“ShowHide”,实现GridView的RowCommand方法控制内部控件隐现即可。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ShowHide")
{
var c = (e.CommandSource as Control).FindControl("CheckBox1");
c.Visible = !c.Visible;
}
}
注意,如果是控制未命名(没有ID)的控件隐现,则需要通过类似这样的索引来访问:(e.CommandSource as Control).Controls[3]