简体   繁体   中英

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 aa 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!

您需要将EnableViewState设置为“true”,以使链接按钮在转发器中正常工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM