I have Repeater that contains a TextBox and a LinkButton. When the LinkButton is clicked, I need to grab the TextBox.Text and do stuff …
Using the EVENT Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) I am able to get the value of the TextBox using TextBox tx = e.Item.FindControl(“txCode”) as TextBox
However
Using the EVENT Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e) I am NOT getting anything back. The TextBox is empty.
How can I get the text/content from the TextBox using ‘OnItemCommand’?
<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>
I am able to get the TextBox Value below
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
TextBox tx = e.Item.FindControl("txCode") as TextBox;
string myText = tx.Text; '<--- working
}
I am NOT able to get the TextBox Value below
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
}
Do not bind your
Repeaterto it’sDataSourceon every postback. Otherwise ViewState cannot be reloaded correctly what causes issues like this.So always check the
IsPostBackproperty in Page_Load when ViewState is enabled(EnableViewState=true):