如何更新查询字符串并重新加载下拉列表选定索引已更改事件上的页面

本文关键字:索引 下拉列表 事件 查询 更新 何更新 字符 字符串 新加载 串并 加载 | 更新日期: 2023-09-27 18:33:21

有有语言的下拉列表 每次从下拉列表中更改所选项目时如何重新加载页面?

  protected void DDLLanguages_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Request.RawUrl.Contains("Language") == false )
            {
                Response.Redirect(Request.RawUrl + "?Language=" + DDLLanguages.SelectedValue);
            }
            else
            {
             //i need to update the quesry string here 
            }
        }

如何更新查询字符串并重新加载下拉列表选定索引已更改事件上的页面

 protected void DDLLanguages_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Request.RawUrl.Contains("Language") == false )
            {
                Response.Redirect(Request.RawUrl + "?Language=" + DDLLanguages.SelectedValue);
            }
            else
            {
             //i need to update the quesry string here 
   Response.Redirect(Request.Url.GetLeftPart(UriPartial.Path)+ "?Language=" + DDLLanguages.SelectedValue);
            }
    }

接受的答案会清除任何现有的查询字符串值。

// parse url into UriBuilder //
var uri = new UriBuilder(Request.RawUrl);
// parse query part of url into a NameValueCollection //
var query = HttpUtility.ParseQueryString(uri.Query);
// update/create "Language" entry in the NameValueCollection //
query["Language"] = DDLLanguages.SelectedValue;
// put updated NameValueCollection back into uri querystring //
uri.Query = query.ToString();
// redirect page to updated uri //
Response.Redirect(uri.ToString());