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
Related
<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
I'm trying to send data from client-side to server-side (asp.net c#), if you really want to know, I want to send the window.name property.
First I thought about having a asp:HiddenField and on the OnSubmit event have some JS write the value in the hidden field. The only problem is that I can access the hidden field value (according to this) only from PreLoad event to PreRenderComplete event. The project that I'm working on has a lot of code in the OnInit event, and unfortunately I cannot move it and I need to use the window.name value here.
The other ideas that I have is to add a custom HTTP Header windowId or on the OnSubmit event have a JS that appends a parameter to the document.location.href.
I managed to write to the header from JS with the XMLHttpRequest setRequestHeader, but, maybe I did something wrong in my implementation, this generates 2 requests, the first one is the normal, expected one(clicking a button/link ...) and the second is from the XMLHttpRequest. I find this behavior very unnatural. Do you have any sugestions? (see code snippet below). I do not what to use AJAX.
var oReq = new window.XMLHttpRequest;
oReq.open('POST', document.location, false);
oReq.setRequestHeader("windowId", window.name);
oReq.send(null);
For the OnSubmit hook idea, i haven't spent to much time on it, but i think I have to append the # character before i append my windowId parameter with it's value, so that the page doesn't reload. I might be wrong about this. Any way, I have to remove this from the URL after I take the value, so that the user doesn't see the nasty URL. Do you have any sugestions?
Ok so what are your ideas?
Thank you for reading all my blabbering, and thank you, in advance, for you answers.
I would recommend the <asp:HiddenField /> (e.g., <asp:HiddenField ID="hfWindowName" runat="server" />. In OnInit you can still access its value by using Request.Form:
string windowName = Request.Form(hfWindowName.UniqueID);
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;");
I am experimenting with URL Rewriting. The first time it displays with the correct url. After perfroming any event handling, the form posts back, and then it has wrong url.
Like the page url is http://devweb.tsgdomain.com/nphnewdemo/Enewsletter/68 and when i click url button then it gives wrong url http://devweb.tsgdomain.com/nphnewdemo/Enewsletter/popup.aspx?name=dev-test-please-review-it insted of this url http://devweb.tsgdomain.com/nphnewdemo/popup.aspx?name=dev-test-please-review-it.
Please suggest some answers to why this is happening (and how I can make this work correctly).
This is happens because the form is rendering using the actual URL (and not what user see).
If you like to change that you can be rewriting the form attribute making a global handler of the form.
Here is a full solution that is tested and working.
http://www.koders.com/csharp/fid39B3A4A2AD871AA78E7E5D8643A076EF4352CDF9.aspx
In the aboce code the line that make the change is the
value = Context.Request.RawUrl;
I have a page that requires the user to go through several steps, however step is performed on the same ASPX page with different panels being displayed.
However this is a requirement that each step has a different URL, this could be a simple as a query string parameter, for example:
Step 1:
/member/signup.aspx?step=1
Step 2:
/member/signup.aspx?step=2
Step 3:
/member/signup.aspx?step=3
However I don't want to have to redirect the user to the new URL each time they continue to the next step, this would involve a lot of redirecting and also a switch statement on the page load to work out which step the user is on.
It would be better if I could alter the URL that is displayed to the user when the original request is sent back to the user, i.e. the user click "next" on step 1 the page then does some processing and then alters response so that the user then sees the step 2 URL but without any redirection.
Any ideas would be greatly appreciated.
Could you convert your Panels into steps in a Wizard control?
It would be a little more complicated than you probably want, but you could achieve this effect with the PostBackUrl property of the submitting button. I'm assuming each panel has its own "submit" button, and they could all use this property to "advance" the process. The drawback is that in order to get to submitted controls, you'd need to use the Page.PreviousPage property in order to access any controls and their values.
You could programmatically alter the PostbackUrl property of your 'Next' button on each Page_Load, based on the query string value. This is a bit strange though, as you wouldn't be able to use a standard event handler for the button click, and you'd have to use the PreviousPage property of the Page to get the data from the previous tab.
I'd say challenge this requirement. Why would anyone need to jump into the middle step? If it's a case of displaying the progress to the user, do this on the page, not in the URL.
You require that each step has different URL, than Response.Redirect is the only option. As you want to avoid the redirection, you can use IFrame but IFrame URL is not visible to user on his browser. I think redirect option is ugly(for both SERVER and CLIENT) as in this case, you first post on the page and than get that page. The best solution is POST BACK with some varible tracking step.
You could implement a form or url rewriting so that your urls end up being
/member/signup/step1/
/member/signup/step2/
/member/signup/step3/
To do this use the
HttpContext.RewritePath method which means you can rewrite /member/signup/step1/ to /member/signup.aspx?step=1 for example. See an example here.
I would also use the PRG (post request get) pattern so that each next link posts the form data of that step to the session then redirects the user top the correct next url. this will ensure that the user can navigate back and forth through the steps safely and also the url will remain intact in all your posts.
Check out server.transfer