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;namespace WebApplicationCSharp { /// <summary> /// VC82 的摘要描述。 /// </summary> public class VC82 : System.Web.UI.Page { protected System.Web.UI.WebControls.DataList EmployeeDataList;
private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { // 只在网页第一次执行时处理,在此之後,清单值 // 会一直保留在控件的查看状态中。 SqlConnection MyConnection = new SqlConnection("server=(local)\\NetSDK;database=北风贸易;Trusted_Connection=yes"); SqlCommand MyCommand = new SqlCommand("SELECT 身份证号码,姓名,目前薪资,部门 FROM 章立民工作室 WHERE 部门 = '业务部'", MyConnection); MyConnection.Open();
SqlDataReader dr = MyCommand.ExecuteReader(); EmployeeDataList.DataSource = dr; EmployeeDataList.DataBind(); dr.Close(); MyConnection.Close(); } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: 此呼叫为 ASP.NET Web Form 设计工具的必要项。 // InitializeComponent(); base.OnInit(e); }
将 SQL 数据绑定到 DataList 控件请参见 通过 ASP.NET 访问数据 | 通过 ADO.NET 访问数据 | System.Web.UI.WebControls 命名空间 | DataList 控件 语言 C#Visual Basic全部显示 Repeater 控件是通用的迭代程序,而 DataList 控件则提供专门用于控制列表布局的附加功能。与 Repeater 不同,DataList 呈现其模板定义元素周围的表行和单元格,从而提供了更为丰富的布局和格式设置功能。例如,DataList 支持 RepeatColumns 和 RepeatDirection 属性,这两项属性分别指定列数和数据项呈现方向(垂直或水平)。DataList 还支持样式特性,如字体大小和字体名称。以下示例显示如何访问一个包含书名及其他信息的 SQL 数据库,并使用 DataList 控件来显示相关数据。若要查看所执行的类似示例,请运行 ASP.NET 快速入门中的 DataList2.aspx 示例。[Visual Basic] <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <html> <script language="VB" runat="server"> Sub Page_Load(sender As Object, e As EventArgs) Dim myConnection As SqlConnection Dim myCommand As SqlDataAdapter ' Create a connection to the "pubs" SQL database located on the ' local computer. myConnection = New SqlConnection("server=localhost;" _ & "database=pubs;Trusted_Connection=Yes") ' Connect to the SQL database using a SQL SELECT query to get all ' the data from the "Titles" table. myCommand = New SqlDataAdapter("SELECT * FROM Titles", myConnection) ' Create and fill a DataSet. Dim ds As DataSet = new DataSet() myCommand.Fill(ds) ' Bind MyDataList to the DataSet. MyDataList is the ID for ' the DataList control in the HTML section of the page. MyDataList.DataSource = ds MyDataList.DataBind() End Sub </script><%-- Display the data. -%> <body> <%-- Open the DataList control and set it for two columns, to be filled in horizontal order. --%> <ASP:DataList id="MyDataList" RepeatColumns="2" RepeatDirection="Horizontal" runat="server"> <ItemTemplate> <div style="padding:15,15,15,15;font-size:10pt;font-family:Verdana"> <div style="font:12pt verdana;color:darkred"> <i><b><%# DataBinder.Eval(Container.DataItem, "title")%> </i></b> </div> <br> <b>Title ID: </b> <%# DataBinder.Eval(Container.DataItem, "title_id") %><br> <b>Category: </b> <%# DataBinder.Eval(Container.DataItem, "type")%><br> <b>Publisher ID: </b> <%# DataBinder.Eval(Container.DataItem, "pub_id") %><br> <b>Price: </b> <%# DataBinder.Eval(Container.DataItem, "price", "{0:c}") %> <p> </div> </ItemTemplate> </ASP:DataList> </body> </html>[C#] <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <html> <script language="C#" runat="server"> void Page_Load(Object sender, EventArgs e) { // Create a connection to the "pubs" SQL database located on the // local computer. SqlConnection myConnection = new SqlConnection("server=localhost;" + "database=pubs;Trusted_Connection=Yes"); // Connect to the SQL database using a SQL SELECT query to get all // the data from the "Titles" table. SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * " + " from Titles", myConnection); // Create and fill a DataSet. DataSet ds = new DataSet(); myCommand.Fill(ds); // Bind MyDataList to the DataSet. MyDataList is the ID for // the DataList control in the HTML section of the page. MyDataList.DataSource = ds; MyDataList.DataBind(); } </script><%-- Display the data. -%> <body> <%-- Open the DataList control and set it for two columns, to be filled in horizontal order. --%> <ASP:DataList id="MyDataList" RepeatColumns="2" RepeatDirection= "Horizontal" runat="server"> <%-- Create a DataList control template named "ItemTemplate". --%> <ItemTemplate> <div style="padding:15,15,15,15;font-size:10pt; font-family:Verdana"> <div style="font:12pt verdana;color:darkred"> <i><b><%# DataBinder.Eval(Container.DataItem, "title")%> </i></b> </div> <br> <b>Title ID: </b> <%# DataBinder.Eval(Container.DataItem, "title_id") %><br> <b>Category: </b> <%# DataBinder.Eval(Container.DataItem, "type")%><br> <b>Publisher ID: </b> <%# DataBinder.Eval(Container.DataItem, "pub_id") %><br> <b>Price: </b> <%# DataBinder.Eval(Container.DataItem,"price", "{0:c}") %> <p> </div> </ItemTemplate> </ASP:DataList> </body> </html>请参见
欢迎使用AspNetPager免费分页控件:http://www.webdiyer.com
顺便赋上一段代码,供你参考
<%@ Page language="c#" Codebehind="VC82.aspx.cs" AutoEventWireup="false" Inherits="WebApplicationCSharp.VC82" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataList 服务器控件使用示范</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="VC82" method="post" runat="server">
<asp:DataList id="EmployeeDataList" style="Z-INDEX: 101; LEFT: 12px; POSITION: absolute; TOP: 12px" runat="server" Width="409px">
<HeaderTemplate>
<TABLE id="Table7" cellSpacing="1" cellPadding="1" width="460" border="1">
<TR>
<TD width="130">身份证号码</TD>
<TD width="100">姓名</TD>
<TD width="130">目前薪资</TD>
<TD width="100">部门</TD>
</TR>
</TABLE>
</HeaderTemplate>
<ItemTemplate>
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="460" border="1">
<TR>
<TD width="130"><%# DataBinder.Eval(Container.DataItem, "身份证号码") %></TD>
<TD width="100"><%# DataBinder.Eval(Container.DataItem, "姓名") %></TD>
<TD width="130"><%# DataBinder.Eval(Container.DataItem, "目前薪资","{0:c}") %></TD>
<TD width="100"><%# DataBinder.Eval(Container.DataItem, "部门") %></TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:DataList>
</form>
</body>
</HTML>
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;namespace WebApplicationCSharp
{
/// <summary>
/// VC82 的摘要描述。
/// </summary>
public class VC82 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList EmployeeDataList;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
// 只在网页第一次执行时处理,在此之後,清单值
// 会一直保留在控件的查看状态中。
SqlConnection MyConnection = new SqlConnection("server=(local)\\NetSDK;database=北风贸易;Trusted_Connection=yes");
SqlCommand MyCommand = new SqlCommand("SELECT 身份证号码,姓名,目前薪资,部门 FROM 章立民工作室 WHERE 部门 = '业务部'", MyConnection);
MyConnection.Open();
SqlDataReader dr = MyCommand.ExecuteReader();
EmployeeDataList.DataSource = dr;
EmployeeDataList.DataBind(); dr.Close();
MyConnection.Close();
}
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 此呼叫为 ASP.NET Web Form 设计工具的必要项。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 此为设计工具支援所必需的方法 - 请勿使用程式码编辑器修改
/// 这个方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
string sMainProjectID;
string sTsbh;
string sSubProjectName;
string sSubProID;
string sSpecialty;
string sSpecialtyID;
DateTime now = DateTime.Now;
//信息数据结构表
//vCommandType, string vbsbh,string vdxgcbh,string vzymcid,string vzymc,string vzyryid,
//string vzyrymc,string vzgcbh,string vRegeditMan ,string vRegeditDate
DataTable oDataTable = new DataTable("SelectInfor");
DataColumnCollection columns = oDataTable.Columns;
columns.Add("CommandType",typeof(System.String));
columns.Add("bsbh",typeof(System.String));
columns.Add("dxgcbh",typeof(System.String));
columns.Add("zymcid",typeof(System.String));
columns.Add("zymc",typeof(System.String));
columns.Add("zyryid",typeof(System.String));
columns.Add("zyrymc",typeof(System.String));
columns.Add("zgcbh",typeof(System.String));
columns.Add("RegeditMan",typeof(System.String));
columns.Add("RegeditDate",typeof(System.String));
columns.Add("ImprotanceLevel",typeof(System.String));
DataRow UserInfoRow = Customer.Tables[CustomerData.CUSTOMERS_TABLE].Rows[0];
string vSubCensor =UserInfoRow[CustomerData.LOGOID_FIELD].ToString();
string vSubCensorName = UserInfoRow[CustomerData.NAME_FIELD].ToString();
//循环读出选中的数据
foreach( DataListItem oDataListItem in DataList1.Items)
{
string[] oArrayLsit = new string[11];
//注意数组越界的情况
sTsbh = ((Label)oDataListItem.FindControl("tsbh")).Text;
sSubProjectName = ((Label)oDataListItem.FindControl("gcmc")).Text;
sSubProID = ((Label)oDataListItem.FindControl("dxgcbh")).Text;
sMainProjectID = zgxbh.Value; DataGrid oDataGrid = (DataGrid)oDataListItem.FindControl("projectinfo");
foreach(DataGridItem oDataGridItem in oDataGrid.Items )
{
CheckBox oCheckBox =(CheckBox)oDataGridItem.FindControl("mycheck");
DropDownList oDropDownList = (DropDownList)oDataGridItem.FindControl("mydropdownlist");
if(oCheckBox.Checked)
{
sSpecialtyID= ((Label)oDataGridItem.FindControl("zymcidColum")).Text;
sSpecialty = ((Label)oDataGridItem.FindControl("zymyColumn")).Text;
string sCareerArr = oDropDownList.SelectedValue.ToString();
string ImprotancLevel = ((DropDownList)oDataGridItem.FindControl("ImprotanceLevel")).SelectedValue.ToString();
/*表的结构
* columns.Add("CommandType",typeof(System.String));
columns.Add("bsbh",typeof(System.String));
columns.Add("dxgcbh",typeof(System.String));
columns.Add("zymcid",typeof(System.String));
columns.Add("zymc",typeof(System.String));
columns.Add("zyryid",typeof(System.String));
columns.Add("zyrymc",typeof(System.String));
columns.Add("zgcbh",typeof(System.String));
columns.Add("RegeditMan",typeof(System.String));
columns.Add("RegeditDate",typeof(System.String));
* */
string[] my= new string[2];
my = sCareerArr.Split('|');
oArrayLsit[0] = "add";
oArrayLsit[1] = sTsbh;
oArrayLsit[2] = sSubProID;
oArrayLsit[3] = sSpecialtyID;
oArrayLsit[4] = sSpecialty;
oArrayLsit[5] = my[0];
oArrayLsit[6] = my[1];
oArrayLsit[7] = sMainProjectID;
oArrayLsit[8] = vSubCensorName;
oArrayLsit[9] = now.ToString();
oArrayLsit[10] = ImprotancLevel;
oDataTable.Rows.Add(oArrayLsit);
}
}
}
bool SucceOrFalse = (new FAssignPro()).SaveInforAssinRY(oDataTable);
if(SucceOrFalse)
{
Response.Write("<script>alert('分配成功')</script>");
Response.Write("<script>window.open('addzyry.aspx','MainFrameSetContents');</script>");
} else
{
Response.Write("<script>alert('分配失败\\n原因\\n可能该单项工程已分配\\n可能该项目的状态信息找不到\\n可能数据库发生错误\\n有问题请联系管理员!\\t\\t\\t')</script>");
Button1.Attributes.Add("onclick","return confrimDelete()");
}
}
}
通过 ASP.NET 访问数据 | 通过 ADO.NET 访问数据 | System.Web.UI.WebControls 命名空间 | DataList 控件
语言
C#Visual Basic全部显示
Repeater 控件是通用的迭代程序,而 DataList 控件则提供专门用于控制列表布局的附加功能。与 Repeater 不同,DataList 呈现其模板定义元素周围的表行和单元格,从而提供了更为丰富的布局和格式设置功能。例如,DataList 支持 RepeatColumns 和 RepeatDirection 属性,这两项属性分别指定列数和数据项呈现方向(垂直或水平)。DataList 还支持样式特性,如字体大小和字体名称。以下示例显示如何访问一个包含书名及其他信息的 SQL 数据库,并使用 DataList 控件来显示相关数据。若要查看所执行的类似示例,请运行 ASP.NET 快速入门中的 DataList2.aspx 示例。[Visual Basic]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim myConnection As SqlConnection
Dim myCommand As SqlDataAdapter
' Create a connection to the "pubs" SQL database located on the
' local computer.
myConnection = New SqlConnection("server=localhost;" _
& "database=pubs;Trusted_Connection=Yes")
' Connect to the SQL database using a SQL SELECT query to get all
' the data from the "Titles" table.
myCommand = New SqlDataAdapter("SELECT * FROM Titles", myConnection)
' Create and fill a DataSet.
Dim ds As DataSet = new DataSet()
myCommand.Fill(ds)
' Bind MyDataList to the DataSet. MyDataList is the ID for
' the DataList control in the HTML section of the page.
MyDataList.DataSource = ds
MyDataList.DataBind()
End Sub
</script><%-- Display the data. -%>
<body>
<%-- Open the DataList control and set it for two columns, to be
filled in horizontal order. --%>
<ASP:DataList id="MyDataList" RepeatColumns="2"
RepeatDirection="Horizontal" runat="server">
<ItemTemplate>
<div style="padding:15,15,15,15;font-size:10pt;font-family:Verdana">
<div style="font:12pt verdana;color:darkred">
<i><b><%# DataBinder.Eval(Container.DataItem, "title")%>
</i></b>
</div>
<br>
<b>Title ID: </b>
<%# DataBinder.Eval(Container.DataItem, "title_id") %><br>
<b>Category: </b>
<%# DataBinder.Eval(Container.DataItem, "type")%><br>
<b>Publisher ID: </b>
<%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
<b>Price: </b>
<%# DataBinder.Eval(Container.DataItem, "price", "{0:c}") %>
<p>
</div>
</ItemTemplate>
</ASP:DataList>
</body>
</html>[C#]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
// Create a connection to the "pubs" SQL database located on the
// local computer.
SqlConnection myConnection = new SqlConnection("server=localhost;" +
"database=pubs;Trusted_Connection=Yes");
// Connect to the SQL database using a SQL SELECT query to get all
// the data from the "Titles" table.
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * " +
" from Titles", myConnection);
// Create and fill a DataSet.
DataSet ds = new DataSet();
myCommand.Fill(ds);
// Bind MyDataList to the DataSet. MyDataList is the ID for
// the DataList control in the HTML section of the page.
MyDataList.DataSource = ds;
MyDataList.DataBind();
}
</script><%-- Display the data. -%>
<body>
<%-- Open the DataList control and set it for two columns, to be
filled in horizontal order. --%>
<ASP:DataList id="MyDataList" RepeatColumns="2"
RepeatDirection= "Horizontal" runat="server">
<%-- Create a DataList control template named "ItemTemplate". --%>
<ItemTemplate>
<div style="padding:15,15,15,15;font-size:10pt;
font-family:Verdana">
<div style="font:12pt verdana;color:darkred">
<i><b><%# DataBinder.Eval(Container.DataItem, "title")%>
</i></b>
</div>
<br>
<b>Title ID: </b>
<%# DataBinder.Eval(Container.DataItem, "title_id") %><br>
<b>Category: </b>
<%# DataBinder.Eval(Container.DataItem, "type")%><br>
<b>Publisher ID: </b>
<%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
<b>Price: </b>
<%# DataBinder.Eval(Container.DataItem,"price", "{0:c}") %>
<p>
</div>
</ItemTemplate>
</ASP:DataList>
</body>
</html>请参见