在我的网络应用程序中,当我选择一个给出错误的下拉控件时。 无法在DropDownList中选择多个项目。这是我的代码......
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
{
}
这是对国家代码的约束......
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].Rows[i]["countryid"].ToString();
if (li.Value == Session["usercountry"].ToString())
li.Selected = true;
ddlCountry.DataTextField = "countryname";
ddlCountry.DataValueField = "countryid";
ddlCountry.Items.Add(li);
}
}
catch
{
}
这是状态绑定到下拉控件..
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
{
}
答案 0 :(得分:1)
使用ddlState.ClearSelection()
方法清除默认选择。
修改强>
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
{
}