简体   繁体   中英

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.

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.

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!

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.

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

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