【问题标题】:Get Value selected dropdownlist option获取值选择的下拉列表选项
【发布时间】:2013-11-07 13:11:40
【问题描述】:

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

请帮帮我 谢谢

【问题讨论】:

  • 失败的代码是什么?我们需要更多信息
  • 你想在 jQuery 函数或你的代码中获取值吗?
  • 您正在使用 jQuery 添加项目,并尝试在 C# 中获取值?
  • 是的,生硬,我用 jquery 添加并尝试用 c# 获取价值
  • 是的,生硬,我用 jquery 添加并尝试用 c# 获取价值

标签: c# jquery asp.net


【解决方案1】:

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

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

【讨论】:

  • 不,我使用 jquery 将选项添加到下拉列表中,并希望使用 c# 获取值选择选项。谢谢
  • 我明白这一点,因为您在问题中添加了jquery 标签:)。这种方法正好适合这种情况。
【解决方案2】:

后面的代码

ddl.Items[ddl.SelectedIndex].Value

【讨论】:

  • 您是否在填写 DDL 时提供了这些值?
【解决方案3】:

这取决于您如何填充 DropDownList。如果您使用的是 DataSet,您应该执行类似于此的操作:

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

希望这会有所帮助。

【讨论】:

  • 我用 jquery 添加动态选项。用 c# 获取值。
【解决方案4】:

这是我在我正在访问的网站上构建的一个小方法......希望它可以帮助某人:)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 2015-02-09
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多