Daniel R
Daniel R

Reputation: 27

How to get duplicate ID ASP elements from codebehind C#

My code:

<asp:DataList ID="datalist" runat="server" >
    <ItemTemplate>
        <asp:Textbox ID="Values" runat="server" type="text" />
    </ItemTemplate>
</asp:DataList>
<asp:Button ID="Button1" runat="server" Text="SEND" OnClick="send" />

How could I get each Values ID elements of the DataList from code behind in C# ?

Upvotes: 1

Views: 158

Answers (1)

VDWWD
VDWWD

Reputation: 35564

You loop all the items in the DataList and use FindControl to locate the TextBox.

protected void send(object sender, EventArgs e)
{
    //loop all the items in the datalist
    foreach (DataListItem item in datalist.Items)
    {
        //find the textbox with findcontrol
        TextBox tb = item.FindControl("Values") as TextBox;

        //do something with the textbox content
        string value = tb.Text;
    }
}

Upvotes: 2

Related Questions