Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net: response.redirect not working

I have a .aspx form in which I have a combobox holding subject I retrive from a DB-table.

A submit button clicking on which questions related to that subject are viewed in a gridview.

I do it by calling the function FillGrid() in the button click event.

I also have pageindexchanging for my gridview in which FillGrid() is again called.

In my FillGrid() function I have used a try catch block. If an error occurs I want to redirect the page to error page using Response.Redirect(). The problem is this response.redirect is not working. One of the reasons of it is that on button click the form is posted twice. Because after reaching to response.redirect statement flow comes back to button click where FillGrid is called().

How can I solve this? Or to put simply, how can I prevent double posting of the form?

like image 496
anay Avatar asked Nov 30 '09 15:11

anay


3 Answers

For example you need to link index.aspx. So you write this:

Response.Redirect("index.aspx", true);

But Response.redirect is not working. Now go to design view select the button which you want to set Response.redirect and press F4 that will open Button Properties then you need to find PostBackUrl option and then locate your URL that you want.

See this image:

like image 92
Samir Rajak Avatar answered Nov 11 '22 14:11

Samir Rajak


Well, do this...

Response.Redirect("~/err.aspx", false);
HttpContext.Current.ApplicationInstance.CompleteRequest();

Now the original page won't be posted back to the browser and you will be redirected to the new page.. The problem with Response.Redirect("~/err.aspx", true); is that an exception is formed that way, which may send the control to the catch block..

•Another way is of course Server.Transfer() but I don't think you wanted that.

like image 35
Vibhum Bhardwaj Avatar answered Nov 11 '22 16:11

Vibhum Bhardwaj


I have encountered exactly same problem. Redirect was not working because of content page's master page has errors. This is sample code:

FormsAuthentication.SetAuthCookie(loginUserDetail.UserName, false);
Response.Redirect(FormsAuthentication.DefaultUrl, false);

FormsAuthentication.DefaultUrl value is "/membersarea/wellcome.aspx" and wellcome.aspx has a master page named, Site.master.

Site.master had javascript errors, so Response.Redirect command was not working.

like image 21
Ahmet Arslan Avatar answered Nov 11 '22 14:11

Ahmet Arslan