我有包含TextBox和LinkButton的Repeater。单击LinkButton时,我需要抓取TextBox.Text并执行操作...
使用EVENT Repeater1_ItemDataBound(object sender,RepeaterItemEventArgs e)我可以使用TextBox tx = e.Item.FindControl(“txCode”)作为TextBox
然而
使用EVENT Repeater1_ItemCommand(object sender,RepeaterCommandEventArgs e)我没有收到任何回复。 TextBox为空。
如何使用'OnItemCommand'从TextBox获取文本/内容?
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<li>
<asp:TextBox ID="txCode" runat="server"></asp:TextBox>
<asp:LinkButton CommandName="verifyCode" ID="lbCode" runat="server">Submit<asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
我可以获得
下面的TextBox值protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
TextBox tx = e.Item.FindControl("txCode") as TextBox;
string myText = tx.Text; '<--- working
}
我无法获得
下面的TextBox值protected void Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "verifyCode")
{
TextBox tx = e.Item.FindControl("txCode") as TextBox;
string myText = tx.Text; '<--- NOT working
}
答案 0 :(得分:2)
不要在每次回发时将Repeater
绑定到DataSource
。否则,无法正确重新加载ViewState会导致此类问题。
因此,在IsPostBack
property启用ViewState时,请务必检查Page_Load中的EnableViewState=true
:
if(!IsPostBack)BindRepeaterToDataSource();