我有一个嵌套的转发器,我在页脚模板中使用了一个文本框。我想在按钮点击中获取textbox.text值。这是我的转发器:
<asp:Repeater ID="rprSSFirst" runat="server" OnItemDataBound="rprSSFirst_ItemDataBound" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
<asp:Repeater ID="rprSSNested" runat="server" > //Nested Repeater
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
</ItemTemplate>
<FooterTemplate>
<div style=" padding: 20px 35px;" id='ajax'>
<asp:TextBox ID="textbox" TextMode="MultiLine" Columns="50" Rows="10" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" OnClick="btn_Save_Click" Text="Save" />
</div>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
//代码背后
protected void btn_Save_Click(object sender, EventArgs e)
{
TextBox txtAns = (TextBox)rprSSFirst.Controls[rprSSFirst.Controls.Count - 1].FindControl("textbox");
}
但txtAns
值始终为空。如何在按钮单击中获取页脚项textbox
值?或任何其他方式?
感谢您的回答。
答案 0 :(得分:4)
您必须先找到两个控件所在的嵌套RepeaterItem
。您可以通过投射NamingContainer
:
protected void btn_Save_Click(object sender, EventArgs e)
{
Button btnSave = (Button) sender;
RepeaterItem item = (RepeaterItem) btnSave.NamingContainer;
TextBox txtAns = (TextBox) item.FindControl("textbox");
}
答案 1 :(得分:0)
您可以像这样使用Commandname属性作为嵌套转发器的按钮:
<asp:Repeater ID="rprSSNested" runat="server" OnItemCommand="rprSSNested_ItemCommand" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
</ItemTemplate>
<FooterTemplate>
<div style=" padding: 20px 35px;" id='ajax'>
<asp:TextBox ID="textbox" TextMode="MultiLine" Columns="50" Rows="10" runat="server" ></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Save" CommandName="cmd" CommandArgument="arg"/>
</div>
</FooterTemplate>
</asp:Repeater>
并在c#代码中添加事件,如下所示:
protected void rprSSNested_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
if (e.CommandName == "cmd")
{
string ss = ((TextBox)e.Item.FindControl("textbox")).Text;
Response.Write(ss);
}
}
}