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

📄 datalist-load-template.aspx

📁 Professional ASP.NET source code
💻 ASPX
字号:
<%@Page Language="C#" debug="true"%>

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>

<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Loading Templates Dynamically with a DataList Control</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">Loading Templates Dynamically with a DataList Control</span><hr />
<!--------------------------------------------------------------------------->

<%-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server" />

<div id="outError" runat="server" />

<form runat="server">
  Select your Template:
  <ASP:DropDownList runat="server" id="TemplateList" AutoPostback="True">
    <ASP:ListItem Value="default" Text="Default Colors" />
    <ASP:ListItem Value="bright" Text="Bright Colors" />
    <ASP:ListItem Value="dark" Text="Dark Colors" />
    <ASP:ListItem Value="mono" Text="Black and White" />
  </ASP:DropDownList>
</form>

<ASP:DataList id="MyDataList" runat="server"
     BorderStyle="Solid"
     BorderWidth=1
     BorderColor="black"
     Font-Name="Tahoma"
     Font-Size="10pt" />

<p><ASP:Label id="lblFileNames" runat="server" /></p>


<!--------------------------------------------------------------------------->

<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 '%18610026%'";

		// create a variable to hold an instance of a DataReader object
		OleDbDataReader objDataReader;

		try
		{
			// create a new Connection object using the connection string
			OleDbConnection objConnect = new OleDbConnection(strConnect);

			// open the connection to the database
			objConnect.Open();

			// create a new Command using the connection object and select statement
			OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect);

			// execute the SQL statement against the command to get the DataReader
			objDataReader = objCommand.ExecuteReader();
		}
		catch (Exception objError)
		{
			// display error details
			outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
								+ objError.Message + "<br />" + objError.Source + "<p />";
			return;		// and stop execution
		}

		// get the partial filename of the required templates
		string strFileName;
		if (Page.IsPostBack)
			strFileName = TemplateList.SelectedItem.Value + ".ascx";
		else
			strFileName = "default.ascx";

		// create the list of template filenames
		string strHeadFile = "templates/head-" + strFileName;
		string strItemFile = "templates/item-" + strFileName;
		string strAltIFile = "templates/alt-" + strFileName;
		string strFootFile = "templates/foot-" + strFileName;

		// load the required template files
		MyDataList.HeaderTemplate = Page.LoadTemplate(strHeadFile);
		MyDataList.ItemTemplate = Page.LoadTemplate(strItemFile);
		MyDataList.AlternatingItemTemplate = Page.LoadTemplate(strAltIFile);
		MyDataList.FooterTemplate = Page.LoadTemplate(strFootFile);

		// display the file names of the current templates
		lblFileNames.Text = "<b>Loaded Templates from Disk:</b><br />"
							+ strHeadFile + "<br />" + strItemFile + "<br />"
							+ strAltIFile + "<br />" + strFootFile + "<br />";

		// set the DataSource property of the DataList
		MyDataList.DataSource = objDataReader;

		// and bind the control to the data
		MyDataList.DataBind();

	}

</script>

<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>

⌨️ 快捷键说明

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