想实现的功能:下拉菜单中是人名,两个文本框分别用来盛放性别、年龄,选中人名后,性别年龄随之显示出来
mydb数据库中存放mytab表,mytab表中的内容如下
| name | sex | age |
|---|---|---|
| xiaoa | man | 12 |
| xiaob | woman | 11 |
| xiaoc | man | 13 |
思路:选中下拉菜单中的人名后,性别年龄随之显示出来,说明要触发下拉菜单的DropDownList1_SelectedIndexChanged事件
数据在文本框中的显示,必然要数据库的链接。
default.aspx.cs代码如下:
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.Configuration;
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 game1
{
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/game1/game1/mydb.mdb';"; //连接字符串
OleDbConnection myConn = new OleDbConnection(strCon); //创建连接对象
string strCom = " SELECT * FROM mytab"; //命令字符串
myConn.Open(); //打开数据库
myDS = new DataSet();//创建一个 DataSet对象,用于存储连接数据库里的内容
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn); //执行 命令字符串
myCommand.Fill(myDS, "mytab"); //把表里的内容放在DataSet对象中
myConn.Close(); //关闭数据库
if (!IsPostBack) //页面第一次被访问时。不加if(!IsPostBack)的话会出现 选中xiaob,xiaoc文本框的结果迅速跳回到xiaoa的结果上去。
{
this.DropDownList1.SelectedIndex = 0; //表示下拉菜单的第一项
TextBox1.Text = myDS.Tables["mytab"].Rows[0].ItemArray[1].ToString();//'0'代表第一个
TextBox2.Text = myDS.Tables["mytab"].Rows[0].ItemArray[2].ToString();//ToString()是转化成字符串,c#中区分大小写如:Table首字符要大写
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
checked //用于检测东东~~
{
switch (this.DropDownList1.SelectedValue) //switch case 用于选择!!!
{
case "xiaoa":
TextBox1.Text = myDS.Tables["mytab"].Rows[0].ItemArray[1].ToString();
TextBox2.Text = myDS.Tables["mytab"].Rows[0].ItemArray[2].ToString();
break ;
case "xiaob":
TextBox1.Text = myDS.Tables["mytab"].Rows[1].ItemArray[1].ToString();
TextBox2.Text = myDS.Tables["mytab"].Rows[1].ItemArray[2].ToString();
break ;
case "xiaoc":
TextBox1.Text = myDS.Tables["mytab"].Rows[2].ItemArray[1].ToString();
TextBox2.Text = myDS.Tables["mytab"].Rows[2].ItemArray[2].ToString();
break ;
}
}
}
}
}
default.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="game1._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>
<style type="text/css">
.style1
{
height: 62px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="margin: 0 auto">
<table style="width: 575px; height: 320px">
<tr>
<td colspan="5" class="style1">
<asp:DropDownList ID="DropDownList1" runat="server" Width="116px"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack=true
DataSourceID="AccessDataSource1" DataTextField="name"
DataValueField="name" oninit="DropDownList1_SelectedIndexChanged">
</asp:DropDownList> //这里不能把AutoPostBack删掉!!
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/mydb.mdb" SelectCommand="SELECT * FROM [mytab]">
</asp:AccessDataSource>
//这里的代码是通过操作控件直接生成的
</td>
</tr>
<tr>
<td colspan="5">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td colspan="5">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>