選中下拉菜單的一項後,下面的下拉菜單項隨之改變

.CS文件

 

 


 

 

 

using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace joke
{
    public partial class _Default : System.Web.UI.Page
    {
        DataSet myDS;
        protected void Page_Load(object sender, EventArgs e)
        {

            string strCon = @"Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = 'D:/Projects/joke/joke/mydb.mdb';";
            OleDbConnection myConn = new OleDbConnection(strCon);
            string strCom = " SELECT * FROM mytab";
            //創建一個 DataSet對象 
            myConn.Open();
            myDS = new DataSet();
            OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
            myCommand.Fill(myDS, "mytab");
            myConn.Close();
            if (!IsPostBack)
            {

                this.DropDownList1.SelectedIndex = 0;
                this.DropDownList2.SelectedValue = myDS.Tables["mytab"].Rows[0].ItemArray[1].ToString();

            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

            switch (this.DropDownList1.SelectedValue)
            {
                case "xiaoa":
                    this.DropDownList2.SelectedValue = myDS.Tables["mytab"].Rows[0].ItemArray[1].ToString();
                    break;
                case "xiaob":
                    this.DropDownList2.SelectedValue = myDS.Tables["mytab"].Rows[1].ItemArray[1].ToString();
                    break;
                case "xiaoc":
                    this.DropDownList2.SelectedValue = myDS.Tables["mytab"].Rows[2].ItemArray[1].ToString();
                    break;
            }
        }


    }
}

 

 

 

 


 

 

.aspx 文件

 

 

 

 

 


 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="joke._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"
            DataSourceID="AccessDataSource1" DataTextField="name" DataValueField="name"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="145px"
            AutoPostBack="True">
        </asp:DropDownList>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server"
            DataFile="~/mydb.mdb" SelectCommand="SELECT * FROM [mytab]">
        </asp:AccessDataSource>
        <br />
        <br />
        <asp:DropDownList ID="DropDownList2" runat="server"
            Height="19px" Width="145px" DataSourceID="AccessDataSource1" DataTextField="sex"
            DataValueField="sex" EnableTheming="True">
        </asp:DropDownList>
   
    </div>
    </form>
</body>
</html>

 

 

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章