(c) 2003 Visual Studio Magazine 
Fawcette Technical Publications

ASP.NET	Master Content Reuse
Listing 1	ASP.NET version 2's support for master pages lets you save reusable content in a single file. This allows you to update a Web site easily by changing the master page's contents. This code shows how the ContentPlaceholder control fits into the GolfClubShack master page.

<%@ master language="C#"%>
<%@ register tagprefix="golfclubshack" 
	tagname="Header" 
	src="UserControls/Header.ascx" %>
<%@ register tagprefix="golfclubshack" 
	tagname="Menu" 
	src="UserControls/Menu.ascx" %>
<html>
	<head runat="server">
		<title>GolfClubShack </title>
		<link type="text/css" rel="Stylesheet" 
			href="Style/Style.css" />
	</head>
	<body background="images/sitebkgrd.gif">
		<form id="frmMain" runat="server">
			<table width="100%" cellpadding="0" 
				cellspacing="0">
			<tr>
				<td colspan="2" valign="top">
					<golfclubshack:Header 
						id="Header1" 
						runat="server" />
					<img height="1" 
						src="images/1x1.gif" 
						width="145">
				</td>
			</tr>
			<tr>
				<td valign="top" width=145>
					<golfclubshack:Menu id="Menu" 
						runat="server" /> 
				</td>
				<td>
					<asp:contentplaceholder 
						id="ContentPlaceHolder1" 
						runat="server">
					</asp:contentplaceholder>
				</td>
			</tr>
			</table>
		</form>
	</body>
</html>

ASP.NET	Hook Up Data
Listing 2	You associate data-display controls, such as the new GridView control, by using an attribute named DataSourceID. When the page first loads, the data source control is invoked, and the returned data is bound to the GridView server control.

<asp:gridview id="GridView1" runat="server"
	datasourceid="SqlDataSource1">
</asp:gridview>

<asp:sqldatasource id="SqlDataSource1" 
	runat="server"
	selectcommand="SELECT Customers.* FROM 
	Customers WHERE customerID = ?"
	providername="System.Data.OleDb" 
	connectionstring="(removed for brevity)">
	<selectparameters>
		<asp:querystringparameter name="CustomerID" 
			querystringfield="CustID" 
			type="String">
		</asp:querystringparameter>
	</selectparameters>
</asp:sqldatasource>


ASP.NET	Calling All Objects
Listing 3	You can use the ObjectDataSource control to dynamically bind objects returned from methods, such as the GetProducts() method in the ProductsDB class (see Listing 4). This code calls a data-access object directly, but it could just as easily call a business object that in turn calls the data-access object. Notice the simplified Eval("field") syntax, which replaces the version 1.1 DataBinder.Eval(Container.DataItem,"field") syntax in data-binding expressions.

<a href='ProductDetails.aspx?productID=
	<%# Eval("ProductID") %>'>
	<img src='ProductImages/thumbs/
		<%# Eval("ProductImage") %>'
	width="100" height="75" border="0">
</a>

<asp:objectdatasource id=
	"odsProductsDB" runat="server" 
	typename="GolfClubShack.ProductsDB" 
		selectmethod="GetProducts">
	<SelectParameters>
		<asp:QueryStringParameter Type="Int32" 
			Name="categoryID" 
			QueryStringField="CategoryID" 
			DefaultValue="15"/>
	</SelectParameters>
</asp:objectdatasource>

[
C#	Retrieve Products
Listing 4	The ProductsDB class's GetProducts() method  retrieves GolfClubShack product data from a SQL Server 2000 database. You can call the GetProducts() method directly from an ObjectDataSource server control, which reduces greatly the amount of code you need to write in your ASP.NET Web forms.

public SqlDataReader GetProducts(int categoryID) {
	SqlConnection myConnection = 
		new SqlConnection(ConfigurationSettings.
			ConnectionStrings["ConnectionString"]);
	SqlCommand myCommand = 
		new SqlCommand("ProductsByCategory", 
			myConnection);
	myCommand.CommandType = 
		CommandType.StoredProcedure;

	SqlParameter parameterCategoryID = 
		new SqlParameter("@CategoryID", 
			SqlDbType.Int, 4);
		parameterCategoryID.Value = categoryID;
	myCommand.Parameters.Add(parameterCategoryID);

	myConnection.Open();
	SqlDataReader result = 
		myCommand.ExecuteReader(
			CommandBehavior.CloseConnection);

	return result;
}


C#	Validate Users
Listing 5	ASP.NET version 2 includes a new Membership object that allows your application to validate, create, and change users. You can write custom membership providers in more advanced scenarios to leverage other data stores.

void ctlLogin_Authenticate(object sender,	System.Web.UI.WebControls.
	AuthenticateEventArgs e) {
	if (Membership.ValidateUser(
		this.ctlLogin.UserName, 
		this.ctlLogin.Password)) { 
		e.Authenticated = true;
		FormsAuthentication.SetAuthCookie(
			this.ctlLogin.UserName, 
			this.ctlLogin.RememberMeSet);
		if (Request.QueryString["ReturnUrl"] != 
			null) {
			Response.Redirect(Request.QueryString[
				"ReturnUrl"].ToString());
		}
		else {
			Response.Redirect("Default.aspx");
		}
	}
}

ASP.NET	Configure Membership
Listing 6	ASP.NET version 2 provides default Access and Sql Server membership providers for calling membership database tables automatically. You can also write custom providers and add them to the web.config file.

<membership defaultProvider="AspNetSqlProvider2"
	userIsOnlineTimeWindow="15" >
	<providers>
		<add name="AspNetSqlProvider2"
			type="System.Web.Security.
				SqlMembershipProvider,
				System.Web, 
				Version=1.1.3300.0, 
				Culture=neutral,
			PublicKeyToken=b03f5f7f11d50a3a"
			connectionStringName=
				"AspNetConnectionString"
			enablePasswordRetrieval="true"
			enablePasswordReset="true"
			requiresQuestionAndAnswer="false"
			applicationName="/"
			requiresUniqueEmail="false"
			passwordFormat="Clear"
			description="Stores and retrieves 
				membership data from the local 
				Microsoft SQL Server database 
				file" 
		/>
	</providers>
</membership>


ASP.NET	Configure Personalization.
Listing 7	Personalization in ASP.NET version 2 provides a quick way to capture and store user preferences. You get IntelliSense immediately after saving the web.config file, because configuration information is compiled into a Profile class automatically.

<personalization enabled="true"
	defaultProvider="AspNetSqlProvider2" >
	<providers>
		<add name="AspNetSqlProvider2"
			type="System.Web.Personalization.
				SqlPersonalizationProvider, 
				System.Web, Version=1.2.3400.0, 
				Culture=neutral, 
				PublicKeyToken=b03f5f7f11d50a3a"
			connectionStringName=
				"AspNetConnectionString"
			applicationName="/"
			description="Stores and retrieves 
				personalization data from the local 
				Microsoft SQL Server database 
				file" />
	</providers>
	<profile>
		<property name="Name"/>
		<property name="FavoriteClubs" />
		<property name="MemberSince" />
		<property name="Skin" />
		<property name="ReceiveNewsletter" />
	</profile>
</personalization>

