itemdatabound.aspx

来自「asp.net技术内幕的书配源码」· ASPX 代码 · 共 57 行

ASPX
57
字号
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat=server>

void Page_Load(Object sender , EventArgs e) 
{
	SqlConnection conPubs;
	SqlCommand cmdSelect;
	SqlDataReader dtrAuthors;

	// Retrieve records from database
	conPubs = new SqlConnection( @"Server=localhost;Integrated Security=SSPI; Database=Pubs" );
	cmdSelect = new SqlCommand( "Select * From Authors", conPubs );
	conPubs.Open();
	dtrAuthors = cmdSelect.ExecuteReader();

	// Bind to Repeater
	rptAuthors.DataSource = dtrAuthors;
	rptAuthors.DataBind();

	dtrAuthors.Close();
	conPubs.Close();
}


void Repeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
	if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
	{
		Label lblAuthor = (Label)e.Item.FindControl("lblAuthor");
		lblAuthor.Text = (string)DataBinder.Eval( e.Item.DataItem, "au_lname" );
	}
}

</Script>

<html>
<head><title>Repeater.aspx</title></head>
<body>
<form Runat="Server">

<asp:Repeater
  ID="rptAuthors"
  OnItemDataBound="Repeater_ItemDataBound"
  Runat="Server">

  <ItemTemplate>
    <asp:Label id="lblAuthor" Runat="Server" />
  </ItemTemplate>

</asp:Repeater>

</form>
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?