我使用这些语法来填充两个 dropdwon 列表,第二个下拉列表必须在我选择第一个后填充,但第二个是 embty,在 mysql 表中我有两个表来填充下拉列表,第一个表包含 (gov_name) 和(gove_id) 和第二个包含 (area_name) 和 (gov_id)。
{
DataSet ds_1;
ListItem item_1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fill_gov();
}
}
private void fill_gov()
{
//Session["conection"] = "Data Source=MEDICONSULT;Initial Catalog=test1;Integrated Security=True";
Session["conection"] = "Data Source=MEDICONSULT;Initial Catalog=test1;Integrated Security=True";
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
connection.Open();
SqlCommand command = new SqlCommand();
connection = new SqlConnection((string)Session["conection"]);
connection.Open();
SqlDataAdapter da_1 = new SqlDataAdapter(command);
da_1 = new SqlDataAdapter();
command = new SqlCommand();
command.Connection = connection;
command.CommandText = "select gov_name from gov";
da_1.SelectCommand = command;
ds_1 = new DataSet();
da_1.Fill(ds_1, "gov");
Session["Hospital"] = DropDownList1.SelectedIndex;
DropDownList1.Items.Clear();
item_1 = new ListItem();
item_1.Value = "0";
item_1.Text = "select governement";
DropDownList1.Items.Add(item_1);
for (int i = 0; i <= ds_1.Tables[0].Rows.Count - 1; i++)
{
item_1 = new ListItem();
item_1.Value = (ds_1.Tables[0].Rows[i]["gov_name"].ToString());
item_1.Text = ds_1.Tables[0].Rows[i]["gov_name"].ToString();
DropDownList1.Items.Add(item_1);
}
}
private void fill_area()
{
// Session["conection"] = "Data Source=MEDICONSULT;Initial Catalog=test1;Integrated Security=True";
Session["conection"] = "Data Source=MEDICONSULT;Initial Catalog=test1;Integrated Security=True";
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
connection.Open();
SqlCommand command = new SqlCommand();
connection = new SqlConnection((string)Session["conection"]);
connection.Open();
SqlDataAdapter da_1 = new SqlDataAdapter(command);
da_1 = new SqlDataAdapter();
command = new SqlCommand();
command.Connection = connection;
command.CommandText = "select area_name from area where area_name !='' and cast (gov_id as varchar) ='" + DropDownList1.SelectedValue + "'";
da_1.SelectCommand = command;
ds_1 = new DataSet();
da_1.Fill(ds_1, "area");
Session["Hospital"] = DropDownList2.SelectedIndex;
DropDownList2.Items.Clear();
item_1 = new ListItem();
item_1.Value = "0";
item_1.Text = "select area";
DropDownList2.Items.Add(item_1);
for (int i = 0; i <= ds_1.Tables[0].Rows.Count - 1; i++)
{
item_1 = new ListItem();
item_1.Value = (ds_1.Tables[0].Rows[i]["area_name"].ToString());
item_1.Text = ds_1.Tables[0].Rows[i]["area_name"].ToString();
DropDownList2.Items.Add(item_1);
}
}