ASP.NET postback with jQuery?

前端 未结 6 1578
深忆病人
深忆病人 2020-12-13 21:19

I have a ASP.NET button but recently, I replaced it with a standard HTML button ... What I need to do is a postback to an ASP.NET page and ensure a method is called.

相关标签:
6条回答
  • 2020-12-13 21:25

    Check out this article

    http://www.deviantpoint.com/post/2009/03/12/Using-jQuery-UI-Dialogs-for-confirmation-windows.aspx

    The basic idea is you place the call back function in a hidden field and run an eval on the $(this).dialog('close');

    I used this to have it work in a Gridview, so if you want to know how to do that leave a comment.

    0 讨论(0)
  • 2020-12-13 21:31

    While you can do Postbacks with JQuery, it might be better to call a web method (web service that is in your page). The call could also be faster because you are not posting the entire page ViewState.

    0 讨论(0)
  • 2020-12-13 21:31

    You should be able to stop the postback with whatever JS is attached to the button onclick event, regardless of whether it is a WebControl or a standar HTML button, by returning false.

    E.g. <input type="image" id="test" onclick="doWhatever();return false;" />

    0 讨论(0)
  • 2020-12-13 21:33

    See if this helps: http://www.codeproject.com/KB/aspnet/sample.aspx.

    Basically, you declare a dummy anchor tag:

    <a id="anchorId" runat="server" onclick="return true" onserverclick="foo"></a> 
    

    In your code behind, you need to declare a foo method:

    protected void foo(object sender, EventArgs e)
    {
        // Do something here
    }
    

    Then you can invoke this anchor's onclick function with this javascript:

    document.getElementById('anchorId').click()
    
    0 讨论(0)
  • 2020-12-13 21:36

    You need to set the __EVENTTARGET hidden field to an appropriate value if you want to trigger the event handler on postback. I would do it a different way, however. Put ASP buttons in your modal dialog that have the event handler associated with them. Have the click handler that pops up the dialog return false (so that the postback doesn't happen from that button click). This way your form is posted back from an ASP button and the handler, including the client-side hidden field setting, is invoked automatically.

    0 讨论(0)
  • 2020-12-13 21:39

    I came across this post through google so I suspect others will too. I tried the methods on here which didn't work for me. I will explain my solution, but first an overview of what I'm trying to do.

    I have a form that uses jQuery ajax to post data to my webservice which in turn updates my database. I have an image upload form, for security reason you can't get the full file path to use in the ajax call to pass to the webservice... so it has to be done with a regular asp.net postback. So my dilemma is how to do both. What I did was use my ajax function to update the database, once I got a success back from the webservice I tell the form to do a postback to a certain method in the codebehind and then redirect to wherever. $("#Button1").click(function () { javascript: __doPostBack('ctl00$ContentPlaceHolder1$atest', '') });

    This is essentially exactly the same as assigning a click function to an asp.net control, except instead of clicking the button I am telling javascript to execute it. atest would be the ID i assigned to the asp.net control.
    Note: the <%=myclientid.ClientID %> will not work here.

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