5

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!

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

there is no linkbutton in the repeater. the linkbutton is outside the userDefDiv panel.
Oh, I see - I was fooled by the click method name. Anyway, the same answer should work. The values of text boxes within a repeater will not be available on a poastback unless EnableViewState=true.
Is your createUserDef method being called on post back, before your button code executes? If so, it overwites the posted-back values with the original values from the database.
Thanks man! That was the problem.. Actually, i've read on the internet that you HAVE TO bind the repeater once again. Thats why called the function..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.