.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>