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.
When a clientside event handler returns false, the postback is omitted.
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.Redirectgoes to the wrong page, you might want to change that.Edit: So you’re redirecting to another page by setting the form’s
Actionto another url. Then you could set the Button’sPostBackUrlto the same url as the current page. Then it would hit the codebehind.