get value from textbox within repeater asp.net c#

与世无争的帅哥 提交于 2019-12-10 15:43:00

问题


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:


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



来源:https://stackoverflow.com/questions/8446226/get-value-from-textbox-within-repeater-asp-net-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!