Refresh Page C# ASP.NET - c#

Is there a Page.Refresh type of command to refresh a page?
I don't want to redirect to the page or refresh in JavaScript.

I think this should do the trick (untested):
Page.Response.Redirect(Page.Request.Url.ToString(), true);

Careful with rewriting URLs, though. I'm using this, so it keeps URLs rewritten.
Response.Redirect(Request.RawUrl);

Response.Redirect(Request.Url.ToString());

You can just do a regular postback to refresh the page if you don't want to redirect. Posting back from any control will run the page lifecycle and refresh the page.
To do it from javascript, you can just call the __doPostBack() function.

Use:
Response.Redirect(Request.RawUrl, true);

You shouldn't use:
Page.Response.Redirect(Page.Request.Url.ToString(), true);
because this might cause a runtime error.
A better approach is:
Page.Response.Redirect(Page.Request.Url.ToString(), false);
Context.ApplicationInstance.CompleteRequest();

Depending on what exactly you require, a Server.Transfer might be a resource-cheaper alternative to Response.Redirect. More information is in Server.Transfer Vs. Response.Redirect.

I use
Response.Redirect(Page.Request.Path);
If you have to check for the Request.Params when the page is refresh use below. This will not rewrite the Request.Params to the URL.
Response.Redirect(Page.Request.Path + "?Remove=1");

I use # for Current page url address at Redirect to Refresh and that working currectly.
What do you think about this:
Response.Redirect("#")

Call Page_load function:
Page_Load(sender, e);

To refresh the whole page, but it works normally:
Response.Redirect(url,bool)

Related

Remove GET variables on URL when doing a submit

I have this problem: I have a page with a datagrid with links that redirect to the same page, adding variables to the URL. Once the user chooses some options in the form, it submits, once again, to itself.
My issue is that I must remove all those variables in the URL or they disrupt the submit. Any way to clean the URL while doing the POST?
No, the POST will happen with the current querystring.
You could add the querystring parameters to Session and negate the need to pass this in via the URL.
Solved it. Just need to add a PostBackUrl to the button. Sorry if it was a dumb question.

Response.Redirect disables back button

I am using using Response.Redirect to transfer control to another page. but this disables back navigation in the browser. What is alternative way to achieve this ?
Response.Redirect does not disable the back button by any means. Check it using web-console or put debug point on your page and check again. I think in your case the browser back button is working but your page may be forcing it to redirect again to the second page
You can do a client-side redirect.
Here is code for 5 different ways to do this with JavaScript
I recommend the first one which is to set the window.location.href property in JS.
You should use Server.Transfer() instead.
You must have added a javascript function somewhere in your project ( like MasterPage or parent page ) which is disabling back button and which is bad from user point of view , otherwise it isn't possible for browser's back button to get disabled on its own just because you have used response.redirect in your code -behind .

Forcing a postback

Is there a way to force a postback in code?
I'm looking to force the raising of a postback from a method in the c# code behind my asp.net web application.
You can try redirecting to same page.
Response.Redirect(Request.RawUrl);
A postback is triggered after a form submission, so it's related to a client action...
take a look here for an explanation:
ASP.NET - Is it possible to trigger a postback from server code?
and here for a solution:
http://forums.asp.net/t/928411.aspx/1
Simpler:
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "DoPostBack", "__doPostBack(sender, e)", true);
Here the solution from http://forums.asp.net/t/928411.aspx/1 as mentioned by mamoo - just in case the website goes offline. Worked well for me.
StringBuilder sbScript = new StringBuilder();
sbScript.Append("<script language='JavaScript' type='text/javascript'>\n");
sbScript.Append("<!--\n");
sbScript.Append(this.GetPostBackEventReference(this, "PBArg") + ";\n");
sbScript.Append("// -->\n");
sbScript.Append("</script>\n");
this.RegisterStartupScript("AutoPostBackScript", sbScript.ToString());
No, not from code behind. A postback is a request initiated from a page on the client back to itself on the server using the Http POST method. On the server side you can request a redirect but the will be Http GET request.
You can use a data-bound control like the Repeater or ListView, re-bind it to a list of control properties as needed, and let it generate the controls dynamically.
As an alternative, you can use Response.Redirect(".") to re-load the same page.
By using Server.Transfer("YourCurrentPage.aspx"); we can easily acheive this and it is better than Response.Redirect(); coz Server.Transfer() will save you the round trip.
You can manually call the method invoked by PostBack from the Page_Load event:
public void Page_Load(object sender, EventArgs e)
{
MyPostBackMethod(sender, e);
}
But if you mean if you can have the Page.IsPostBack property set to true without real post back, then the answer is no.

How to know which page has called the Usercontrol

I have a usercontrol(.ascx) page that is used by about 10 pages to display something in common. In the control i should know which aspx page is using(or calling). How is that possible?
Thank you in advance!
You can get the page object by calling:
this.Page
Or the URL by calling
HttpContext.Current.Request.Url
You can check Request.Url or (Page)HttpContext.Current.Handler.
The control has a Page property that will be set to the page that it is on.

<form> action when using Server.Transfer()

I am using Server.Transfer() to transfer processing from one page to another. The problem is that the form action in the source of the page, having been transferred, refers to the destination page and not the original page as per the URL in the browser.
Is there a way to make the action of the form reflect the URL in the browser, rather than the actual destination page?
Thanks in advance!
Mark
Not to worry, I've rewritten my routing code using the System.Web.Routing namespace so all the logic is centralised in my global.asax. Works a treat!
Thanks for your help.
Mark
if you give the form an id you might be able to change the action in the attributes property.
<form runat="server" id="form1" action="">
</form>
and then in the code refer to the form like this:
form1.Attributes["action"] = "new action";
changing the action will probably cause postback events intended for the new page to work incorrectly. Your other alternative if your design permits would be to use the Response.Redirect.
Also, you might want to look into PostBackUrl for the buttons on the new page, which will change the page that they post to.
You should investigate if HttpContext.RewritePath might be useful here. Typically in cases like this you would use rewrite path on the PreRender event of the destination page using the URL of the original page. This causes controls that use the current internal path to generate URLs and such to "think" they are still on the original URL at the time they render themselves.

Categories

Resources