有两个下拉框,怎么设置选择了其中一项后,影响另外一个下拉框。但是不提交到服务器那边去!
这样不会造成屏幕闪烁!

解决方案 »

  1.   

    那你得使用静态的客户端控件了,javascript,搜一下“联级菜单”。或者说用HttpXml。否则从数据库读取还是要刷页面的。
      

  2.   

    SelectedIndexChanged事件 必须是要发回服务器的前提下才能触发
      

  3.   

    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Test.WebForm1" %>
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    </HEAD>
    <BODY>
    <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(TypeId)
      {
       var drp2 = document.getElementById("DropDownList2");
       for(var i = 0;i<=drp2.options.length -1;i++)
       {
       drp2.remove(i);
       }
       var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
       var oDoc = new ActiveXObject("MSXML2.DOMDocument");
       oHttpReq.open("POST", "WebForm2.aspx?TypeId="+TypeId, false);
       oHttpReq.send("");
       result = oHttpReq.responseText;
       oDoc.loadXML(result);
       items = oDoc.selectNodes("//NewDataSet/Class");
       for (var item = items.nextNode(); item; item = items.nextNode())
       {
       var cid = item.selectSingleNode("jobId").nodeTypedValue;
       var cname = item.selectSingleNode("jobName").nodeTypedValue;
      
       //document.getElementById('dropdownlist').value
      
       var newOption = document.createElement("OPTION");
       newOption.text = cname;
       newOption.value = cid;
       drp2.options.add(newOption);
       }
      }
    </script>
    <form id="Form1" method="post" runat="server">
    <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
    <asp:DropDownList id="DropDownList2" runat="server"></asp:DropDownList><BR>
    <asp:Button id="Button1" runat="server" Text="确定>>"></asp:Button>
    </form>
    </BODY>
    </HTML>
      

  4.   

    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;
    using System.Data.SqlClient;
    using System.Xml;
    using System.Configuration;namespace Test
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DropDownList DropDownList1;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.DropDownList DropDownList2;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!this.IsPostBack)
    {
    SqlConnection Conn = new SqlConnection();
    Conn.ConnectionString = ConfigurationSettings.AppSettings["strConnection"].ToString();
    Conn.Open();
    string strSql = "select  deptId,deptName from DeptInfo ";
    DataSet ds = new DataSet();
    SqlDataAdapter Adp = new SqlDataAdapter(strSql,Conn); 
    Adp.Fill(ds,"TypeIdList");
    DropDownList1.DataSource = ds.Tables["TypeIdList"].DefaultView;
    DropDownList1.DataTextField = "deptName";
    DropDownList1.DataValueField = "deptId";
    DropDownList1.DataBind();
    ListItem pitme=new ListItem("选择类别","0");
    DropDownList1.Items.Insert(0,pitme);
    DropDownList2.Items.Insert(0,pitme);
    //this.DropDownList1.Attributes.Add("onchange","load(this.options[this.selectedIndex].innerText)");
    this.DropDownList1.Attributes.Add("onchange","load(this.options[this.selectedIndex].value)");
       }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
    Response.Write("DropDownList1:" + DropDownList1.SelectedValue.ToString() +"<br><br>");
    Response.Write("DropDownList2:" + Request["DropDownList2"].ToString());
    Response.End(); }
    }
    }
      

  5.   

    <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="Test.WebForm2" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <FONT face="宋体"></FONT>
    </form>
    </body>
    </HTML>
    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;
    using System.Data.SqlClient;
    using System.Xml;
    using System.Configuration;namespace Test
    {
    /// <summary>
    /// WebForm2 的摘要说明。
    /// </summary>
    public class WebForm2 : System.Web.UI.Page
    {
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(this.Request["TypeId"]!=null)
    {
       string TypeId = this.Request["TypeId"].ToString(); SqlConnection Conn = new SqlConnection(); Conn.ConnectionString = ConfigurationSettings.AppSettings["strConnection"].ToString();
    Conn.Open(); string strSql = "select  jobId,jobName from jobInfo where deptId="+ TypeId ;

    DataSet ds = new DataSet(); SqlDataAdapter Adp = new SqlDataAdapter(strSql,Conn);  Adp.Fill(ds,"Class"); 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();
       } } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    }
    }
      

  6.   

    数据库脚本:
    Create DataBase article
    Gouse article
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Class_Info]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[Class_Info]
    GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Type_Info]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[Type_Info]
    GOCREATE TABLE [dbo].[Class_Info] (
    [Classid] [int] identity(1,1) primary key ,
    [ClassName] [nvarchar] (50) NOT NULL ,
    [Typeid] [int] NOT NULL 
    )
    GOCREATE TABLE [dbo].[Type_Info] (
    [TypeId] [int] identity(1,1) primary key ,
    [TypeName] [nvarchar] (50) NOT NULL 

    GOinsert into [Type_Info] values('ASP技术')
    insert into [Type_Info] values('.NET技术')
    insert into [Type_Info] values('JSP技术')
    insert into [Type_Info] values('PHP技术')
    insert into [Type_Info] values('JavaScript')
    insert into [Type_Info] values('DELPHI')
    insert into [Type_Info] values('数据库')goinsert into [Class_Info](ClassName,TypeId) values('ASP基础',1)
    insert into [Class_Info](ClassName,TypeId) values('ASP技巧',1)
    insert into [Class_Info](ClassName,TypeId) values('数据库相关',1)
    insert into [Class_Info](ClassName,TypeId) values('正则表达式',1)
    insert into [Class_Info](ClassName,TypeId) values('ASP.NET',2)
    insert into [Class_Info](ClassName,TypeId) values('VB.NET',2)
    insert into [Class_Info](ClassName,TypeId) values('C#',2)
    insert into [Class_Info](ClassName,TypeId) values('JSP基础',3)
    insert into [Class_Info](ClassName,TypeId) values('JSP技巧',3)
    insert into [Class_Info](ClassName,TypeId) values('数据库相关',3)
    insert into [Class_Info](ClassName,TypeId) values('正则表达式',3)
    insert into [Class_Info](ClassName,TypeId) values('PSP基础',4)
    insert into [Class_Info](ClassName,TypeId) values('PSP技巧',4)
    insert into [Class_Info](ClassName,TypeId) values('数据库相关',4)
    insert into [Class_Info](ClassName,TypeId) values('正则表达式',4)
    insert into [Class_Info](ClassName,TypeId) values('JS基础',5)
    insert into [Class_Info](ClassName,TypeId) values('JS技巧',5)
    insert into [Class_Info](ClassName,TypeId) values('DELPHI基础',6)
    insert into [Class_Info](ClassName,TypeId) values('DELPHI基础',6)
    insert into [Class_Info](ClassName,TypeId) values('MS ACCESS',7)
    insert into [Class_Info](ClassName,TypeId) values('MS SQL',7)
    insert into [Class_Info](ClassName,TypeId) values('ORACLE',7)Go
      

  7.   

    kenMoxi(摩西)  NB           楼主  他的可以用  把分全给他吧   哈哈
      

  8.   

    TO:kenMoxi(摩西)
    我把你的代码改成了VB代码,现在有几个问题想请教一下,
    1.每次选择DropDownList1后,DropDownList2数据都是完全一样的,
    比如我在DropDownList1选择浙江省后,DropDownList2出现11个杭州市,有11条数据是正确的,但怎么都是第一条数据啊。
    2.DropDownList2并没有全部清空再添加新的条目,比如我在DropDownList1选择浙江省后再选择湖南省,DropDownList2的浙江数据并没有全部清空,就又加上了湖南的数据。
    3.在第二个页面里writer.IndentChar = ' ';我改成VB代码writer.IndentChar = chr(32) 这样正确吗
      

  9.   

    to:kenMoxi(摩西)
       在html脚本里的函数是什么意思。我怎么点了第一个下拉框之后,第二个下拉框是空白的,什么都没有,刚开始里面都有“请选择”。怎么才是把第一个选择的条目通过查询,把查询结果放到第二个下拉框中?
      

  10.   

    TO:wycaicai(wycaicai)
    第一个页面的JS脚本是把你在第一个下拉框选择的值POST到第二个页面,然后再从第二个页面把数据取回加到你二个下拉框里
      

  11.   

    t0:yanga(急雨) 
        谢谢!
        但是我进行调试的时候,看到了第一个页面把第一个下拉框选择的数据post 到了第二个页面,第二个页面也从数据库中取出了相映的数据,但是怎么我的第二个下拉框都是空白的啊?
    你那边是怎么做的呢?
        我QQ 395611645。能给我看看吗?谢谢,急!
      

  12.   

    综上所述那ASP.NET岂不是把我们代到了JAVASCRIPT年代???