0

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.

Josh
  • 1,648
  • 8
  • 27
  • 58

3 Answers3

2

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.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • The response.redirect is not being hit, with or without the ClientClick="return false;" With it, the button does nothing. Without it, the button submits the form and goes to the form action url. Neither execute my code. Any ideas? – Josh May 09 '12 at 16:52
  • @Josh: Why do you mention the form action url? Do you do some kind of url rewriting? – Tim Schmelter May 09 '12 at 17:04
  • The asp:button is within a form. This form gathers some account data and posts to another page where the account is created. This button is for people who already have an account to skip directly to the login page. When I click this button, it is sending me to the same page as the submit button for the form, basically submitting the form. I don't want this, I just want it to hit my code and leave the form alone. – Josh May 09 '12 at 17:10
  • You could provide a [PostBackUrl](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.postbackurl.aspx) in your button that is the current page url. – Tim Schmelter May 09 '12 at 17:19
0

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.

Curtis
  • 101,612
  • 66
  • 270
  • 352
  • The browser is not getting to my code. When clicking the button it submits the form and goes to the form action="url" page. – Josh May 09 '12 at 16:16
0

Remove the OnClientClick="return false;"

Kamran Pervaiz
  • 1,861
  • 4
  • 22
  • 42