Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.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# 获取所选值下拉列表选项_C#_Jquery_Asp.net - Fatal编程技术网

C# 获取所选值下拉列表选项

C# 获取所选值下拉列表选项,c#,jquery,asp.net,C#,Jquery,Asp.net,我有下拉列表控件,并将动态添加选项添加到下拉列表中,但当我得到值选项时,返回字符串为空 请帮帮我 谢谢你的代码隐藏 ddl.Items[ddl.SelectedIndex].Value 这取决于您如何填充DropDownList。如果您使用的是数据集,则应执行以下类似操作: DataSet ds = yourProcedureToGetDataSet(); yourDropDownList.DataTextField = ds.Tables[0].Columns["name"].Caption

我有下拉列表控件,并将动态添加选项添加到下拉列表中,但当我得到值选项时,返回字符串为空

请帮帮我 谢谢你的代码隐藏

ddl.Items[ddl.SelectedIndex].Value

这取决于您如何填充DropDownList。如果您使用的是数据集,则应执行以下类似操作:

DataSet ds = yourProcedureToGetDataSet();
yourDropDownList.DataTextField = ds.Tables[0].Columns["name"].Caption;
yourDropDownList.DataValueField = ds.Tables[0].Columns["id"].Caption;
yourDropDownList.DataSource = ds;
yourDropDownList.DataBind();
yourDropDownList.Items.Insert(0, new ListItem("Select a whatever", "-1"));
然后可以读取所选值:

int i = int.Parse(yourDropDownList.SelectedValue);

希望这有帮助。

使用以下代码:
请求.Form[ddl.UniqueID]


上面的代码允许获取所选选项的值。

以下是我在访问的站点上构建的一个小方法。。。希望它能帮助某人:)


失败的代码是什么?我们需要更多信息你想在jQuery函数或你的代码中获取值吗?你正在用jQuery添加项,并试图用C#获取值?是的,curt,我用jQuery添加值,然后用C#获取值。是的,curt,我用jQuery添加值,然后用C#获取值。我用jQuery添加动态选项,用C#获取值。不,我使用jquery将选项添加到下拉列表中,并希望使用c#获取所选值选项。谢谢,我理解这一点,因为您在问题中添加了
jquery
标记:)。这种方法正好适合这种情况。您在填充DDL时提供了值吗?
    private string GetDDLValueByName(HtmlDocument doc, string Name)
    {
        string Value = "";
        HtmlElementCollection selects = GetElementCollectionFromDocument(webBrowser1.Document, "SELECT");
        if (selects != null)
        {
            foreach (HtmlElement select in selects)
            {
                if (select.Name == Name)
                {
                    HtmlElementCollection children = select.Children;
                    if (children.Count > 0)
                    {
                        foreach (HtmlElement child in children)
                        {
                            if (child.OuterHtml.Contains("selected"))
                                return child.InnerText;
                        }
                    }
                    else
                        return "";
                }
            }
        }
        return Value;
    }