Ebikeneser
Ebikeneser

Reputation: 2364

Use checkBox and textBox in DataList

I have created a DataList in asp.net -

<asp:DataList runat="server" ID="pTextBox">

<ItemTemplate>

<asp:CheckBox ID="CheckBoxPN" runat="server"  Checked='false' />
<asp:TextBox ID="profileTextBox" runat="server" Text='<%# Container.DataItem.ToString() %>'></asp:TextBox>&nbsp;


</ItemTemplate>
</asp:DataList>

This creates checkBoxes and textBoxes based on the string values passed through from a webService.

How can I get the profileTextBox Text string value when a user clicks CheckBoxPN and populate another textBox outwith the DataList on the page with the string value??

Upvotes: 3

Views: 2455

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460228

You can use the CheckedChanged event of the CheckBox and cast it's NamingContainer to DataListItem, the you just have to use FindControl to find a different server control:

protected void CheckBoxPN_CheckedChanged(Object sender, EventArgs e)
{
    CheckBox chk = (CheckBox) sender;
    DataListItem item = (DataListItem) chk.NamingContainer;
    TextBox txt = (TextBox) item.FindControl("profileTextBox");
    this.OtherTextBoxOnPage.Text = txt.Text; // here we are
}

By the way, this approach works with any web-databound control(Repeater, GridView, etc.)

Upvotes: 2

Related Questions