两个DropDownList,第一个从数据库中绑定全国的省市,第二个根据第一个选择的省市从数据库中动态的绑定该省市的所有地区,我想问的是,怎样才能使得第一个DropDownList选定的时候不要刷新页面?

解决方案 »

  1.   

    最简便得方法就是用Ajax.Net中的UpdatePanel
      

  2.   

    1、用Ajax
    2、使用客户端的回调。(前提是熟悉Js)
      

  3.   

    用AJAX就行了。如果想方便快捷就装一个AJAX.NET上去吧,使用里面提供的UpdatePanelhttp://www.asp.net/ajax/
      

  4.   

    dropdownlist本身就是web控件 你想用他做出联动效果 必须触发他的autopostback属性 要不你说怎么能让他动态的把值传给第二个dropdownlist
    你要是想不刷新就用HTML的 select就得了呗,那就需要用AJAX了
    给你一个AJAX的动态从库绑定HTML的select我做的例子:
    quickroomJS.js:
    --------------------------------------------------------------
    //页面加载时绑定下拉列表
    function selectBindPG()
    {
        var ds = QuickRoom.getData().value;
        //用户订单填写下拉列表
        var select = document.getElementById("roomType");    
        var optionItem = document.createElement("option");
        optionItem.text = "--------------请选择--------------";
        optionItem.value = "-1";
        select.add(optionItem);
        for(var i = 0; i < ds.Tables[0].Rows.length; i++)
        {
            var optionItem = document.createElement("option");
            optionItem.text = ds.Tables[0].Rows[i]["i_room_name"];
            optionItem.value = ds.Tables[0].Rows[i]["i_room_id"];
            select.add(optionItem);
        }
    }HTML:
    --------------------------------------------------
    <head>
    <script type="text/javascript" src="js/quickroomJS.js"></script>
    </head>
    <body onload="selectBindPG();">
    <form id="form1" runat="server">
    ...
    <select id="roomType" style="position: relative">
    </select>
    ...
    </form>C#:-->QuickRoom.aspx.cs
    ---------------------------------------------------
    protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(QuickRoom));
            if (!IsPostBack)
            { }
        }#region//得到房间类型
        [AjaxPro.AjaxMethod]
        public DataSet getData()
        {
            string connstr = "server=.;uid=sa;pwd=;database=JinBaiLiHotel";
            con = new SqlConnection(connstr);
            da = new SqlDataAdapter("select i_room_id,i_room_name from hotel_rooms", con);
            ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
        #endregionweb.config:-->配置AJAXpro.DLL
    ------------------------------------------------------
    <system.web>
        <httpHandlers>
          <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
        </httpHandlers>
    ...
    现在以上代码已经就把第一个下拉列表的值从数据库中绑定好了
    下面你就做连动就可以啦
    用第一个select下拉列表中的 onchange事件,自己写一个JS方法
    声明第二个select接受值
    取select值的方法:
    Select1.options[Select1.selectedIndex].text;
    Select1.options[Select1.selectedIndex].value;
    这些是给你的提示,你自己考虑一下吧
      

  5.   

    要用ajax不然就要刷新页面,如果你觉得麻烦的话,还不如就让它刷新页面也没什么关系
      

  6.   

    下面的是CSDN上一个帖子中的
    1.  去http://sourceforge.net/projects/magicajax下载MagicAjax的Dll了。 
    2.  将该dll加入到webForm所在项目的引用中。 
    3.  在Web.Config文件中 <configuration> 节点下添加如下子节点: 
            <configSections>
                  <section   name="magicAjax" type="MagicAjax.Configuration.MagicAjaxSectionHandler,   MagicAjax"   />
           </configSections> 
      
    4.  在Web.Config文件中 <   system.web> 节点下添加如下子节点: 
                    <httpModules>
    <add   name="MagicAjaxModule"   type="MagicAjax.MagicAjaxModule,   MagicAjax"   /> 
                  </httpModules> 
    5.  在希望采用Ajax的webForm页面HTML开头引入Ajax的注册语句: 
    <%@ Register TagPrefix="ajax" Namespace="MagicAjax.UI.Controls" Assembly="MagicAjax" %> 
      将你需要采用无刷新操作的控件包在MagicAjax的标签中,类似如下代码: 
    <ajax:AjaxPanel   ID="AjaxPanel1"   runat="Server">
        <asp:DropDownList   id="DropDownList1"   runat="server"> </asp:DropDownList>
    </ajax:AjaxPanel> 
    这样就可以了