Repeater中PostBack提交通过Request.Form[btn.UniqueID]得到相关表单内容
前台代码:
代码
<div>
<asp:Repeater ID="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text='<%#Eval("name") %>' CommandName="get"/>
</ItemTemplate>
</asp:Repeater>
</div>
<asp:Repeater ID="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text='<%#Eval("name") %>' CommandName="get"/>
</ItemTemplate>
</asp:Repeater>
</div>
代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("name", typeof(String));
dt.Rows.Add(new Object[] { "张三" });
dt.Rows.Add(new Object[] { "李四" });
dt.Rows.Add(new Object[] { "王五" });
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}
//Repeater的ItemCommand事件
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "get")
{
Button btn = e.Item.FindControl("Button1") as Button;
if (btn != null)
{
Response.Write("Rpeater控件中的子空间Button的name属性值为:" + btn.UniqueID + "<br/>");
Response.Write("得到PostBack提交的值:" + Request.Form[btn.UniqueID]);
}
}
}
{
if (!IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("name", typeof(String));
dt.Rows.Add(new Object[] { "张三" });
dt.Rows.Add(new Object[] { "李四" });
dt.Rows.Add(new Object[] { "王五" });
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}
//Repeater的ItemCommand事件
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "get")
{
Button btn = e.Item.FindControl("Button1") as Button;
if (btn != null)
{
Response.Write("Rpeater控件中的子空间Button的name属性值为:" + btn.UniqueID + "<br/>");
Response.Write("得到PostBack提交的值:" + Request.Form[btn.UniqueID]);
}
}
}