我有州和城市下拉列表(级联)如下:
protected void BindStateDropDown()
{
string CS = ConfigurationManager.ConnectionStrings["SportsActiveConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from tblState", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlState.DataSource = ds;
ddlState.DataTextField = "StateName";
ddlState.DataValueField = "StateId";
ddlState.DataBind();
}
ddlState.Items.Insert(0, new ListItem("---Select---", "0"));
ddlCity.Items.Insert(0, new ListItem("---Select---", "0"));
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
int StatId = Convert.ToInt32(ddlState.SelectedValue);
string CS = ConfigurationManager.ConnectionStrings["SportsActiveConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("spCities", con);
cmd.Parameters.AddWithValue("@StatId", StatId);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlCity.DataSource = ds;
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityId";
ddlCity.DataBind();
}
ddlCity.Items.Insert(0, new ListItem("---Select---", "0"));
}
用于将值插入数据库,如下所示:
cmd.Parameters.AddWithValue("@State", ddlState.SelectedItem.Text);
cmd.Parameters.AddWithValue("@City", ddlCity.SelectedItem.Text);
这样可以正常工作并在数据库表中插入状态和城市。现在我尝试在编辑页面上显示值,如下所示:
ddlState.SelectedIndex = ddlState.Items.IndexOf(ddlState.Items.FindByText(rdr["State"].ToString()));
ddlCity.SelectedIndex = ddlCity.Items.IndexOf(ddlCity.Items.FindByText(rdr["City"].ToString()));
abpve代码绑定州但不绑定城市。城市下拉列表仍显示---选择---。一旦城市被绑定,我应该能够选择任何其他城市/州并再次更新。 任何帮助将不胜感激
答案 0 :(得分:1)
试试这个
ddlState.SelectedIndex = ddlState.Items.IndexOf(ddlState.Items.FindByText(rdr["State"].ToString()));
然后将城市值设置为隐藏值hf_city
hf_city.Value = rdr["City"].ToString();
然后关闭阅读器和连接,然后再次调用在ddlState_SelectedIndexChanged上创建的函数
int StatId = Convert.ToInt32(ddlState.SelectedValue);
string CS = ConfigurationManager.ConnectionStrings["SportsActiveConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("spCities", con);
cmd.Parameters.AddWithValue("@StatId", StatId);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlCity.DataSource = ds;
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityId";
ddlCity.DataBind();
}
ddlCity.Items.Insert(0, new ListItem("---Select---", "0"));
ddlCity.SelectedIndex = ddlCity.Items.IndexOf(ddlCity.Items.FindByText(hf_city.Value));
答案 1 :(得分:1)
创建另一种绑定cityDropdownlist的方法:
protected void BindCityDropDown(int StatId)
{
string CS = ConfigurationManager.ConnectionStrings["SportsActiveConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("spCities", con);
cmd.Parameters.AddWithValue("@StatId", StatId);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlCity.DataSource = ds;
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityId";
ddlCity.DataBind();
}
ddlCity.Items.Insert(0, new ListItem("---Select---", "0"));
}
在dropdownlistselected index change里面调用它:
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
int StatId = Convert.ToInt32(ddlState.SelectedValue);
BindCityDropdown(StatId);
}
现在在编辑部分绑定citydropdownlist然后分配值:
ddlState.SelectedIndex = ddlState.Items.IndexOf(ddlState.Items.FindByText(rdr["State"].ToString()));
int StatId = Convert.ToInt32(ddlState.SelectedValue);
BindCityDropdown(StatId);
ddlCity.SelectedIndex = ddlCity.Items.IndexOf(ddlCity.Items.FindByText(rdr["City"].ToString()));