Jquery - Pass asp button command argument with jquery

前端 未结 5 1666
野的像风
野的像风 2020-12-18 07:11

I have asp button:


         


        
相关标签:
5条回答
  • 2020-12-18 07:38

    Try this. It worked for me.

    //Client Side JQuery:

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

    //Server side click method

    String Param = Request.Form(btnTrigger.UniqueID)
    
    0 讨论(0)
  • 2020-12-18 07:40

    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()

    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"]

    protected void CodeBehindFunction(object sender, EventArgs e)
    {
     string arguments = Request.Form["__EVENTARGUMENT"];
     ...
    }
    
    0 讨论(0)
  • 2020-12-18 07:42

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

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

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

    0 讨论(0)
  • 2020-12-18 07:48

    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

        <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();
    
    0 讨论(0)
  • 2020-12-18 08:02

    CommandArgument is 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:

    <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:

    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 __doPostBack method on page.

    0 讨论(0)
提交回复
热议问题