Uğur Canbulat
Uğur Canbulat

Reputation: 11

ASP.NET: find control in ListView with ClientId in code behind

My problem was asked to you before but I cannot make it work. I need your help, I think.

<asp:ListView runat="server" ID="listvsl">
    <ItemTemplate>
        <td>
            <img style="width: 100%; height: 150px;" src="<%#Eval("resim") %>" /><br />
            <asp:Button ID="in" runat="server" Text="Sol" ClientIDMode="Inherit" OnClick="haraket" Style="width: 75px;" />
            <asp:Button ID="cik" OnClick="haraket" runat="server" Text="Sağ" Style="width: 75px;" /><br />
            <asp:Button ID="Button10" runat="server" Text="Sil" OnClick="sil_Clickice" Style="width: 150px;" /><br />
            <asp:TextBox ID="aciklax" runat="server" TextMode="MultiLine" ClientIDMode="Inherit" /><br />
            <asp:Button ID="Button12" runat="server" Text="Güncelle" ClientIDMode="Inherit" OnClick="yazignc" />
        </td>
    </ItemTemplate>
</asp:ListView>

I need to get the aciklax TextBox with FindControl when I click "Button12"

Button bu = (Button)sender;//Button12's onclick event
string[] falanca = bu.ClientID.ToString().Split('_');
string alcez = falanca[0] + "_" + falanca[1] + "_" + "aciklax" + "_" + falanca[3];

This is my method to find the ClientId. I get the ClientId but I cannot get the TextBox control with it. Can you help me ?

Upvotes: 0

Views: 2600

Answers (1)

Martin Parenteau
Martin Parenteau

Reputation: 73731

Fortunately, you don't need to parse the ClientID to find the TextBox control. You can use the NamingContainer property of the button to find the corresponding ListView item. Then you can find the TextBox in the item, using the original ID specified in the ItemTemplate:

Button btn = sender as Button;
ListViewItem item = btn.NamingContainer as ListViewItem;
TextBox txt = item.FindControl("aciklax") as TextBox;

Upvotes: 2

Related Questions