2011-11-23 52 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; 
    }