简体   繁体   中英

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?

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:

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.

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.

Try this:

Response.Redirect("url");
Response.End;

I believe the behavior you are seeing is correct. A response.redirect is done through the browser. So when you get an error, the page is posted to the browser and the browser triggers the redirect. The redirect causes the page_load to fire again (this is the standard flow in ASP.NET).

While you can use Server.Transfer to get around this, I'd recommend using the error redirection built into ASP.NET. See this page for more information on what's available and how to use them.

For me, the re-direct issue was with my web.config file. In that file, each page was specifically allowed access. Once I added the code block for my new page I was re-directing to in the web.config file the re-direct worked. Hopefully, if someone has a similar issue it can save them time and not waste most of a day like I did.

In web.config file I added a code block like below:

  <location path="sitedirectory/yournewpage.aspx">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM