Reputation: 1918
I have Drop Down List control and add dynamic add option into Dropdownlist but But when I get value option return string empty
please help me thanks
Upvotes: 2
Views: 12551
Reputation: 21
Here's a little method I built that worked on the site I was accessing... hopefully it will help someone :)
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;
}
Upvotes: 0
Reputation: 22448
Use following code: Request.Form[ddl.UniqueID]
The code above allows to get value of selected option.
Upvotes: 4
Reputation: 2256
It depends on how you are populating the DropDownList. If you are using a DataSet, you should be doing something similar to this:
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"));
Then you can read the selected value:
int i = int.Parse(yourDropDownList.SelectedValue);
Hope this helps.
Upvotes: 1