我正在使用2个下拉列表。国家第一,国家第二。如果我从1英尺的下拉列表中选择印度,则第二个将自动从数据库中自动绑定印度的所有州。来自数据库的ASP dropdownlist数据绑定
我已经使用country_tbl
的国家,并与第一个下拉列表和india_tbl
,us_tbl
,sri_tbl
绑定绑定有关这些国家的状态。
请帮帮我。我该怎么办?
我的代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
method1();
}
}
protected void method1()
{
string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
SqlConnection con = new SqlConnection(s1);
string s2 = "select * from country";
SqlCommand cmd = new SqlCommand(s2, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "name";
DropDownList1.DataSource = dr;
DropDownList1.DataBind();
con.Close();
dr.Close();
}
protected void methodInd()
{
string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
SqlConnection con = new SqlConnection(s1);
string s2 = "select * from india";
SqlCommand cmd = new SqlCommand(s2, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "name";
DropDownList2.DataSource = dr;
DropDownList2.DataBind();
con.Close();
dr.Close();
}
protected void methodpak()
{
string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
SqlConnection con = new SqlConnection(s1);
string s2 = "select * from pakistan";
SqlCommand cmd = new SqlCommand(s2, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "name";
DropDownList2.DataSource = dr;
DropDownList2.DataBind();
con.Close();
dr.Close();
}
protected void methodsri()
{
string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
SqlConnection con = new SqlConnection(s1);
string s2 = "select * from srilanka";
SqlCommand cmd = new SqlCommand(s2, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "name";
DropDownList2.DataSource = dr;
DropDownList2.DataBind();
con.Close();
dr.Close();
}
protected void submit_Click(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text=="india")
{
methodInd();
}
else if (DropDownList1.SelectedItem.Text=="pakistan")
{
methodpak();
}
else if (DropDownList1.SelectedItem.Text=="srilanka")
{
methodsri();
}
}
有什么问题? – codingbiz 2012-07-21 12:51:17
你对每个国家都有不同的表格吗?你确定你做对了吗?您可以将所有州保存在一张表中,并且有一个CountryID列来标识“这些州属于哪个国家”。你可以像查询它'SELECT ID,状态的名称哪里COUNTRY_ID = theCountryId' – Shyju 2012-07-21 15:02:34