关于asp.net中两个DropDownList之间数据库调用传递选项
2011-09-19 20:14
363 查看
private void fillProvince()
{
string strCnn = "data source=.;initial catalog=liaotian;integrated security=true";
SqlConnection sqlCnn = new SqlConnection(strCnn);
SqlCommand sqlCmm = new SqlCommand();
sqlCmm.Connection = sqlCnn;
sqlCmm.CommandText = "SELECT * FROM bumen1";
SqlDataReader reader = null;
try
{
sqlCnn.Open();
reader = sqlCmm.ExecuteReader();
ListItem li;
while (reader.Read())
{
li = new ListItem(reader["部门"].ToString(), reader["id"].ToString());
this.DropDownList1.Items.Add(li);
}
}
catch
{
this.Response.Write("数据库错误");
}
finally
{
reader.Close();
sqlCnn.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strCnn = "data source=.;initial catalog=liaotian;integrated security=true";
SqlConnection sqlCnn = new SqlConnection(strCnn);
SqlCommand sqlCmm = new SqlCommand();
sqlCmm.Connection = sqlCnn;
sqlCmm.CommandText = "SELECT id,职位 FROM zhiwei WHERE
pid=@id";
SqlParameter param = new SqlParameter("@id", System.Data.SqlDbType.Int);
param.Value = this.DropDownList1.SelectedValue;
sqlCmm.Parameters.Add(param);
SqlDataReader reader = null;
try
{
sqlCnn.Open();
reader = sqlCmm.ExecuteReader();
this.DropDownList2.DataSource = reader;
this.DropDownList2.DataTextField = "职位";
this.DropDownList2.DataValueField = "id";
this.DropDownList2.Items.Clear();
this.DropDownList2.Items.Add(new ListItem("--请选择职位--", "0"));
this.DropDownList2.DataBind();
}
catch { throw; }
finally
{
reader.Close();
sqlCnn.Close();
}
记住加主键
{
string strCnn = "data source=.;initial catalog=liaotian;integrated security=true";
SqlConnection sqlCnn = new SqlConnection(strCnn);
SqlCommand sqlCmm = new SqlCommand();
sqlCmm.Connection = sqlCnn;
sqlCmm.CommandText = "SELECT * FROM bumen1";
SqlDataReader reader = null;
try
{
sqlCnn.Open();
reader = sqlCmm.ExecuteReader();
ListItem li;
while (reader.Read())
{
li = new ListItem(reader["部门"].ToString(), reader["id"].ToString());
this.DropDownList1.Items.Add(li);
}
}
catch
{
this.Response.Write("数据库错误");
}
finally
{
reader.Close();
sqlCnn.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strCnn = "data source=.;initial catalog=liaotian;integrated security=true";
SqlConnection sqlCnn = new SqlConnection(strCnn);
SqlCommand sqlCmm = new SqlCommand();
sqlCmm.Connection = sqlCnn;
sqlCmm.CommandText = "SELECT id,职位 FROM zhiwei WHERE
pid=@id";
SqlParameter param = new SqlParameter("@id", System.Data.SqlDbType.Int);
param.Value = this.DropDownList1.SelectedValue;
sqlCmm.Parameters.Add(param);
SqlDataReader reader = null;
try
{
sqlCnn.Open();
reader = sqlCmm.ExecuteReader();
this.DropDownList2.DataSource = reader;
this.DropDownList2.DataTextField = "职位";
this.DropDownList2.DataValueField = "id";
this.DropDownList2.Items.Clear();
this.DropDownList2.Items.Add(new ListItem("--请选择职位--", "0"));
this.DropDownList2.DataBind();
}
catch { throw; }
finally
{
reader.Close();
sqlCnn.Close();
}
记住加主键
相关文章推荐
- 在两个ASP.NET页面之间传递值
- 在两个ASP.NET页面之间传递值
- 在两个ASP.NET页面之间传递值
- 在两个ASP.NET页面之间传递值[querystring,session,server.Transfer]
- 在两个ASP.NET页面之间传递值[querystring,session,server.Transfer][转]
- 在两个ASP.NET页面之间传递值
- [转]在两个ASP.NET页面之间传递值
- 在两个ASP.NET页面之间传递值
- ASP.NET两个页面之间传递值的几种方式
- 在两个ASP.NET页面之间传递变量【转】
- 在两个ASP.NET页面之间传递值
- 在两个ASP.NET页面之间传递值
- 在两个ASP.NET页面之间传递值[转]
- [网络收集]在两个ASP.NET页面之间传递变量 (QueryString,Session和Cookies Application)
- 在两个ASP.NET页面之间传递值
- 在两个ASP.NET页面之间传递值
- 在两个ASP.NET页面之间传递值
- 在两个ASP.NET页面之间传递值
- 关于ASP.NET同页面内【用户控件与父页面】以及【用户控件与用户控件】之间方法调用/传值 一点点经验
- 在两个ASP.NET页面之间传递变量