這是一個簡單的例子,在NorthWind數據庫中建立了兩張表,在後台用到了數據訪問層,所以如果要測試,必須根據存儲過程寫一個方法去綁定DropDownList.
界面如下:
1.表的創建:
if exists (select * from dbo.sysobjects where id = object_id(N''[dbo].[Type]'') and OBJECTPROPERTY(id, N''IsUserTable'') = 1)
drop table [dbo].[Type]
GO
CREATE TABLE [dbo].[Type] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[TypeName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
if exists (select * from dbo.sysobjects where id = object_id(N''[dbo].[Info]'') and OBJECTPROPERTY(id, N''IsUserTable'') = 1)
drop table [dbo].[Info]
GO
CREATE TABLE [dbo].[Info] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[TypeID] [int] NULL ,
[Info] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
創建表之後,可以在裡面添加一些記錄,注意Info表裡的TypeID是Type表的外鍵,必須注意
2.用了數據訪問層,用了存儲過程,
如下:
(1).SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
ALTER PROC GetType
AS
--得到類型
SELECT * FROM TYPE
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
(2).SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
ALTER PROC GetInfoByTypeID
@TYPEID INT
AS
--得到類型的信息
SELECT ID,INFO FROM INFO WHERE TYPEID=@TYPEID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
前台:
<%@ Page language="c#" Codebehind="DropDownListRefur.ASPx.cs" AutoEventWireup="false" Inherits="NetTest.DropDownListRefur" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD Html 4.0 Transitional//EN" >
<Html>
<HEAD>
<title>DropDownListRefur</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">
<link href="Css/BasicLayout.css" rel="stylesheet" type="text/CSS">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<SCRIPT LANGUAGE="JavaScript">
<!--
//以XML求取數據
function XMLPost(obj)
{
var svalue= obj.value;
//alert(svalue);
if(svalue=="請選擇")
{
document.all("ddlInfo").length=0;
}
else
{
var webFileUrl = "DropDownListRefur.ASPx?TypeID=" + svalue;
var result = "";
var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
xmlHttp.open("POST", webFileUrl, false);//使用XMLPOST方式
XMLHttp.send("");//發送
result = XMLHttp.responseText;//得到返回的結果
if(result != "")
{
document.all("ddlInfo").length=0;//清空原來存在的
var piArray = result.split(",");
for(var i=0;i<piArray.length;i++)
{
var ary1 = piArray[i].toString().split("|");
//alert(ary1[0].toString());
//alert(ary1[1].toString());
document.all("ddlInfo").options.add(new Option(ary1[0].toString(),ary1[1].toString()));
}
}
else
{
alert(result);
}
}
}
//-->
</SCRIPT>
<table align="center" border="0" width="40%">
<tr>
; <td align="right">
<asp:DropDownList id="ddlType" runat="server" Width="124px"></ASP:DropDownList></td>
<td align="left">
<asp:DropDownList id="ddlInfo" runat="server" Width="123px"></ASP:DropDownList></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button id="btnSubmit" runat="server" Text="顯示ddlInfo選中的編號" CssClass="redButtonCSS"></ASP:Button></td>
</tr>
</table>
</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 DataBaseClass;
namespace NetTest
{
/// <summary>
/// DropDownListRefur 的摘要說明。
/// </summary>
public class DropDownListRefur : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList ddlType;
protected System.Web.UI.WebControls.DropDownList ddlInfo;
protected System.Web.UI.WebControls.Button btnSubmit;
private ClassDataGrid CDG=new ClassDataGrid();
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
RefurData();
}
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.Net Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void RefurData()
{
string TypeID =Request.QueryString["TypeID"];//第一次請求結果為null
if(TypeID + "Flag" != "Flag")//如果加Flag不等於Flag,表示ddlType改變過,觸發過onchange事件
{
DDlInfoBind(TypeID);
}
if(!this.IsPostBack)
{
DDlTypeBind();
}
}
//返回ddlInfo下拉框需要的值給XMLhttp
private void DDlInfoBind(string TID)
{
string Mystr = "";
int TypeID=int.Parse(TID);
SqlDataReader dr=CDG.GetTypeInfo(TypeID);//此處用了數據訪問層的代碼,如要運行,必須自己寫方法
DataTable MyTab =Tools.ConvertDataReaderToDataTable(dr);
if(MyTab.Rows.Count != 0)//判斷是否有記錄
{
for(int i=0;i<MyTab.Rows.Count;i++)//循環DataTable裡的記錄
{
Mystr += "," + MyTab.Rows[i][1].ToString() + "|" + MyTab.Rows[i][0].ToString();
/// <summary>
/// 綁定第一個下拉框
/// </summary>
private void DDlTypeBind()
{
ddlType.DataSource=CDG.GetMType();//此處用了數據訪問層的代碼,如要運行,必須自己寫方法
ddlType.DataTextFIEld="TypeName";
ddlType.DataValueFIEld="ID";
ddlType.DataBind();
ddlType.Items.Insert(0,"請選擇");
ddlType.Items[0].Selected=true;
ddlType.Attributes.Add("onchange","XMLPost(this);");
}
private void btnSubmit_Click(object sender, System.EventArgs e)
{
try
{
int ID=int.Parse(Request.Form["ddlInfo"].ToString());//只是在服務器端得到ddlInfo選中的對應的編號
Response.Write("<script>alert(''"+ID+"'')</script>");
DDlTypeBind();
}
catch
{}
}
}
}