DropDownList聯動的例子(c#)

來源:互聯網
上載者:User

TreeDropDownList.aspx:

---------------------------------------------------------------------------------------------------------------------------------- 

<%@ Page language="c#" Codebehind="TreeDropDownList.aspx.cs" AutoEventWireup="false" Inherits="TowDropDownList.TowDropDownList" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

 <HEAD>

  <title>TreeDropDownList</title>

  <meta content="Microsoft Visual Studio 7.0" 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 language="javascript">

  var Wantcount;

  var Groupcount;

  Groupcount=0;

  Group = new Array();

  <asp:Literal id="Want" runat="server"></asp:Literal>

  function changelocation1(locationid)

  {

   document.submit.DirectionList1.length = 0;

   document.submit.WantList1.length = 0;

   var locationid=locationid;

   var i;

   var flag;

   var j;

   for (i=0;i<Groupcount; i++)

   {

    if (Group[i][0] == locationid)

    {

     flag = true;

     for (j =0;j<document.submit.DirectionList1.length;j++)

      if (document.submit.DirectionList1[j].value == Group[i][1])

       {

        flag = false;

        break;

       }

     if (flag)

      document.submit.DirectionList1.options[document.submit.DirectionList1.length]

      = new Option(Group[i][1], Group[i][1]);

    }

   }

  }


  function changelocation11(locationid, direction)

  {

   document.submit.WantList1.length = 0;

   var locationid=locationid;

   var i;

   var flag;

   var j;

   for (i=0;i<Groupcount;i++)

   {

    if (Group[i][0] == locationid && Group[i][1] == direction)

    {

     

     flag = true;

     for (j=0;j<document.submit.WantList1.length;j++)

      if (document.submit.WantList1[j].value == Group[i][2])

      {

       flag = false;

       break;

      }

     if (flag)

      document.submit.WantList1.options[document.submit.WantList1.length]

      = new Option(Group[i][2], Group[i][2]);

    }

   }

  }

  </script>

 </HEAD>

 <BODY ms_positioning="GridLayout">

  <form id="submit" method="post" runat="server">

   <TABLE style="Z-INDEX: 101; LEFT: 184px; POSITION: absolute; TOP: 144px">

    <TR>

     <TD style="WIDTH: 115px; HEIGHT: 17px" align="middle">求職意向:</TD>

     <TD style="HEIGHT: 17px"><asp:dropdownlist id="SourceList1" runat="server" DataTextField="v_source" DataValueField="v_source" onchange="changelocation1(document.submit.SourceList1.options[document.submit.SourceList1.selectedIndex].value);changelocation11(document.submit.SourceList1.options[document.submit.SourceList1.selectedIndex].value,document.submit.DirectionList1.options[document.submit.DirectionList1.selectedIndex].value)" Width="100px"></asp:dropdownlist><asp:dropdownlist id="DirectionList1" runat="server" DataValueField="v_GroupName" onchange="changelocation11(document.submit.SourceList1.options[document.submit.SourceList1.selectedIndex].value,document.submit.DirectionList1.options[document.submit.DirectionList1.selectedIndex].value)" Width="100px"></asp:dropdownlist><asp:dropdownlist id="WantList1" runat="server" DataValueField="v_title" Width="100px"></asp:dropdownlist></TD>

    </TR>

   </TABLE>

  </form>

  <script language="javascript">

   changelocation1(document.submit.SourceList1.options[document.submit.SourceList1.selectedIndex].value);

   changelocation11(document.submit.SourceList1.options[document.submit.SourceList1.selectedIndex].value,document.submit.DirectionList1.options[document.submit.DirectionList1.selectedIndex].value)

  </script>

 

 

 


TreeDropDownList.aspx.cs:

-----------------------------------------------------------------------------------------------------------------------------------------

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

namespace TowDropDownList

{

 /// <summary>

 /// TowDropDownList 的摘要說明。

 /// </summary>

 public class TowDropDownList : System.Web.UI.Page

 {

  protected System.Web.UI.WebControls.Literal Want;

  protected System.Web.UI.WebControls.DropDownList SourceList1;

  protected System.Web.UI.WebControls.DropDownList DirectionList1;

  protected System.Web.UI.WebControls.DropDownList WantList1;

  protected System.Web.UI.WebControls.Literal Group;

  private void Page_Load(object sender, System.EventArgs e)

  {

   if (!IsPostBack)

   {

    DataSet ds = new DataSet();

    ds.ReadXml(Server.MapPath("DataSource.xml"));

    for(int i = 0;i <ds.Tables[0].Rows.Count ;i++)

    {

     DataRow dr = ds.Tables[0].Rows[i];

     Want.Text += String.Format("Group[Groupcount++] = new Array(\"{0}\",\"{1}\",\"{2}\");\n", dr["v_Source"],dr["v_GroupName"].ToString(), dr["v_title"].ToString());

     if (SourceList1.Items.FindByText(dr["v_Source"].ToString()) == null)

     {

      SourceList1.Items.Add(dr["v_Source"].ToString());

     }

    }

   }

  }

  #region Web Form Designer generated code

  override protected void OnInit(EventArgs e)

  {

   //

   // CODEGEN:該調用是 ASP.NET Web Form設計器所必需的。

   //

   InitializeComponent();

   base.OnInit(e);

  }

  

  /// <summary>

  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改

  /// 此方法的內容。

  /// </summary>

  private void InitializeComponent()

  {   

   this.Load += new System.EventHandler(this.Page_Load);

  }

  #endregion

 }

}

 


DataSource.xml:

-----------------------------------------------------------------------------------------------------------------------------

<DataSource>

<Data>

<v_Source>電腦類</v_Source>

<v_GroupName>軟體</v_GroupName>

<v_title>程式員</v_title>

</Data>

<Data>

<v_Source>電腦類</v_Source>

<v_GroupName>軟體</v_GroupName>

<v_title>系統分析員</v_title>

</Data>

<Data>

<v_Source>電腦類</v_Source>

<v_GroupName>軟體</v_GroupName>

<v_title>專案經理</v_title>

</Data>

<Data>

<v_Source>電腦類</v_Source>

<v_GroupName>硬體</v_GroupName>

<v_title>網路工程師</v_title>

</Data>

<Data>

<v_Source>電腦類</v_Source>

<v_GroupName>硬體</v_GroupName>

<v_title>電腦維護</v_title>

</Data>

<Data>

<v_Source>電腦類</v_Source>

<v_GroupName>資料庫</v_GroupName>

<v_title>資料庫開發</v_title>

</Data>

<Data>

<v_Source>電腦類</v_Source>

<v_GroupName>資料庫</v_GroupName>

<v_title>資料庫管理</v_title>

</Data>

<Data>

<v_Source>行政/管理類</v_Source>

<v_GroupName>行政</v_GroupName>

<v_title>文秘</v_title>

</Data>

<Data>

<v_Source>行政/管理類</v_Source>

<v_GroupName>行政</v_GroupName>

<v_title>助理</v_title>

</Data>

<Data>

<v_Source>行政/管理類</v_Source>

<v_GroupName>管理</v_GroupName>

<v_title>總經理</v_title>

</Data>

<Data>

<v_Source>行政/管理類</v_Source>

<v_GroupName>管理</v_GroupName>

<v_title>部門經理</v_title>

</Data>

<Data>

<v_Source>行政/管理類</v_Source>

<v_GroupName>管理</v_GroupName>

<v_title>總監</v_title>

</Data>

</DataSource>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.