📄 dataview-binding.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>Repeated-Value Data Binding to a DataView 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}
</style></head>
<body bgcolor="#ffffff">
<span class="heading">Repeated-Value Data Binding to a DataView 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"/>
<form runat="server">
HTML <b><select></b> elements:<br />
<select id="MySelectList" runat="server" /><p />
<b><ASP:DropDownList></b> controls:<br />
<ASP:DropDownList id="MyDropDown" runat="server" /><p />
<b><ASP:ListBox></b> controls:<br />
<ASP:ListBox id="MyASPList" runat="server" /><p />
<b><ASP:DataGrid></b> control:<br />
<ASP:DataGrid id="MyDataGrid" runat="server" /><p />
<b><ASP:Repeater></b> control:<br />
<ASP:Repeater id="MyRepeater" runat="server">
<ItemTemplate>
<div>
<%-- Note the two different methods of binding:
The first casts the DataItem to a DataRowView
The second is late-bound, using the DataBinder to evaluate the type
The second method is marginally slower, but doesn't require explicit
knowledge of the underlying data container
--%>
<b><%# ((DataRowView)Container.DataItem)["Title"]%></b><br />
ISBN: <%# DataBinder.Eval(Container.DataItem, "ISBN") %>
Published: <%# DataBinder.Eval(Container.DataItem, "PublicationDate", "{0:D}") %>
</div>
</ItemTemplate>
</ASP:Repeater><p />
<b><ASP:DataList></b> control:<br />
<ASP:DataList id="MyDataList" runat="server">
<ItemTemplate>
<%-- Note the two different methods of binding:
The first casts the DataItem to a DataRowView
The second is late-bound, using the DataBinder to evaluate the type
The second method is marginally slower, but doesn't require explicit
knowledge of the underlying data container
--%>
<b><%# ((DataRowView)Container.DataItem)["Title"] %></b><br />
ISBN: <%# DataBinder.Eval(Container.DataItem, "ISBN") %>
Published: <%# DataBinder.Eval(Container.DataItem, "PublicationDate", "{0:D}") %>
</ItemTemplate>
</ASP:DataList><p />
<b><ASP:CheckBoxList></b> control:<br />
<ASP:CheckBoxList id="MyCheckList" runat="server" /><p />
<b><ASP:RadioButtonList></b> control:<br />
<ASP:RadioButtonList id="MyRadioList" runat="server" /><p />
</form>
<!--------------------------------------------------------------------------->
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
// 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 '%18610029%'";
// create a variable to hold an instance of a DataView object
DataView objDataView;
// get DataView from get-dataview-control.ascx user control
objDataView = ctlDataView.GetDataView(strConnect, strSelect);
if (objDataView == null)
return;
// set the DataSource property of the controls
// <select> list displays values from the Title column
// and uses the ISBN as the <option> values
MySelectList.DataSource = objDataView;
MySelectList.DataValueField = "ISBN";
MySelectList.DataTextField = "Title";
// do same with ASP: list controls
MyDropDown.DataSource = objDataView;
MyDropDown.DataValueField = "ISBN";
MyDropDown.DataTextField = "Title";
MyASPList.DataSource = objDataView;
MyASPList.DataValueField = "ISBN";
MyASPList.DataTextField = "Title";
// a DataGrid can figure out the columns in the DataView
// by itself, so we just set the DataSource property
MyDataGrid.DataSource = objDataView;
// the Repeater and DataList require <ItemTemplate> entries
// that specify the columns - this is done within the element
// definition earlier in the page
MyRepeater.DataSource = objDataView;
MyDataList.DataSource = objDataView;
// in the CheckboxList we'll display the Title and
// use the Value as the control value
MyCheckList.DataSource = objDataView;
MyCheckList.DataValueField = "ISBN";
MyCheckList.DataTextField = "Title";
// in the RadioList we'll display and format the
// Value and use the Key as the control value
MyRadioList.DataSource = objDataView;
MyRadioList.DataValueField = "ISBN";
MyRadioList.DataTextField = "PublicationDate";
MyRadioList.DataTextFormatString = "Published on {0:dddd, MMMM dd, yyyy}";
// finally, bind all the controls on the page
Page.DataBind();
}
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -