<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>DataList ItemCreated Event Example</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server" src="fetchData_sql.cs" />
<script language="C#" runat="server">
void Page_Load ( Object src, EventArgs e ) {
if ( !IsPostBack ) {
string query = "SELECT * FROM Employees";
myDataList.DataSource = fetchData ( query );
myDataList.DataBind ( );
}
}
void showItem ( Object src, DataListItemEventArgs e ) {
if ( e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem ) {
object [ ] dataitems = ( ( DataRowView ) e.Item.DataItem ).Row.ItemArray;
msg.Text += "<br>" + dataitems [ 0 ] + " " + dataitems [ 1 ] + ", " + dataitems [ 2 ];
}
else {
msg.Text += "<br>" + e.Item.ItemType.ToString ( );
}
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>DataList ItemCreated Event Example</h2></div>
<hr size=1 width=92%>
<form runat="server">
<div align="center">
<asp:datalist id="myDataList" runat="server"
width="92%" cellpadding=5 repeatcolumns=3
headerstyle-backcolor="lightsteelblue"
headerstyle-font-bold backcolor="ghostwhite"
onItemCreated="showItem">
<headertemplate>
NorthWind Traders Employees
</headertemplate>
<itemtemplate>
<%# Eval ( "FirstName" ) %>
<%# Eval ( "LastName" ) %>
</itemtemplate>
</asp:datalist>
</div>
<p style="padding-left:10"><asp:label id="msg" runat="server" />
</form>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc -->
</body>
</html>