updatepanel下dropdownlist不能响应selectindexchanged事件的原因(很傻比)

 <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。

  1. 二级联动甚至是多级联动代码。
  2. 当时在做的时候,总是不响应selectindexchanged事件,DropDownList1的是响应的,但是DropDownList2不响应。原来是将ScriptManager和UpdatePanel分别放在两个<tr>中,后来放在一起了,尽量这么做吧。好像就响应了。。。。。
  3. DropDownList2的value改变后,不能再tbx_code显示数据,但是当点击别的控件(能够与服务器交流的控件,造成postback事件的),是可以显示的,这是将tbx_code放在UpdatePanel中的时候,才会发生AJAX改变,否则就只能当postback的时候,tbx_code才会发生改变了。。。。。。我晕啊
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值