0
votes

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
}
1
Have you debugged to see if this event is raised? Maybe you've missed to check for !IsPostBack in Page_Load. Bind the repeater not on postbacks to it's DataSource. - Tim Schmelter
if(!IsPostBack) - Damm <-- worked! Thanks @Tim Schmelter - Ravi Ram

1 Answers

2
votes

Do not bind your Repeater to it's DataSource on every postback. Otherwise ViewState cannot be reloaded correctly what causes issues like this.

So always check the IsPostBack property in Page_Load when ViewState is enabled(EnableViewState=true):

if(!IsPostBack)BindRepeaterToDataSource();