GridView中对按钮的访问
在GridView同一列中声明两个Button,开始时一个Button是隐藏的。
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<table>
<td align="center" style="width=78px">
<asp:Button ID="Edit" runat="server" Text="编辑" Visible="true" OnClick="Edit_Click"/>
<asp:Button ID="Cancel" runat="server" Text="取消" Visible="false" OnClick="Cancel_Click" />
</td>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_LoadComplete(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("11");
dt.Rows.Add("1");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Edit_Click(object sender, EventArgs e)
{
Control c =((Button)sender).Parent.FindControl("Cancel");
if (c != null)
{
c.Visible = true;
}
}
protected void Cancel_Click(object sender, EventArgs e)
{
Control c = (Button)sender;
if (c != null)
{
c.Visible = false;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:Button ID="Edit" runat="server" Text="编辑" Visible="true" OnClick="Edit_Click"/>
<asp:Button ID="Cancel" runat="server" Text="取消" Visible="false" OnClick="Cancel_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>