我面临一个小问题,我将在下面告诉您所有细节。如果你帮助我,我会非常高兴。
我的数据库中有3个表作为" tbl_User"," tbl_City"," tbl_Town"。
我的" tbl_User"表:
我的" tbl_City"表:
我的" tbl_Town"表:
如您所见," tbl_City"和" tbl_Town"表彼此相关。这意味着每个城市都有城镇。
我的代码如下:
Fonksiyon function = new Fonksiyon();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetCity();
GetTown();
GetCityAndTownSelectively();
}
}
private void GetCityAndTownSelectively()
{
if (Session["userid"] != null)
{
DataRow dr = function.GetDataRow("SELECT tbl_City.cityno, tbl_City.cityname, tbl_Town.townno, tbl_Town.townname FROM tbl_User LEFT JOIN tbl_City on tbl_City.cityno = tbl_User.city LEFT JOIN tbl_Town on tbl_Town.townno = tbl_User.town WHERE userid=" + Session["userid"].ToString());
if (dr == null)
{
Response.Redirect("default.aspx");
}
else
{
DropDownListCity.ClearSelection();
DropDownListCity.Items.FindByValue(dr[0].ToString()).Selected = true;
DropDownListTown.ClearSelection();
DropDownListTown.Items.FindByValue(dr[2].ToString()).Selected = true;
}
}
else
{
Response.Redirect("default.aspx");
}
}
private void GetCity()
{
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
string connectionString = ConfigurationManager.ConnectionStrings["aytasarimConnectionString"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT * FROM tbl_City", conn);
try
{
conn.Open();
reader = comm.ExecuteReader();
DropDownListCity.DataSource = reader;
DropDownListCity.DataValueField = "cityno";
DropDownListCity.DataTextField = "cityname";
DropDownListCity.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 tbl_Town WHERE cityno='" + DropDownListCity.SelectedValue + "'", conn);
try
{
conn.Open();
reader = comm.ExecuteReader();
DropDownListTown.DataSource = reader;
DropDownListTown.DataValueField = "townno";
DropDownListTown.DataTextField = "townname";
DropDownListTown.DataBind();
reader.Close();
}
catch
{
string message = "<script>alert('Error!');</script>";
Response.Write(mesaj);
}
}
protected void DropDownListCity_SelectedIndexChanged(object sender, EventArgs e)
{
GetTown();
}
程序在以下行中给出错误:DrpDwnLstTown.Items.FindByValue(dr[2].ToString()).Selected = true;
我想我发现错误的原因:当我改变我的GetTown方法SQL查询时这样:SELECT * FROM tbl_Town
我的代码带来了城镇有选择地但当我点击DropDownListTown时,我会看到所有城镇。问题是我只能看到与城市相连的城镇。
答案 0 :(得分:1)
这是您需要的完整代码。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetCity();
if (DropDownListCity.Items != null)
{
GetTown(Convert.ToInt32(DropDownListCity.SelectedValue.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 tbl_City order by cityName", conn);
try
{
conn.Open();
reader = comm.ExecuteReader();
DropDownListCity.DataSource = reader;
DropDownListCity.DataValueField = "cityno";
DropDownListCity.DataTextField = "cityname";
DropDownListCity.DataBind();
reader.Close();
}
catch
{
string message = "<script>alert('Error!');</script>";
Response.Write(message);
}
}
private void GetTown(Int32 selectedCityNo)
{
if (selectedCityNo == 0)
{
DropDownListTown.Visible = false;
}
else
{
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
string connectionString = ConfigurationManager.ConnectionStrings["aytasarimConnectionString"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT * FROM tbl_Town WHERE cityno='" + selectedCityNo.ToString() + "' order by townname", conn);
try
{
conn.Open();
reader = comm.ExecuteReader();
DropDownListTown.DataSource = reader;
DropDownListTown.DataValueField = "townno";
DropDownListTown.DataTextField = "townname";
DropDownListTown.DataBind();
reader.Close();
}
catch
{
string message = "<script>alert('Error!');</script>";
Response.Write(message);
}
}
}
protected void DropDownListCity_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlCity = (DropDownList)sender;
string selectedID = ddlCity.ID;
DropDownList ddlSelectedCity = (DropDownList)FindControl(selectedID);
GetTown(Convert.ToInt32(ddlSelectedCity.SelectedValue.ToString()));
}