Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get value from textbox within repeater asp.net c#

I've been trying to get this working for a couple of hours now but nothing from google could help me fix the problem.

I have a very simple repeater control:

   <asp:Panel ID="userDefDiv" Visible="false" runat="server">
                <asp:Repeater ID="userDefRepeater" EnableViewstate="false" runat="server">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" EnableViewState="false"></asp:TextBox><br/>
                    </ItemTemplate>
                </asp:Repeater>
            </asp:Panel>

the userDefDiv panel is inside another panel, which is inside contentPLaceHolder. the parent panel to userDefDiv does NOT have the "enableviewstate="false"".

So. Everything on this page happens after a couple of linkbuttons_click. so nothing happens during page_load. And after i click another linkbutton i want to get the data from the different textboxes that is within the repeater.

C# code:

This is the code to create all the repeater items.

public void createUserDef()
{
        DataTable userDefData;
        userDefData = ..... (data from Database.)

            userDefDiv.Visible = true;
            userDefRepeater.DataSource = userDefData;
            userDefRepeater.DataBind();
}

The code for the linkbutton:

protected void linkButton_Click(object sender, EventArgs e)
{
    createUserDef();

    Label2.Visible = true;
    foreach (RepeaterItem item in userDefRepeater.Items)
    {
        TextBox box = (TextBox)item.FindControl("TextBox1");
        string b = box.Text;
        Label2.Text += b + " . ";
    }
}

As you see i create the repeater once again during the click. But the only thing i can read in label2. is a a number of " .", on dot for each textbox. but the text from the textbox is empty.. What am I doing wrong??

thanks for reading! Mattias

SOLUTION:

  1. add EnableVIewState="true" to textbox & repeater.

  2. Dont call call dataBind() before you get the values.

Thanks!

like image 305
Easyrider Avatar asked Oct 09 '22 06:10

Easyrider


1 Answers

You need to set EnableViewState to 'true' for linkbuttons to work properly in a repeater

like image 80
Ray Avatar answered Oct 13 '22 10:10

Ray