How to update querystring in C#?

后端 未结 11 1146
野的像风
野的像风 2020-12-02 12:06

Somewhere in the url there is a &sortBy=6 . How do I update this to &sortBy=4 or &sortBy=2 on a button click? Do I need to write custom string functions to creat

相关标签:
11条回答
  • 2020-12-02 12:40

    If you really want this you need to redirect to new same page with changed query string as already people answered. This will again load your page,loading page again just for changing querystring that is not good practice.

    But Why do you need this?

    If you want to change the value of sortBy from 6 to 4 to use everywhere in the application, my suggession is to store the value of query string into some variable or view state and use that view state everywhere.

    For e.g.

    in Page_Load you can write

    if (!IsPostBack)
    {
      ViewState["SortBy"] = Request.QueryString["sortBy"];
    }
    

    and everywhere else ( even after postback ) you can use ViewState["SortBy"]

    0 讨论(0)
  • 2020-12-02 12:40

    You can do it clientside with some javascript to build the query string and redirect the page using windows.open.

    Otherwise you can use Response.Redirect or Server.Transfer on the server-side.

    0 讨论(0)
  • 2020-12-02 12:46

    To modify an existing QueryString value use this approach:

    var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
    nameValues.Set("sortBy", "4");
    string url = Request.Url.AbsolutePath;
    Response.Redirect(url + "?" + nameValues); // ToString() is called implicitly
    

    I go into more detail in another response.

    0 讨论(0)
  • 2020-12-02 12:49
        private void UpdateQueryString(string queryString, string value)
        {
            PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
            isreadonly.SetValue(this.Request.QueryString, false, null);
            this.Request.QueryString.Set(queryString, value);
            isreadonly.SetValue(this.Request.QueryString, true, null);
        }
    
    0 讨论(0)
  • 2020-12-02 12:56

    I think you could perform a replacement of the query string in the following fashion on your server side code.

    NameValueCollection filtered = new NameValueCollection(request.QueryString);
    
    filtered.Remove("SortBy");
    filtered.Add("SortBy","4");
    
    0 讨论(0)
  • 2020-12-02 12:56

    The query string is passed TO the server. You can deal with the query string as a string all you like - but it won't do anything until the page is ready to read it again (i.e. sent back to the server). So if you're asking how to deal with the value of a query string you just simply access it Request.QueryString["key"]. If you're wanting this 'change' in query string to be considered by the server you just need to effectively reload the page with the new value. So construct the url again page.aspx?id=30 for example, and pass this into a redirect method.

    0 讨论(0)
提交回复
热议问题