当一个下拉框的值发生改变时另一个也发生变化
并且数据都是在数据库中
就是当一下拉框选取一个表的外键时,另一个下拉框显示主键
要求不能闪屏

解决方案 »

  1.   

    最简单的就是你直接用微软的ajax控件,UpdatePanle就可以搞定了。把两个DropDownList直接放进去,就OK了。
      

  2.   

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            </asp:DropDownList>
            <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
            </asp:DropDownList></div>
        </form>
    </body>
    </html>
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;  using System.Data.OleDb;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    if (! IsPostBack ){    //binding the dropdown list box    //test use a staring arry
        string[] s1= new string[]{"11-----","12------","13------",};    string[] s2= new string[]{"21-----","22------","23------",};    DropDownList1.DataSource = s1;
        DropDownList1.DataBind();
            //select chose
        if (ViewState["list1"] != null) {
            ListItem item =(ListItem)ViewState["list1"] ;
            foreach (ListItem it in DropDownList1.Items ){            if (item.Text == it.Text) {                it.Selected = true;
                    break;
                
                }
            
            
            }
        
        }DropDownList2.DataSource = s2;//可根据S1情况进行绑定
        DropDownList2.DataBind();
        //select chose
        if (ViewState["list2"] != null)
        {
            ListItem item = (ListItem)ViewState["list2"];
            foreach (ListItem it in DropDownList2.Items)
            {            if (item.Text == it.Text)
                {                it.Selected = true;
                    break;            }
            }    }  }    }    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["list1"] = DropDownList1.SelectedItem.Text;    }
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["list2"] = DropDownList1.SelectedItem.Text;    }
    }