⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dataset-binding.aspx

📁 Professional ASP.NET source code
💻 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-dataset-control.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Repeated-Value Data Binding to a DataSet 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 DataSet 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>&lt;select&gt;</b> elements:<br />
<select id="MySelectList" runat="server" /><p />

<b>&lt;ASP:DropDownList&gt;</b> controls:<br />
<ASP:DropDownList id="MyDropDown" runat="server" /><p />

<b>&lt;ASP:ListBox&gt;</b> controls:<br />
<ASP:ListBox id="MyASPList" runat="server" /><p />

<b>&lt;ASP:DataGrid&gt;</b> control:<br />
<ASP:DataGrid id="MyDataGrid" runat="server" /><p />

<b>&lt;ASP:Repeater&gt;</b> control:<br />
<ASP:Repeater id="MyRepeater" runat="server">
  <ItemTemplate>
    <div>
      <b><%# DataBinder.Eval(Container.DataItem, "Title") %></b><br />
      ISBN: <%# DataBinder.Eval(Container.DataItem, "ISBN") %> &nbsp;
      Published: <%# DataBinder.Eval(Container.DataItem, "PublicationDate", "{0:D}") %>
    </div>
  </ItemTemplate>
</ASP:Repeater><p />

<b>&lt;ASP:DataList&gt;</b> control:<br />
<ASP:DataList id="MyDataList" runat="server">
  <ItemTemplate>
      <b><%# DataBinder.Eval(Container.DataItem, "Title") %></b><br />
      ISBN: <%# DataBinder.Eval(Container.DataItem, "ISBN") %> &nbsp;
      Published: <%# DataBinder.Eval(Container.DataItem, "PublicationDate", "{0:D}") %>
  </ItemTemplate>
</ASP:DataList><p />

<b>&lt;ASP:CheckBoxList&gt;</b> control:<br />
<ASP:CheckBoxList id="MyCheckList" runat="server" /><p />

<b>&lt;ASP:RadioButtonList&gt;</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 strConnectString  = ctlConnectStrings.OLEDBConnectionString;

		// specify ISBN code to select some rows from the database
		string strISBN = "18610029%";

		// create a variable to hold an instance of a DataView object
		DataSet objDataSet;

		// get dataset from get-dataset-control.ascx user control
		objDataSet = ctlDataView.BooksDataSet(strConnectString, strISBN);

		if (objDataSet == 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
		// DataMember indicates which table to use from DataSet
		MySelectList.DataSource = objDataSet;
		MySelectList.DataMember = "Books";
		MySelectList.DataValueField = "ISBN";
		MySelectList.DataTextField = "Title";

		// do same with ASP: dropdown list control
		MyDropDown.DataSource = objDataSet;
		MyDropDown.DataMember = "Books";
		MyDropDown.DataValueField = "ISBN";
		MyDropDown.DataTextField = "Title";

		// in this case, set DataMember to Authors table
		// and use ISBN as value and display LastName
		MyASPList.DataSource = objDataSet;
		MyASPList.DataMember = "Authors";
		MyASPList.DataValueField = "ISBN";
		MyASPList.DataTextField = "LastName";

		// a DataGrid can figure out the columns in the DataSet
		// by itself, so we just set the DataSource and DataMember
		MyDataGrid.DataSource = objDataSet;
		MyDataGrid.DataMember = "Books";

		// the Repeater and DataList require <ItemTemplate> entries
		// that specify the columns - this is done within the element
		// definition earlier in the page
		MyRepeater.DataSource = objDataSet;
		MyRepeater.DataMember = "Books";
		MyDataList.DataSource = objDataSet;
		MyDataList.DataMember = "Books";

		// in the CheckboxList we'll display the Title and
		// use the Value as the control value
		MyCheckList.DataSource = objDataSet;
		MyCheckList.DataMember = "Books";
		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 = objDataSet;
		MyRadioList.DataMember = "Books";
		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 + -