开发者

Run a Javascript function before postback

开发者 https://www.devze.com 2023-02-10 07:45 出处:网络
On my ASPX page, there is a button with this code: OnClick=\"save_Click\" Is it possible to execute Javascript before postback and if the result is true, then do the postback and go to meth开发者_开

On my ASPX page, there is a button with this code:

OnClick="save_Click"

Is it possible to execute Javascript before postback and if the result is true, then do the postback and go to meth开发者_开发问答od save_click?


There is a property called "OnClientClick" as well. Here you can specify a function that will validate (I'm guessing), or just run regular javascript.

If your data is not valid you can just return false; from the method. That should cancel your postback


you should use the very well known way: return confirm('bla bla bla')

adding this snippet to the onclick attribute of the button in the page or button prerender method, server side...


http://msdn.microsoft.com/en-us/library/7ytf5t7k.aspx

<%@ Page Language="C#" %>
<script runat="server">
    protected void Button1_Click(Object sender, EventArgs e)
    {
        Label1.Text = "Server click handler called.";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
  <form id="form1" runat="server">
    <asp:Button ID="Button1" Runat="server" 
      OnClick="Button1_Click" 
        OnClientClick="return confirm('Ready to submit.');" 
        Text="Test Client Click" />
    <br />
    <asp:Label ID="Label1" Runat="server" text="" />
  </form>
</body>
</html>

Possible duplicate of : Execute ClientSide before ServerSide in ASP.NET


I changed the definition of the __doPostback function to accomplish this:

<script type="text/javascript">
    var originalDoPostback = __doPostBack;
    __doPostBack = function (p1, p2) {
        doSomethingCustomHere();
        originalDoPostback(p1, p2);
    };
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消