<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
Width="200" AutoPostBack="True">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" AutoPostBack="true" runat="server" Width="200"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
<asp:Label ID="lbl" runat="server" Text="转入地操作代码:"></asp:Label>
<asp:TextBox ID="tbx_code" runat="server" ReadOnly="true"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindDrop();
}
}
private void BindDrop()
{
//将数据捆绑到下拉列表中
string sqlStr = "select * from t_provincecode";
//DataTable dt = DataBase.GetTable(sqlStr);
string sql = ConfigurationManager.AppSettings["ConString"];
SqlConnection cn = new SqlConnection(sql);
cn.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlStr, cn);
DataTable dt = new DataTable("city");
sda.Fill(dt);
//DataSet ds = cn.dataset(sqlStr);
DropDownList1.DataTextField = "nameofcity"; //设置列表显示的字
DropDownList1.DataValueField = "code"; //设置列表提交后获得的字段,自己理解为隐藏绑定数据
DropDownList1.DataSource = dt; //ds.Tables[0].DefaultView;
DropDownList1.DataBind();
cn.Close();
sda.Dispose();
DropDownList1.Items.Insert(0, new ListItem("请选择市级操作单位", ""));//第一项中加入内容,重点是绑定后添加
//DropDownList2.Items.Insert(0, new ListItem("请选择县区级操作单位", ""));//第一项中加入内容,重点是绑定后添加
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
//int codeofxian = Convert.ToInt32(DropDownList2.SelectedValue);
//Response.Write("<script>alert('hello')</script>");
tbx_code.Text=DropDownList2.SelectedValue.ToString();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int code = Convert.ToInt32(DropDownList1.SelectedValue);//页面加载后DropDownList1.DataValueField隐藏绑定的数据,后边根据它查询DropDownList2要显现的数据
string sqlStr = "select * from t_citycode where citycode='" + code + "'";
string sql = ConfigurationManager.AppSettings["ConString"];
SqlConnection cn = new SqlConnection(sql);
cn.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlStr, cn);
DataTable dt = new DataTable("xianqu");
sda.Fill(dt);
//DataTable dt = DataBase.GetTable(sqlStr);
DropDownList2.Items.Clear();
DropDownList2.AppendDataBoundItems = true;
DropDownList2.Items.Insert(0, new ListItem("请选择县区级操作单位", ""));//第一项中加入内容,重点是绑定后添加
DropDownList2.DataTextField = "nameofxian"; //设置DropDownList1事件SelectedIndexChanged改变后DropDownList2列表显示的数据
DropDownList2.DataValueField = "codeofxian";//设置选中后呈现的selected.value的值,是什么
DropDownList2.DataSource = dt;//ds.Tables[0].DefaultView;
DropDownList2.DataBind();
}
前台和后台如上,想的是DropDownList2改变后,将相应的值放在tbx_code,献上这段代码的原因有3。
- 二级联动甚至是多级联动代码。
- 当时在做的时候,总是不响应selectindexchanged事件,DropDownList1的是响应的,但是DropDownList2不响应。原来是将ScriptManager和UpdatePanel分别放在两个<tr>中,后来放在一起了,尽量这么做吧。好像就响应了。。。。。
- DropDownList2的value改变后,不能再tbx_code显示数据,但是当点击别的控件(能够与服务器交流的控件,造成postback事件的),是可以显示的,这是将tbx_code放在UpdatePanel中的时候,才会发生AJAX改变,否则就只能当postback的时候,tbx_code才会发生改变了。。。。。。我晕啊