前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Web2.aspx.cs" Inherits="Web2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>测试中……</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Web2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataView dv = new DataView(create());
dv.RowFilter = "Description in ('c1','c2')";
DropDownList1.DataSource = dv;
DropDownList1.DataTextField = "product";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
dv = new DataView(create2());
DropDownList2.DataSource = dv;
DropDownList2.DataTextField = "Product";
DropDownList2.DataValueField = "Version";
DropDownList2.DataBind();
}
protected DataTable create()
{
DataTable tblDatas = new DataTable();
tblDatas.TableName = "tb";
tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
tblDatas.Columns[0].AutoIncrement = true;
tblDatas.Columns[0].AutoIncrementSeed = 1;
tblDatas.Columns[0].AutoIncrementStep = 1;
tblDatas.Columns.Add("Product", Type.GetType("System.String"));
tblDatas.Columns.Add("Version", Type.GetType("System.String"));
tblDatas.Columns.Add("Description", Type.GetType("System.String"));
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a1", "b1", "c1" });
tblDatas.Rows.Add(new object[] { null, "a2", "b2", "c2" });
tblDatas.Rows.Add(new object[] { null, "a3", "b3", "c3" });
tblDatas.Rows.Add(new object[] { null, "a4", "b4", "c4" });
return tblDatas;
}
protected DataTable create2()
{
DataTable tblDatas = new DataTable("Datas");
DataColumn dc = null;
dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
dc.AutoIncrement = true;//自动增加
dc.AutoIncrementSeed = 1;//起始为1
dc.AutoIncrementStep = 1;//步长为1
dc.AllowDBNull = false;//
//dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
//dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
//dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
tblDatas.Columns.Add("Product", Type.GetType("System.String"));
tblDatas.Columns.Add("Version", Type.GetType("System.String"));
tblDatas.Columns.Add("Description", Type.GetType("System.String"));
DataRow newRow;
newRow = tblDatas.NewRow();
newRow["Product"] = "水果刀";
newRow["Version"] = "2.0";
newRow["Description"] = "打架专用";
tblDatas.Rows.Add(newRow);
newRow = tblDatas.NewRow();
newRow["Product"] = "折叠凳";
newRow["Version"] = "3.0";
newRow["Description"] = "行走江湖七武器之一";
tblDatas.Rows.Add(newRow);
return tblDatas;
}
}