简体   繁体   English

如何通过GridView将复选框选中的行索引传递给JavaScript

[英]how to pass checkbox checked row index to javascript from gridview

I have a gridview and i want to pass rowindex value to javascript. 我有一个gridview,我想将rowindex值传递给javascript。 I tried with following code but its not working. 我尝试使用以下代码,但无法正常工作。

In Gridview 在Gridview中

<asp:TemplateField>
     <ItemTemplate>
        <asp:CheckBox ID="chkSelect" runat="server"  onclick="javascript:return checkSts('<%# DataBinder.Eval(Container.DataItemIndex) %>')" />
     </ItemTemplate>
</asp:TemplateField> 

In Javascript: 用Javascript:

function checkSts(i) {  alert(i); }

You can type it as 您可以输入为

<input type="checkbox" onclick="return checkSts('<%#Eval("FieldName")%>')" />

and work. 和工作。

Alternative with asp:checkbox 使用asp:checkbox替代方法

<asp:CheckBox runat="server" ID="chBEna" onclick='<%#getCode(Container.DataItem)%>' />

and on code behind 以及后面的代码

protected string getCode(object oItem)
{
    string cPid = DataBinder.Eval(oItem, "FieldName").ToString();

    return "return checkSts('" + cPid + "')";
}

both checked and works. 检查和工作。

You can get rowindex of element by using parentElement to reach to the row in which particular elemnt is nested 您可以通过使用parentElement到达嵌套特定元素的行来获取元素的rowindex

eg. 例如。

onclick="callme(this)"

function callme(obj)
{
  obj.parentElement.parentElement.rowIndex--- this wud be rowindex of the row.
}

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

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