动态控件事件获得方式
动态控件事件获得方式
aspx页面代码:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<!------------------------控件里为控件添加事件------------------------>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<table border="1" bordercolor="black" width="400">
<tr><td>id</td><td>Control</td></tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("user_id") %></td>
<td><asp:Button ID="Button1" runat="server" Text="Button" OnCommand="btnGrid_Command" CommandArgument='<%#Eval("user_name") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:wshzylConnectionString %>"
SelectCommand="SELECT [user_id], [user_name], [user_code] FROM [ws_user] WHERE ([user_id] < @user_id)">
<SelectParameters>
<asp:Parameter DefaultValue="10" Name="user_id" Type="Int64" />
</SelectParameters>
</asp:SqlDataSource>
<div runat="server" id="Panel1"></div>
</div>
</form>
</body>
</html>
cs代码:
protected void Page_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "动态按钮";
btn.CommandArgument = "CommandArgument";
btn.Command += new CommandEventHandler(btnGrid_Command);
this.Panel1.Controls.Add(btn);
}
protected void btnGrid_Command(object sender, CommandEventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "sdf", "alert('" + e.CommandArgument.ToString() + "');", true);
}