Change url on server side - c#

When user clicked on A tag ( <a href='showSomething.aspx?id=11&key=fixed'>Bring Something</a> ) showSomethingPage.aspx will be shown with URL parameters. But I want to change URL from showSomething.aspx?id=11&key=fixed to showSomething.aspx?key=fixed in despite of shown the thing with id 11.

Try saving the ID in the session state and then using Response.Redirect.

Use a redirect:
C# code (put this in Page_Load or something):
Response.Redirect("showSomething.aspx?&key=fixed");

If you just want to hide the URL from the status bar your link, use the onclick event to open the URL:
a href="javascript:void(0)" onclick="window.location='showSomething.aspx?id=11&key=fixed'"
Hope it helps you. ;)

You can save the id in the session and then redirect to showSomething.aspx?key=fixed. then showSomething.aspx?key=fixed will read the session and file the id=11 and show it.

Related

ASP.NET Redirection

I'm currently redirecting a page using
Response.Redirect("URL");
this works great, however it changes the url too. For example:
http://localhost/index.aspx
Redirected to: test.aspx will change the url to:
http://localhost/test.aspx
Is it possbile to redirect the content without changing the URL?... so the Url will be:
http://localhost/test.aspx
BUT the content would of been redirected to
test.aspx
Thank you.
Try to use Server.Transfer(URL); to avoid changing the url.
More info here.
And also check the differences at this post and this one.
Make sure that this solution meets your needs.
Use Server.Transfer
This will show the content of default.aspx page, but it will not change the url
Server.Transfer("Default.aspx");

ASP.NET Page Cycle Confusion

If I have a button on my ASP.NET page which will take you to another page but that page will do something and then will send user back using
Uri uu= Request.UrlReferrer;
if (uu!= null)
Response.Redirect(uu.ToString());
Now Which page event can I use so that when other pages displays I can display a message box.
In short I am running my custom code in a "aspx" page where user is directed on button click, and then after custom code I am sending user back to old page, but it happens so quickly that user doesn't realize that he went on another page, now I want to display a message box after redirect on same page user started from, what to do :S !
More Information
EDIT
Sorry guys but I can't make changes to ASP page where button is at all
:(
I'm a bit unsure about how the Request.UrlReferrer gets set. I think it's a browser implementation detail. So I wouldn't trust on that.
I would go for something like
A.aspx -> Redirects to -> B.aspx
B.aspx -> Redirects to -> B.aspx?message=1
And check if message=1 is set.
But if you want to use the Request.UrlReferrer it should be accessible on Page_Load
If you use it this way, it'll never appear to the client.
Maybe try redirecting back using javascript with a delay so user can be informed
The best thing to do it's add a flag to the redirected page so you can show something special when the flag is turned on
Uri uu= Request.UrlReferrer;
if (uu!= null)
Response.Redirect(uu.ToString() + "?Message=DataHasChanged");
and then in the ASP page
<% if (Request.QueryString["Message"] == "DataHasChanged") { %>
<div class="alert">The data has changed. Please review it or whatever</div>
<% } %>

asp.net hyperlink - click to make the POST request

<asp:HyperLink ID="hlBanner" Target="_blank" style="padding-left:10px;" runat="server" ImageUrl="banner.png" />
I want to send some infomation such as FName, LName & Email into the POST request to another page ProcessInfo.aspx. This processing page pulls the values from the posted form like Request.Form["FName"]. I have to use only the POST technique, because i cannot make the changes to the ProcessInfo.aspx.
I cannot use the Querystring parameters to pass the info. I was hoping to use the WebRequest class to make the redirection to the second page.
How can i build the navigateURL property for making the POST request ?? Pls suggest. I am open to change the control also.
If you want to use Request.Form Collection here is a good solution.
http://www.c-sharpcorner.com/UploadFile/desaijm/ASP.NetPostURL11282005005516AM/ASP.NetPostURL.aspx
Use the Server.Transfer to call ProcessInfo.aspx.
If you have the inputs FName, LName & Email on your current page, you will receive the values you ProcessInfo page too.
i found the easier way and is already present on stackOverflow.com
https://stackoverflow.com/a/6440159/86023

ASP.net Redirect to the calling page

I have a page that calls another page with some query string parameters. I want to return back to that page after clicking on a button.
I have to mention that I write that code in a user control and I don't know what page called that second page.
Is there something like Back button in browsers?
Simplest way use javascript on client side with
window.back();
For server side you need to save the url referer in page_load:
if(!Page.IsPostback)
{
ViewState["GoBackTo"] = Request.UrlReferrer;
}
and on a button click using Response.Redirect:
Response.Redirect( ViewState["GoBackTo"].ToString() );
edit: please note ppumkin's comment below!
You could look at Cross Page Posting.
Alternatively, if you are generating the link programatically you could include the returnUrl in the url e.g. http://localhost/secondpage.aspx?returnurl=firstpage.aspx
You can then read this querystring parameter in the secondpage and perform as redirect back once your work is done.
You can use the Request.UrlReferrer, but it is not necessarily sent from the client all the time:
Response.Redirect(Request.UrlReferrer.AbsoluteUri);
put this line of code on the page load event
Btn_Back.Attributes.Add("onClick", "javascript:history.back(); return false;");

How to remove query string parameters from the url?

Assume I have the link http://www.somesite.com/file.aspx?a=1&b=2
And now I want to remove all the parameters, so it becomes:
http://www.somesite.com/file.aspx
Or I may want to remove only 1 of the parameters such as
http://www.somesite.com/file.aspx?b=2
Is there a way to do the above in C#? What is happening is that I am coming from another page with a parameter called edit in the url, but when the page does a post back, the edit parameter is still there, so it still thinks it is in edit mode. Example:
User A goes to page one.aspx and clicks on an edit link. They are taken to two.aspx?edit=true. During page load, it sees the the query string parameter edit is not null and it puts the contents in edit mode. Once the user is done editing, the page is refreshed, but the url is still two.aspx?edit=true and keeps the contents in edit mode, when in fact it should be two.aspx
Request.Querystring is read-only collection - You cannot modify that.
If you need to remove or change the param in querystring only way out is to trigger a new GET request with updated querystring - This means you will have to do Response.Redirect with updated URL. This will cause you lose the viewstate of the current page.
Use the PostBackUrl property, for example:
<asp:Button ID="DoneEditingButton" runat="server" Text="Done editing" PostBackUrl="~/two.aspx" />
When you are done with the edit you are doing a post back so just define the action to post to two.aspx instead of just posting back to itself that way it will drop off the get parameters.
How about checking Page.IsPostBack to see if the current request is a postback or not?
if you have only string, you can use:
strinULR.Split('?').First();
or
strinULR.Split('?').FirstOrDefault();
Try something like this.
if (url.Contains("?"))
url = url.Substring(0, url.IndexOf("?"));
In this example, I'm checking if the url even contains a query string, and if it does, subtracting getting the "left part" of the string prior to the ?.
Late but you can do this to remove query string from URL without another GET Request.
http://www.codeproject.com/Tips/177679/Removing-Deleting-Querystring-in-ASP-NET

Categories