📄 datalist-bind-events.aspx
字号:
<%@Page Language="C#"%>
<%@Import Namespace="System.Data" %>
<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
<%@ Register TagPrefix="wrox" TagName="getdataview" Src="..\global\get-dataview-control.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Handling Data Binding Events in a DataList Object</title>
<style type="text/css">
body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input {font-family:Tahoma,Arial,sans-serif; font-size:9pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
.rHead {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial;
font-size:14pt; font-weight:bold; padding:8px; color:green}
.rItem {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial,sans-serif;
font-size:10pt}
.rFoot {font-family:Tahoma,Arial; font-size:8pt; padding:8px; color:darkgray}
.bigRed {font-family:Arial Black,sans-serif; font-size:12pt; font-weight:normal; color:red}
</style></head>
<body bgcolor="#ffffff">
<span class="heading">Handling Data Binding Events in a DataList Object</span><hr />
<!--------------------------------------------------------------------------->
<%-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server" />
<%-- insert the control that creates the DataSet --%>
<wrox:getdataview id="ctlDataView" runat="server" />
<ASP:DataList id="MyDataList" runat="server" RepeatLayout="Table"
OnItemDataBound="CheckTitle">
<HeaderTemplate>
<div class="rHead">
Some of the Latest Wrox Press Books<br />
<img src="images/redrule.gif">
</div>
</HeaderTemplate>
<ItemTemplate>
<div class="rItem">
<b><ASP:Label id="TitleLabel" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' /></b><br />
* ISBN: <%# DataBinder.Eval(Container.DataItem, "ISBN") %>
Published: <%# DataBinder.Eval(Container.DataItem, "PublicationDate") %>
</div>
</ItemTemplate>
<FooterTemplate>
<img src="images/redrule.gif">
<div class="rFoot">
For more information visit <a href="http://www.wrox.com">http://www.wrox.com</a>
</div>
</FooterTemplate>
</ASP:DataList><p />
<!--------------------------------------------------------------------------->
<script language="c#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
if (!Page.IsPostBack) // fill the DataList from a DataView object
{
// get connection string from ..\global\connect-strings.ascx user control
string strConnect = ctlConnectStrings.OLEDBConnectionString;
// create a SQL statement to select some rows from the database
string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '%18610026%'";
// create a variable to hold an instance of a DataView object
DataView objDataView;
// get dataset from get-dataset-control.ascx user control
objDataView = ctlDataView.GetDataView(strConnect, strSelect);
if (objDataView == null)
return;
// set the DataSource property of the DataList
MyDataList.DataSource = objDataView;
// and bind the control to the data
MyDataList.DataBind();
}
}
void CheckTitle(Object objSender, DataListItemEventArgs objArgs)
{
// see what type of row (header, footer, item, etc.) caused the event
ListItemType objItemType = (ListItemType)objArgs.Item.ItemType;
// only format the results if it's an Item or AlternatingItem event
if (objItemType == ListItemType.Item || objItemType == ListItemType.AlternatingItem)
{
// objArgs.Item.DataItem returns the data for this row of items
DataRowView objRowVals = (DataRowView)objArgs.Item.DataItem;
// get the value of the Title column
string strTitle = objRowVals["Title"].ToString();
if (strTitle.IndexOf("Active Server Pages") != -1 || strTitle.IndexOf("ADO") != -1)
{
// get a reference to the "Title" ASP:Label control in this row
Label objLabel = (Label)objArgs.Item.FindControl("TitleLabel");
// add a message to this Label control
objLabel.Text += " <span class='bigRed'>Great for ASP Programmers!</span>";
}
}
}
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -