How to use __doPostBack()

后端 未结 7 1378
离开以前
离开以前 2020-11-22 04:43

I\'m trying to create an asyncrhonous postback in ASP.NET using __doPostBack(), but I have no idea how to do it. I want to use vanilla JavaScript.

Some

相关标签:
7条回答
  • 2020-11-22 04:59

    Old question, but I'd like to add something: when calling doPostBack() you can use the server handler method for the action.

    For an example:

    __doPostBack('<%= btn.UniqueID%>', 'my args');
    

    Will fire, on server:

    protected void btn_Click(object sender, EventArgs e)
    

    I didn't find a better way to get the argument, so I'm still using Request["__EVENTARGUMENT"].

    0 讨论(0)
  • 2020-11-22 04:59

    This is how I do it

        public void B_ODOC_OnClick(Object sender, EventArgs e)
        {
            string script="<script>__doPostBack(\'fileView$ctl01$OTHDOC\',\'{\"EventArgument\":\"OpenModal\",\"EncryptedData\":null}\');</script>";
            Page.ClientScript.RegisterStartupScript(this.GetType(),"JsOtherDocuments",script);               
        }
    
    0 讨论(0)
  • 2020-11-22 05:02

    Here's a brief tutorial on how __doPostBack() works.

    To be honest, I don't use it much; at least directly. Many server controls, (e.g., Button, LinkButton, ImageButton, parts of the GridView, etc.) use __doPostBack as their post back mechanism.

    0 讨论(0)
  • 2020-11-22 05:09

    You can try this in your web form with a button called btnSave for example:

    <input type="button" id="btnSave" onclick="javascript:SaveWithParameter('Hello Michael')" value="click me"/>
    
    <script type="text/javascript">
    function SaveWithParameter(parameter)
    {
      __doPostBack('btnSave', parameter)
    }
    </script>
    

    And in your code behind add something like this to read the value and operate upon it:

    public void Page_Load(object sender, EventArgs e)
    {
      string parameter = Request["__EVENTARGUMENT"]; // parameter
      // Request["__EVENTTARGET"]; // btnSave
    }
    

    Give that a try and let us know if that worked for you.

    0 讨论(0)
  • 2020-11-22 05:13

    I'd just like to add something to this post for asp:button. I've tried clientId and it doesn't seem to work for me:

    __doPostBack('<%= btn.ClientID%>', '');
    

    However, getting the UniqueId seems to post back to the server, like below:

    __doPostBack('<%= btn.UniqueID%>', '');
    

    This might help someone else in future, hence posting this.

    0 讨论(0)
  • 2020-11-22 05:17

    This is also a good way to get server-side controls to postback inside FancyBox and/or jQuery Dialog. For example, in FancyBox-div:

       <asp:Button OnClientClick="testMe('param1');" ClientIDMode="Static"  ID="MyButton"  runat="server" Text="Ok" >
    </asp:Button>
    

    JavaScript:

    function testMe(params) {
        var btnID= '<%=MyButton.ClientID %>';          
        __doPostBack(btnID, params);
    }
    

    Server-side Page_Load:

     string parameter = Request["__EVENTARGUMENT"];
     if (parameter == "param1")
         MyButton_Click(sender, e);
    
    0 讨论(0)
提交回复
热议问题