如何在DropDownList SelectedIndexChanged事件上更新查询字符串并重新加载页面?

时间:2014-12-07 14:44:11

标签: c# asp.net drop-down-menu query-string

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

  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 

            }

        }

2 个答案:

答案 0 :(得分:1)

 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);

            }

    }

答案 1 :(得分:0)

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

// 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());