Jquery - 使用 jquery 传递 asp 按钮命令参数

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

Jquery - Pass asp button command argument with jquery

jqueryasp.neteventargs

提问by Muno

I have asp button:

我有asp按钮:

<asp:button ID="btn1" runat="server" CommandArgument="" CssClass="btn1" OnClick="button_click"></asp:button>

and script:

和脚本:

$("a").click(function () {
            var val = $(this).attr('id').toString();                            
            $('.btn1').attr('CommandArgument',val);
            alert($('.btn1').attr('CommandArgument').toString());
            $('.btn1').click();
        });

after click it alerts me command argument. But on next step - when i trigger btn1 click with jquery it goes to codebehind and command argument is empty. Can i pass somehow command argument with jquery?

单击后它会提醒我命令参数。但是在下一步 - 当我使用 jquery 触发 btn1 单击时,它会转到代码隐藏并且命令参数为空。我可以用 jquery 以某种方式传递命令参数吗?

I've tried to save value to global variables but after postback they're all empty. And i don't want to use cookies or viewstate for that.

我试图将值保存到全局变量,但在回发后它们都是空的。我不想为此使用 cookie 或 viewstate。

Thanks!

谢谢!

回答by Yuriy Rozhovetskiy

CommandArgumentis completely a server-side property and doesn't render any html attribute. So you can't change any button's attribute and fire click on it. The good news is that you can fire postback with client-side __doPostBack function and pass your custom value as second parameter:

CommandArgument完全是服务器端属性,不呈现任何 html 属性。所以你不能改变任何按钮的属性并点击它。好消息是您可以使用客户端 __doPostBack 函数触发回发并将您的自定义值作为第二个参数传递:

<script type="text/javascript">
    $("a").click(function () {
        var val = $(this).attr('id').toString();
        __doPostBack("<%= btn1.UniqueID %>", val);
    });
</script>

And you can get passed argument in server click handler from the Request.Form collection:

您可以从 Request.Form 集合中获取服务器单击处理程序中传递的参数:

protected void button_click(object sender, EventArgs e)
{
    var argument = Request.Form["__EVENTARGUMENT"];
}

If script above won't work then maybe __doPostBack function not defined on page. In this case add this code to Page_PreRender method: ClientScript.GetPostBackEventReference(btn1, string.Empty);this will force page to define __doPostBackmethod on page.

如果上面的脚本不起作用,那么 __doPostBack 函数可能未在页面上定义。在这种情况下,将此代码添加到 Page_PreRender 方法:ClientScript.GetPostBackEventReference(btn1, string.Empty);这将强制页面在页面上定义__doPostBack方法。

回答by Mike

I find using __doPostBack('id','args') creates a full page postback when on a submit button. I find it easier is add a hiddenfield the page, set the value, then call the button.click() event

我发现使用 __doPostBack('id','args') 在提交按钮上创建一个完整的页面回发。我发现在页面上添加一个隐藏字段更容易,设置值,然后调用 button.click() 事件

    <asp:Button ID="btnTrigger" runat="server" ClientIDMode="Static" OnClick="btnTrigger_Click" />
    <asp:HiddenField ID="CommandArg" runat="server" ClientIDMode="Static" />


    $('#CommandArg').val('my argument');
    $('#btnTrigger').click();

回答by Michael

Or you can try this one:

或者你可以试试这个:

<script type="text/javascript">
    function MaybeThisWorks(obj)
    {
        __doPostBack( obj.id, 'OtherInformation' );
        return true;
    }
</script>

...

<asp:Button Text="ButtonValue" ID="RepP1" runat="server" OnClientClick="MaybeThisWorks(this);" OnClick="CodeBehindFunction" />

'OtherInformation' can be replaced by any string or variable.
e.g.
$('#calendar').fullCalendar('getDate').getMonth()+','+$('#calendar').fullCalendar('getDate').getFullYear()

'OtherInformation' 可以被任何字符串或变量替换。
例如
$('#calendar').fullCalendar('getDate').getMonth()+','+$('#calendar').fullCalendar('getDate').getFullYear()

CodeBehindFunction is the code behind function that executes on the server when you click the button (e.g. a button that prints a report and needs jquery values in codebehind function).
In codebehind function you can get the arguments by Request.Form["__EVENTARGUMENT"]

CodeBehindFunction 是当您单击按钮时在服务器上执行的代码隐藏函数(例如打印报告并需要代码隐藏函数中的 jquery 值的按钮)。
在代码隐藏函数中,您可以通过 Request.Form["__EVENTARGUMENT"] 获取参数

protected void CodeBehindFunction(object sender, EventArgs e)
{
 string arguments = Request.Form["__EVENTARGUMENT"];
 ...
}

回答by Jordan W. Cobb

I usually just add an attribute to the button in the code behind, then access it through the DOM in Javascript or jQuery.

我通常只是在后面的代码中给按钮添加一个属性,然后通过 Javascript 或 jQuery 中的 DOM 访问它。

 mybutton.Attributes.Add("MyVar", "MyValue");

This will render out to the page, and you can then access the value using Javascript.

这将呈现给页面,然后您可以使用 Javascript 访问该值。

回答by Norman Abreu Avellaneda

Try this. It worked for me.

尝试这个。它对我有用。

//Client Side JQuery:

//客户端JQuery:

$('#btnTrigger').val('MyParameter');
$('#btnTrigger').click();

//Server side click method

//服务端点击方法

String Param = Request.Form(btnTrigger.UniqueID)