javascript 如何在asp.net中使用javascript获取命令参数的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18374102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:40:42  来源:igfitidea点击:

How to get value of command arugment using javascript in asp.net

javascriptasp.net

提问by user2660267

         <asp:LinkButton ID="lnkprogress" runat="server" class="label label-info"  BackColor="#589FC2"  CommandArgument='<%#Eval("BookingId")%>'  OnClientClick="jk();" >In progress</asp:LinkButton>

This button exist in repeater for every row.I want to get the value of command argument using javascript.please reply

此按钮存在于每一行的转发器中。我想使用 javascript.please 获取命令参数的值

回答by rtpHarry

You can do it using html5 data attributes like this:

您可以使用这样的 html5 数据属性来做到这一点:

<asp:LinkButton ID="lnkprogress" runat="server" class="label label-info"  BackColor="#589FC2"  data-bookingid='<%#Eval("BookingId")%>'  CommandArgument='<%#Eval("BookingId")%>'  OnClientClick="jk();" >In progress</asp:LinkButton>

Then retrieve it using some JavaScript like this:

然后使用一些 JavaScript 检索它,如下所示:

var lnkProgress = document.getElementById('<%= lnkProgress.ClientID %>');
var bookingID = lnkProgress.getAttribute("data-bookingid");

Although I'm assuming that your lnkProgress is going to be inside a naming container like one of the databound controls so it might not be as easy as getElementById('<%= lnkProgress.ClientID %>');to get a reference to it.

尽管我假设您的 lnkProgress 将位于一个命名容器内,如数据绑定控件之一,因此它可能不像getElementById('<%= lnkProgress.ClientID %>');获得对它的引用那么容易。

回答by Bhavik

<asp:LinkButton ID="lnkprogress" runat="server" class="label label-info"  
    BackColor="#589FC2" data-CommandArgument='<%#Eval("BookingId")%>'
    OnClientClick="jk();" >In progress</asp:LinkButton>

Using Javascript:

使用 JavaScript:

document.getElementsId("lnkprogress")[0].getAttribute("CommandArgument")

Check this

检查这个

Using jQuery:

使用jQuery:

$('#lnkprogress').data('CommandArgument');

回答by 9988ScooterGirl

Add an html5 attribute as suggested by rtpHarry and send the control as a parameter in OnClientClick:

添加 rtpHarry 建议的 html5 属性,并将控件作为参数发送到 OnClientClick 中:

<asp:LinkButton ID="lnkprogress" runat="server" class="label label-info"  `BackColor="#589FC2"  data-bookingid='<%#Eval("BookingId")%>'  CommandArgument='<%#Eval("BookingId")%>'  OnClientClick="javascript: jk(this);" >In progress</asp:LinkButton>

In your function just reference the control attribute:

在您的函数中只需引用 control 属性:

function jk(ctrl) {
            var id = ctrl.getAttribute("data-bookingid");
        }

By sending the control as a parameter you don't have to worry if the linkbutton is inside another container, i.e. gridview.

通过将控件作为参数发送,您不必担心链接按钮是否在另一个容器内,即 gridview。