2011-11-23 54 views
2

我有下拉列表控制,并添加动态添加选项到DROPDOWNLIST但但当我得到价值选择返回字符串空获得值选择下拉列表选项

请帮我 感谢

+0

什么码这是失败的?我们需要更多信息 – flipchart

+1

你想在jQuery函数中或者在你的代码中获取值吗? –

+0

获得代码背后的值 – Pouya

回答

4

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

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

+0

不,我添加选项下拉列表与jquery,并希望得到值选择与C#选项。谢谢 – Pouya

+1

我知道这是因为你添加了'jquery'标签到你的问题:)。这种方法恰恰适用于这种情况。 –

1

代码背后

ddl.Items[ddl.SelectedIndex].Value 
+0

试图获取值当编写空 – Pouya

+0

你有却使DDL提供的值该代码值的回报? –

1

这取决于你如何填充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); 

希望这有助于。

+0

我添加动态选项与jquery.and获得值与C#。 – Pouya

0

这是我建立了一个工作,我在访问该网站上的一个小方法......希望这将帮助别人:)

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