Get ClientID of a Control Placed INSIDE a DataControl

跟風遠走 提交于 2019-12-13 05:37:47

问题


Can anyone help me to get the ClientID of a control which is kept in the ItemTemplate of an DataControl? My control is like this.

<asp:ListView ID="ListView1" runat="server">
            <LayoutTemplate>
                 <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                    <tr runat="server">
                        <td>Contacts</td>
                    </tr>
                    <tr id="itemPlaceholder" runat="server" >

                    </tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                 <tr style="">
                    <td>
                        <asp:CheckBox ID="chkFlag" runat="server" AutoPostBack="true" />
                    </td>                
                    <td><asp:Label ID="LabelContacts" runat="server" Text='<%#Eval("cont_name") %>'></asp:Label>
                    <asp:HiddenField ID="hfGSM" runat="server" Value='<%#Eval("cont_gsm") %>' />
                    </td>
                </tr>
            </ItemTemplate>
            </asp:ListView>

I need to get the ClientID of CheckBox from inside this ListView. Do anyone knows how to do it? Please help me in this


回答1:


something like this should work, if you assign it to an attribute of the CheckBox for example, or test it rendering it in the label of the CheckBox:

<asp:CheckBox ID="chkFlag" runat="server" AutoPostBack="true"
  onClick="alert('<%# ((Control)Container).FindControl("chkFlag").ClientID %>')" />

P.S. I just copied from here, search in SO when opening new questions, or do we want to explode their SQL? :D

How do I find the Client ID of control within an ASP.NET GridView?




回答2:


in the ItemDataBound event handler... you can find the control and get the client id like this

((CheckBox)e.Item.FindControl("chkFlag")).ClientID


来源:https://stackoverflow.com/questions/7496348/get-clientid-of-a-control-placed-inside-a-datacontrol

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