<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    302班

    java突擊隊
    posts - 151, comments - 74, trackbacks - 0, articles - 14
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
     實現(xiàn)無刷新DropDownList聯(lián)動效果
    在做一個文章添加功能時,想在選擇大類后,自動將其所屬二級小類顯示出來,使用DropDownList的SelectedIndexChanged事件可以很容易實現(xiàn),但每次選擇后頁面總要刷新一次,讓人感覺很不爽。為實現(xiàn)DropDownList無刷新二級聯(lián)動,這幾天在網(wǎng)上找了些資料,但都無法達到我想要的效果,經(jīng)過反復調(diào)試,現(xiàn)已基本實現(xiàn)了此功能,現(xiàn)將代碼附下。

    一、數(shù)據(jù)庫設計:
    字段名 數(shù)據(jù)類型 說明
    ClassID 自動編號 類編號
    ClassName     varchar(8) 類名
    UpClassID int(4) 上級類編號
    ClassLevel int(4) 類級別,1為大類,2為小類

    二、設計步驟:
    1、首先,我們新建一個頁面DropTest.aspx,在其中放入兩個DropDownList控件:DropDownList1和DropDownList2,其完整代碼如下:
    <%@ Page language="c#" Codebehind="DropTest.aspx.cs" AutoEventWireup="false" Inherits="studyWEB.DropTest" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
     <HEAD>
      <title>WebForm2</title>
      <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
      <meta content="C#" name="CODE_LANGUAGE">
      <meta content="JavaScript" name="vs_defaultClientScript">
      <meta content="
    http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
      <script>
          function load(ClassID){           //ClassID為接收傳遞的大類編號
          var drp2 = document.getElementById("DropDownList2");
          function RemoveAll(oElem) {             //清除DropDownList2的所有項
          var i = 0;
          for (i = oElem.length; i >= 0; i--){
          oElem.options.remove(i);
          }
          }
          RemoveAll(drp2) 
           var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
           var oDoc = new ActiveXObject("MSXML2.DOMDocument");
           oHttpReq.open("POST", "DropChild.aspx?ClassID="+ClassID, false);         //調(diào)用讀取小類數(shù)據(jù)的頁面,將大類
                                                                                                                               // 編號值傳遞過去
           oHttpReq.send("");
           result = oHttpReq.responseText;
           oDoc.loadXML(result);
           items1 = oDoc.selectNodes("http://CLASSNAME/Table/ClassName");              //讀取所有請求大類所屬小類的類名
           items2 = oDoc.selectNodes("http://CLASSNAME/Table/ClassID");                   //讀取所有請求大類所屬小類的編號
           var itemsLength=items1.length;
           for(i=0;i<itemsLength;i++)                                                                 //將小類的類名和編號賦予DropDownList2
       {
          var newOption = document.createElement("OPTION");
          newOption.text=items1[i].text;
          newOption.value=items2[i].text;
          drp2.options.add(newOption);
       }
          }
      </script>
     </HEAD>
     <body MS_POSITIONING="flowLayout">
      <form id="Form1" method="post" runat="server">
       <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
       <asp:DropDownList id="DropDownList2" runat="server"></asp:DropDownList>
       <asp:TextBox id="TH" runat="server" BorderStyle="None" ForeColor="White" BorderColor="White"></asp:TextBox>
       <asp:Label id="Label1" runat="server"></asp:Label>
       <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
      </form>
     </body>
    </HTML>
    該頁面的后臺文件(DropDownList1.aspx.cs)中Page_Load內(nèi)的代碼如下:
    if(!this.IsPostBack)
       {
        SqlConnection con = new SqlConnection("server=localhost;database=gswebDB;uid=sa;pwd=;");
        SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from classname where ClassLevel=1",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        this.DropDownList1.DataSource=ds.Tables[0].DefaultView;
        this.DropDownList1.DataTextField = "ClassName";
        this.DropDownList1.DataValueField = "ClassID";
        this.DropDownList1.DataBind();
        this.DropDownList1.Attributes.Add("onchange","load(this.options[this.selectedIndex].value)");  //將ClassID作為參數(shù)傳遞給腳本函數(shù)load(ClassID),如果要傳遞的是ClassName,應將value改為innerText,但如果大類為中文,則調(diào)用小類時出現(xiàn)無法顯示的問題
       // this.DropDownList2.Attributes.Add("onChange","javascript:document.Form1.TH.value=this.options[this.selectedIndex].value;");   //讀取DropDownList2的值,將其賦給一個TextBox控件TH,以獲取DropDownList2的值,為獲取DropDownList2的值,網(wǎng)上有人說可通過使用隱藏的TextBox控件來獲取,我未能實現(xiàn),因為在客戶端隱藏的TextBox控件也是不可用腳本來訪問的,沒法給其賦值,我只能通過將其樣式、字體顏色設于背景相同來達到隱藏效果,這是一個很笨的方法,有誰有好的方法,請幫我。
       }
    此頁面實現(xiàn)如下功能:首先從數(shù)據(jù)庫內(nèi)讀取所有類級別為1(即大類)的類名和類編號,綁定到DropDownList1控件上;然后通過DropDownList1的Attributes屬性調(diào)用javascript函數(shù)load(ClassID);load()函數(shù)通過調(diào)用DropChild.aspx頁面,讀取XML流,得到大類所屬小類的ClassName和ClassID。
    2、新建DropChild.aspx頁面文件,其中不插入任何控件和文本,只在其后臺文件(DropChild.aspx.cs)中的Page_Load中加入以下代碼:
    if(this.Request["ClassID"]!=null)
       {
        int state = Convert.ToInt32(this.Request["ClassID"]);
        SqlConnection con = new SqlConnection("server=localhost;database=gswebDB;uid=sa;pwd=;");
        SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from classname where UpClassID='"+state+"'",con);
        DataSet ds = new DataSet("CLASSNAME");
        da.Fill(ds);
        XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
        writer.Formatting = Formatting.Indented;
        writer.Indentation = 4;
        writer.IndentChar = ' ';
        ds.WriteXml(writer);
        writer.Flush();    
        Response.End();
        writer.Close();
           該方法得到用戶選擇的大類的編號,通過查詢以后得到一個DataSet對象,使用該對象的WriteXML方法直接將內(nèi)容寫到Response.OutputStream里面然后傳遞到客戶端,客戶端的load方法通過result =oHttpReq.responseText;句話得到一個XML字符串,最后解析此串。
       
        另外,測試獲取DropDownList2值,添加了TextBox控件TH,當點擊Button時,處理事件代碼如下:
    private void Button1_Click(object sender, System.EventArgs e)
      {
       Label1.Text=TH.Text;
      }

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 激情内射亚洲一区二区三区| 亚洲av无码专区在线播放| 亚洲午夜无码久久久久软件| 亚洲美女免费视频| 久久久久亚洲精品日久生情| 97在线视频免费| 亚洲精品**中文毛片| 国产四虎免费精品视频| 亚洲国产成人九九综合| 毛片免费观看网站| 亚洲AⅤ男人的天堂在线观看 | 国产一级高清视频免费看| 色屁屁www影院免费观看视频| 亚洲AV中文无码乱人伦| 免费一区二区三区在线视频| 国产精品亚洲精品日韩已方| 国产做国产爱免费视频| 久久丫精品国产亚洲av不卡| 欧美a级成人网站免费| 日韩亚洲翔田千里在线| 国产亚洲av人片在线观看| 99热在线观看免费| 激情五月亚洲色图| www国产亚洲精品久久久| 国产中文字幕在线免费观看| 亚洲国产成人综合| 日韩激情淫片免费看| 好吊色永久免费视频大全| 亚洲一区二区电影| 免费高清在线爱做视频| 黄色视频在线免费观看| 亚洲精品国产情侣av在线| 国产成人综合久久精品免费 | 亚洲高清视频免费| 韩国二级毛片免费播放| 亚欧洲精品在线视频免费观看| 久久狠狠高潮亚洲精品| 国产一级淫片a视频免费观看| 伊人久久免费视频| 亚洲AV色无码乱码在线观看| 亚洲av中文无码乱人伦在线r▽|