Getting erro in Dropdown control selectedindexchanged event?
in my web application when i select a dropdown control which is giving error like. Cannot have multiple items selected in a DropDownList. This is my code...
try
{
ddlState.Items.Clear();
ddlState.Enabled = true;
ListItem li1 = new ListItem();
li1.Value = "0";
li1.Text = "Select State";
ddlState.Items.Add(li1);
clsStates.Countryid = int.Parse(ddlCountry.SelectedValue.ToString());
DataSet ds = clsStates.selectStateBl();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
li1 = new ListItem();
li1.Text = ds.Tables[0].Rows[i]["statename"].ToString();
li1.Value = ds.Tables[0].Rows[i]["stateid"].ToString();
ddlState.DataTextField = "statename";
ddlState.DataValueField = "stateid";
ddlState.Items.Add(li1);
}
}
catch
{
}
this is binding the countries code...
try
{
ListItem li = new ListItem();
DataSet ds = clsCountrys.selectCountryB();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
li = new ListItem();
li.Text = ds.Tables[0].Rows[i]["countryname"].ToString();
li.Value = ds.Tables[0].开发者_StackOverflow社区Rows[i]["countryid"].ToString();
if (li.Value == Session["usercountry"].ToString())
li.Selected = true;
ddlCountry.DataTextField = "countryname";
ddlCountry.DataValueField = "countryid";
ddlCountry.Items.Add(li);
}
}
catch
{
}
and this is state binding to dropdown control..
try
{
ddlState.Items.Clear();
ddlState.Enabled = true;
ddlState.ClearSelection();
ListItem li1 = new ListItem();
li1.Value = "0";
li1.Text = "Select State";
ddlState.Items.Add(li1);
clsStates.Countryid = int.Parse(ddlCountry.SelectedValue.ToString());
DataSet ds = clsStates.selectStateBl();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
li1 = new ListItem();
li1.Text = ds.Tables[0].Rows[i]["statename"].ToString();
li1.Value = ds.Tables[0].Rows[i]["stateid"].ToString();
if (Session["userstate"].ToString() == li1.Value)
li1.Selected = true;
ddlState.DataTextField = "statename";
ddlState.DataValueField = "stateid";
ddlState.Items.Add(li1);
}
}
catch
{
}
Make use of ddlState.ClearSelection()
method to clear default selection.
Check this post : ASP.NET Tips: DropDownList.ClearSelection() to avoid "Cannot have multiple items selected in DropDownList"
EDIT
try
{
ddlState.ClearSelection();
ddlState.Items.Clear();
ddlState.Enabled = true;
ListItem li1 = new ListItem();
li1.Value = "0";
li1.Text = "Select State";
ddlState.Items.Add(li1);
clsStates.Countryid = int.Parse(ddlCountry.SelectedValue.ToString());
DataSet ds = clsStates.selectStateBl();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
li1 = new ListItem();
li1.Text = ds.Tables[0].Rows[i]["statename"].ToString();
li1.Value = ds.Tables[0].Rows[i]["stateid"].ToString();
ddlState.DataTextField = "statename";
ddlState.DataValueField = "stateid";
ddlState.Items.Add(li1);
}
//code updated by pranay rana
ddlState.ClearSelection();
ListItem li = ddlState.Items.FindByValue(Session["userstate"].ToString());
if (li != null)
{
li.Selected = true;
}
//code updated by pranay rana
}
catch
{
}
精彩评论