Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 传入查询字符串以生成新URL_C#_Asp.net_Query String - Fatal编程技术网

C# 传入查询字符串以生成新URL

C# 传入查询字符串以生成新URL,c#,asp.net,query-string,C#,Asp.net,Query String,我有一个带有下拉列表的网页,当您选择一个产品时,该网页会用新信息刷新。我想做的是向页面传递一个查询字符串,这样我就可以通过URL打开页面,而不是单击下拉列表项 我不知道如何在页面加载中执行此操作。我的页面默认为默认选项,但我希望其他下拉列表项能够通过URL访问,因此我需要知道如何向其传递查询字符串 private void BindDropDownList() { { try {

我有一个带有下拉列表的网页,当您选择一个产品时,该网页会用新信息刷新。我想做的是向页面传递一个查询字符串,这样我就可以通过URL打开页面,而不是单击下拉列表项

我不知道如何在页面加载中执行此操作。我的页面默认为默认选项,但我希望其他下拉列表项能够通过URL访问,因此我需要知道如何向其传递查询字符串

 private void BindDropDownList()
        {
            {
                try
                {
                    string[] productTexts;

                    string[] productValues;





                    BizManager biz = new BizManager();

                    biz.GetProductSeriesList(out productTexts, out productValues);

                    DDLProduct.Items.Clear();

                    int x = 0;
                    foreach (string s in productTexts)
                    {
                        ListItem li = new ListItem(s, productValues[x]);
                        x++;
                        DDLProduct.Items.Add((li));
                    }
                }
                catch (Exception ex)
                {
                    ErrMsg = App.HandleError(MethodBase.GetCurrentMethod(), ex, string.Empty);
                }
            }
        }
在这里,我从web配置中的一个键调用params,但我需要我的URL如下所示

您可以使用访问查询字符串值

Request.QueryString[“产品”]


您需要处理它不存在的情况,并执行它现在执行的操作,否则,您的下拉列表需要默认为QueryString值。

您可以执行类似的操作。检查QueryString是否存在,如果存在,请检查DropDownList值是否存在匹配值。找到后,设置正确的DropDownList值

if (Request.QueryString["Product"] != null)
{
    string product = Request.QueryString["Product"].ToString();

    if (DropDownList1.Items.Cast<ListItem>().Any(x => x.Value == product))
    {
        DropDownList1.SelectedValue = product;
    }
}
if(Request.QueryString[“Product”!=null)
{
字符串product=Request.QueryString[“product”].ToString();
if(DropDownList1.Items.Cast().Any(x=>x.Value==product))
{
DropDownList1.SelectedValue=产品;
}
}

显示下拉列表的代码及其逻辑。执行
catch(异常示例)
不是最好的方法-这是一种反模式。你真的应该只捕获一个你可以有意义地处理的特定异常。为什么
.ToList()
在这里呢?@Enigmativity。没有理由。我很快写了这篇文章,但没有意识到
.Items
已经是一个集合了。