Question

I have a button like

<asp:Button ID="Button1" runat="server" Text="Submit"  onclick="RedirectToLocker"  
                 OnClientClick="return false;" UseSubmitBehavior="false" />

and a method in the codebehind like

  public void RedirectToLocker(object sender, EventArgs e)
        {
            Response.Redirect(ConfigurationManager.AppSettings["LockerLoginURL"]);

        }

But when I click the button it doesn't hit the code in the method. If I remove OnClientClick = "return false;" then the button submits the form and goes to the wrong page. What am I doing wrong?

EDIT: I am using the OnClientClick="return false;" code because without it the button for some reason acts as a submit for the form it is nested in and immediately redirects to the form action url instead of hitting my code.

Était-ce utile?

La solution

When a clientside event handler returns false, the postback is omitted.

OnClientClick="return false;" // <-- no postback

So you either need to remove it or tell us why you actually want to return false from the js-onclick event. If the Response.Redirect goes to the wrong page, you might want to change that.

Edit: So you're redirecting to another page by setting the form's Action to another url. Then you could set the Button's PostBackUrl to the same url as the current page. Then it would hit the codebehind.

Autres conseils

You need to remove OnClientClick="return false;" to run the server side method.

If the browser is redirecting to the wrong page, then check your LockerLoginURL application setting value in the web.config. This should match the URL you are being redirected to.

Change this to the correct URL.

Remove the OnClientClick="return false;"

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top