使用 jquery/javascript 从 asp:LinkBut​​ton 的 CommandArgument 属性获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4606445/
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-25 14:16:45  来源:igfitidea点击:

Getting the value from asp:LinkButton's CommandArgument attribute using jquery/javascript

javascriptasp.netjqueryrepeater

提问by LobalOrning

I need to get the value of the CommandArgumentattribute of a LinkButton, in an asp:Repeater.

我需要CommandArgument在 asp:Repeater 中获取LinkBut​​ton的属性值。

I have an asp:Repeater with 2 LinkButtons whose CommandArgument I set to a value:

我有一个带有 2 个 LinkBut​​tons 的 asp:Repeater,我将其 CommandArgument 设置为一个值:

<ItemTemplate>
    <tr class="odd">                                        
        <td><%#DataBinder.Eval(Container.DataItem, "batch_id")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "productId")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "serial_number")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "activation_card_number")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "transaction_amount","{0:C}")%></td>                                        
        <td><%#DataBinder.Eval(Container.DataItem, "response_dt", "{0:M/d/yyyy HH:mm:ss}")%></td>                                        
        <td style="text-align:center;"><%#DataBinder.Eval(Container.DataItem, "resp_process_msg")%></td>
        <td style="text-align:center;"><%#DataBinder.Eval(Container.DataItem, "resp_response_code")%></td>
        <td style="text-align:center;"><asp:LinkButton ID="lnkBtnRestageAdd" CommandName="Add" CommandArgument='<%#Eval("activation_card_number")%>' runat="server" Text="stage" class="add" OnClientClick="return false;" /></td>                                                                                
        <td style="text-align:center;"><asp:LinkButton ID="lnkBtnRestageMinus" CommandName="Subtract" CommandArgument='<%#Eval("activation_card_number")%>' runat="server" Text="stage" class="minus" OnClientClick="return false;" /></td>
      </tr>
</ItemTemplate>

I have suppressed the PostBack with OnClientClick="return false;" so that I can pop a jQuery dialog modal when the link buttons get clicked:

我用 OnClientClick="return false;" 抑制了 PostBack 这样我就可以在点击链接按钮时弹出一个 jQuery 对话框模式:

if (btnAdd != null) {
    $(".add").click(function() {
        $("#<%=divDialogAdd.ClientID %>").removeAttr("style");
        $("#<%=divDialogAdd.ClientID %>").dialog("open");
    });
}

In the modal I have 2 other asp:LinkButtons, and when the 'Yes' button is clicked I do the postback like so:

在模态中,我还有另外两个 asp:LinkBut​​tons,当单击“是”按钮时,我会像这样执行回发:

yesBtn.click(function() {
    setTimeout('__doPostBack(\'btnAdd\',\'\')', 0); //need to add a param
});

What I need to do, is somehow grab the CommandArgument value from the LinkButton in the Repeater, so that I can pass that as a parametere or assign it to a hidden field. I have tried jQuery's attr(), but that only works when the attribute was set using that function as well. How can I get this value, or what other way can I go about this?

我需要做的是以某种方式从中继器中的 LinkBut​​ton 获取 CommandArgument 值,以便我可以将其作为参数传递或将其分配给隐藏字段。我尝试过 jQuery 的attr(),但只有在使用该函数设置属性时才有效。我怎样才能得到这个值,或者我可以用什么其他方式来解决这个问题?

回答by Jishnu A P

I don't think you can get that property in javascript. Because the command argument is passed as the the argument in the doPostBack function. But i think you can change your code like this to decrease the complexity.

我认为您无法在 javascript 中获得该属性。因为 command 参数是作为 doPostBack 函数中的参数传递的。但我认为您可以像这样更改代码以降低复杂性。

<asp:LinkButton ID="lnkBtnRestageAdd" 
                CommandName="Add" 
                CommandArgument='<%#Eval("activation_card_number")%>' 
                runat="server" 
                Text="stage" 
                class="add" 
                OnClientClick='<%#Eval("activation_card_number","javascript:return YourFunction({0});")%>' />

do the stuff that you want to do in the function YourFunctionreturn trueif Yes is pressed and return false

如果按下 Yes 并返回,则在函数YourFunctionreturn 中执行您想要执行的操作truefalse