简体   繁体   English

获取GridView内部动态控件的ID

[英]Get ID of a dynamic control inside gridview

I have a gridview control, say, 'grd'. 我有一个gridview控件,例如“ grd”。 Inside it, I have a template field 在里面,我有一个模板字段

<asp:TemplateField>
<ItemTemplate>
<input id="chkSelectRow" type="checkbox" runat="server" name="chkSelectRow" />
</ItemTemplate>
</asp:TemplateField>

if I bind 5 rows in the grid then there will be 5 check boxes. 如果我在网格中绑定5行,那么将有5个复选框。 If I have only 2 rows then there will be 2 check boxes. 如果我只有2行,那么将有2个复选框。 My question is How can I get the id of each check boxe with javascript from an outside event, say, a button click 我的问题是如何从外部事件(例如单击按钮)获取带有javascript的每个复选框的ID

I think this is what you are looking for : 我认为这是您要寻找的:

I have written the Jquery code block which will find your checkboxes form grd and display it in alert(). 我已经编写了Jquery代码块,它将在grd找到您的复选框,并将其显示在alert()中。 you can change code as per your requirement. 您可以根据需要更改代码。

HTML for Button : 按钮的HTML:

 <input type="button" value="save" onclick="test();" />

Jquery: jQuery:

 function test() {

            $('input[type="checkbox"][id*="chkSelectRow"]').each(function () {
                alert($(this).attr('id') + ' : ' + $(this).attr('checked'));
            });
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM