我在用户注册时将城市和城镇信息保存到用户表。我想要做的是:当用户进入用户个人资料编辑页面时,我希望有选择地看到城市和城镇的DropDownList,但同时我想为城市中的所有其他数据填写DropDownList和镇表。例如:当我注册时,我选择波特兰作为城市,我选择胡德河作为城镇。我希望这两个人成为一个选定的人,但我想在同一个下拉列表中列出所有其他城镇和城市。
这是我在用户表中获取城市和城镇信息的地方:
private void GetUserCityAndTown()
{
if(Session["userid"] != null)
{
DataRow dr = function.GetDataRow("SELECT city.name, town.name FROM kullanici LEFT JOIN city on city.city_no = user.city LEFT JOIN town on town.town_no = user.town WHERE userid=" + Session["userid"].ToString());
if (dr != null)
{
DrpDwnLstCity.Text = dr[0].ToString();
DrpDwnLstTown.Text = dr[1].ToString();
}
}
这是我从城市表中获取所有城市信息的地方:
private void GetCity()
{
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
string connectionString = ConfigurationManager.ConnectionStrings["aytasarimConnectionString"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT * FROM city", conn);
try
{
conn.Open();
reader = comm.ExecuteReader();
DrpDwnLstCity.DataSource = reader;
DrpDwnLstCity.DataValueField = "city_no";
DrpDwnLstCity.DataTextField = "name";
DrpDwnLstCity.DataBind();
reader.Close();
}
catch
{
string message = "<script>alert('Error!');</script>";
Response.Write(message);
}
}
这就是我从城镇表中获取所有城镇信息的地方:
private void GetTown()
{
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
string connectionString = ConfigurationManager.ConnectionStrings["aytasarimConnectionString"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT * FROM town WHERE city_no='" + DrpDwnLstCity.SelectedValue + "'", conn);
try
{
conn.Open();
reader = comm.ExecuteReader();
DrpDwnLstTown.DataSource = reader;
DrpDwnLstTown.DataValueField = "town_no";
DrpDwnLstTown.DataTextField = "name";
DrpDwnLstTown.DataBind();
reader.Close();
}
catch
{
string message = "<script>alert('Error!');</script>";
Response.Write(message);
}
}
protected void DrpDwnLstCity_SelectedIndexChanged(object sender, EventArgs e)
{
GetTown();
}
我在哪里犯错误?我应该使用类似的东西来选择DropDownList:
DrpDwnLstCity.Items.FindByValue(dr["city_no"].ToString()).Selected = true;