Ebikeneser
Ebikeneser

Reputation: 2364

Access string value in separate DataList

I have one DataList with a textBox and CheckBox -

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

<ItemTemplate>

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


</ItemTemplate>
</asp:DataList>

Where the code behind -

 protected void CheckBoxPN_CheckedChanged(Object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;
        DataListItem item = (DataListItem)chk.NamingContainer;
        TextBox txt = (TextBox)item.FindControl("profileTextBox");
        string text = txt.Text;
        TextBox2.Text = txt.Text;

    }

This works a treat when the CheckBox and TextBox are in the same DataList.

My problem being I now have two DataLists like so -

<asp:Label ID="Label3" runat="server" Text="Exclude"></asp:Label>
<asp:DataList runat="server" ID="excludeTextBox">
<ItemTemplate>

<asp:TextBox ID="myTextBox" runat="server" Text='<%# Container.DataItem.ToString() %>'></asp:TextBox>


</ItemTemplate>
</asp:DataList>

<asp:DataList runat="server" ID="activeCheck" >
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text="Active"></asp:Label>

<asp:CheckBox ID="CheckBox1" runat="server"  Checked='<%# Container.DataItem.ToString().Equals("1") %>' OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true" />


</ItemTemplate>
</asp:DataList>

I need to access the myTextBox text value when I click on CheckBox1 where the logic goes in -

protected void CheckBox1_CheckedChanged(Object sender, EventArgs e)
    {
        // access string value in excludeTextBox

    }

How can I do this?

Upvotes: 0

Views: 331

Answers (1)

Ricketts
Ricketts

Reputation: 5213

Assuming the checkboxes and textboxes in each data list match up, you would do it very similar to the way you were doing it before, but instead of looking in the same DataListItem, you'd find the item in the other DataList with the same index, like this:

protected void CheckBox1_CheckedChanged(Object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    DataListItem item = (DataListItem)chk.NamingContainer;

    TextBox txt = (TextBox)excludeTextBox.Items[item.ItemIndex].FindControl("myTextBox");

    //--- do work here with txt
}

Good Luck!

Upvotes: 1

Related Questions