Removing a querystring from url in asp.net

HI, I need to remove a querystring when a user clicks a particular LinkButton.
So for example if the querystring is http://UserProfileManager.com?UserID=1234 …. when the user clicks on the Linkbutton, I want the url to be http://UserProfileManager.com. The issue is that everything is on one page, and I am using asp:panel to show and hide different areas of the webpage.

Any ideas would be appreciated.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You have several options:

1) In your code behind, just set the LinkButton’s URL to the shorter address if the querystring contains a “UserID” key:

if (Request.QueryString["UserID"] != null) {
    this.LinkButton.PostBackUrl = "http://UserProfileManager.com";
} else {
    // other address
}

2) Send the UserID in a hidden field instead of the querystring.

3) Separate your view and edit pages – putting everything in one *.aspx is probably going to cause more trouble than it’s worth down the road.

Method 2

you cant actually remove a querystring from the URL. I mean to say that there is no .remove() method available for that.
If you still want to do so you will have to use the .substring() method and get it done manually.

Just a tip that may prove helpful:
If you are using the QueryString values to maintain different states on the same page then i would rather suggest you to keep those values in a ViewState on the page and make changes to it accordingly. This way your URL will remain clean, users wont be able to harm your flow and will work just fine.

take care!

Method 3

On page load you could extract the query string to a hidden text box.

When the user clicks the button clear the text box

If the user navigates away use the string in the text box and append the query string to the URL.

I hope this helps
Sp

Method 4

Look at the request object – while I can’t remember the exact properties, there are ones line scheme (which would return “http://”) – there will be one, or a combination that will give you what you require.

Further research – look at the Request.Url property. Then strip off the query string. I couldn’t see an exact method in the uri / uricomponent classes to give you exactly what you are looking for, but it shouldn’t be that hard.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x