Figure 2 A Menu User Control
<%@ Control Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
// Set the current selection of list
String selectionId = Request.Params["selection"];
if (selectionId != null)
{
MyList.SelectedIndex = Int32.Parse(selectionId);
}
// Obtain list of menu categories and databind to list control
SqlConnection conn = new SqlConnection("Server=(local);
User Id=sa;Password=!nt3ns!ty;Initial Catalog=Northwind;");
SqlCommand cmd = new SqlCommand(
"SELECT * FROM Categories", conn);
// Open connection to the database
conn.Open();
// Retrieve data and bind to DataList
MyList.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection);
MyList.DataBind();
}
</script>
<table width="100%">
<tr>
<td>
<asp:DataList id="MyList" runat="server"
RepeatDirection="Horizontal" RepeatColumns="8"
cellpadding="3" cellspacing="0">
<ItemTemplate>
<asp:HyperLink cssclass="MenuUnselected" id="HyperLink1"
Text='<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>'
NavigateUrl='<%# "DefaultNew.aspx?CategoryID=" +
DataBinder.Eval(Container.DataItem, "CategoryID") +
"&selection=" + Container.ItemIndex %>' runat="server" />
</ItemTemplate>
<SelectedItemTemplate>
<asp:HyperLink cssclass="MenuSelected" id="HyperLink2"
Text='<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>'
NavigateUrl='<%# "DefaultNew.aspx?CategoryID=" +
DataBinder.Eval(Container.DataItem, "CategoryID") +
"&selection=" + Container.ItemIndex %>' runat="server" />
</SelectedItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
Figure 3 Placing a Menu User Control on a Web Form
<%@ Page Language="C#" Debug="true" %>
<%@ Register TagPrefix="Widgets" TagName="Menu" Src="_MenuNew.ascx" %>
<html> <body>
<table cellspacing="0" cellpadding="0" width="100%" border="0">
<tr>
<td>
<Widgets:Menu id="Menu1" runat="server" />
</td>
</tr>
<tr>
<td>
<!-- Place page content here -->
Welcome to Widgets online!
<!-- End page content -->
</td>
</tr>
</table></body>
</html>
Figure 4 Using the CustomValidator Control with a Web Service
<%@ Page Language="VB" Debug=true %>
<%@ Import Namespace="ZipCodeResolver" %>
<html>
<head>
<script runat=server>
Sub ValidateBtn_OnClick( sender As object, e As EventArgs )
If Page.IsPostBack then
If Page.IsValid Then
lblOutput.Text = "Address validation succeeded."
Else
lblOutput.Text = "<font color=red>Address validation " +
"failed! Please fill out all required fields and " +
"make sure you have entered " + _
"your correct zip code.</font>"
End If
End If
End Sub
Public Sub ServerValidation( source As object, _
args As ServerValidateEventArgs )
Dim sResults as String
'Get form values
Dim sAddress as String = txtAddress1.Text
Dim sCity as String = txtCity.Text
Dim sState as String = txtState.Text
Dim sZip as String = txtZip.Text
'Create an instance of the ZipCodeResolver object
Dim zipChecker as New ZipCodeResolver()
'Call the remote method (Eraserver web service)
sResults = zipChecker.ShortZipCode( 9999, _
sAddress, sCity, sState )
'Validation succeeds if the zip returned from the Web service
'matches the zip code given by the user.
If sResults = sZip then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub
</script>
</head>
<body>
<form runat="server">
<asp:Label id=lblOutput runat="server"
Text="Please enter your ship-to address:"
Font-Name="Verdana"
Font-Size="10pt" />
<p>
<table>
<tr>
<td>Name: </td>
<td><asp:TextBox id="txtName" runat="server"/></td>
<td><asp:RequiredFieldValidator runat="server"
ErrorMessage="Name field cannot be left blank"
InitialValue=" "
ControlToValidate="txtName"
Display="Dynamic">
<font color=red>*</font>
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Address1:</td>
<td><asp:TextBox id="txtAddress1" runat="server"/><br></td>
<td><asp:RequiredFieldValidator runat="server"
ControlToValidate="txtAddress1"
Display="Static">
<font color=red>*</font>
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Address2:</td>
<td><asp:TextBox id="txtAddress2" runat="server"/><br></td>
<td></td>
</tr>
<tr>
<td>City:</td>
<td><asp:TextBox id="txtCity" runat="server"/><br></td>
<td><asp:RequiredFieldValidator runat="server"
ControlToValidate="txtCity"
Display="Static">
<font color=red>*</font>
</asp:RequiredFieldValidator>
</td>
</tr>
<tr><td>State:</td>
<td><asp:TextBox id="txtState" runat="server"/><br></td>
<td><asp:RequiredFieldValidator runat="server"
ControlToValidate="txtState"
Display="Static">
<font color=red>*</font>
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Zip Code(5 digit):</td>
<td><asp:TextBox id="txtZip" maxlength=5
runat="server"/><br></td>
<td><asp:RequiredFieldValidator runat="server"
ControlToValidate="txtZip"
Display="Static">
<font color=red>*</font>
</asp:RequiredFieldValidator>
</td>
</tr>
</table>
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="txtZip"
OnServerValidate="ServerValidation"
Display="Static"
ErrorMessage="Your shipping address appears invalid!<br>
Please recheck all fields and make sure that..."
Font-Color="red"
runat="server"/><br>
<asp:Button id="Button1"
Text="Validate"
OnClick="ValidateBtn_OnClick"
runat="server"/><br>
</form>
</body>
</html>
Figure 9 Using Bound and Template Columns
<asp:DataGrid runat="server" id="MyList" AutoGenerateColumns="true">
<Columns>
<asp:BoundColumn HeaderText="Product Name" DataField="ModelName" />
<asp:BoundColumn HeaderText="Model" DataField="ModelNumber" />
<asp:TemplateColumn HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox runat="server" id="Quantity"
Text='<%# DataBinder.Eval(Container.DataItem, "Quantity") %>'
width="40px" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
Figure 10 Caching a DataSet
public static void GetCategoryData()
{
// if the DataSet exists in the cache
if( Page.Cache["Categories"]!= null )
//Return the cached DataSet
return (DataSet)Page.Cache["Categories"];
// otherwise, retrieve DataSet from database
else
{
//Create ADO.NET connection and command objects
SqlConnection conn = new SqlConnection(
ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand cmd = new SqlCommand("SELECT * FROM Categories",
conn);
//Create DataAdapter and DataSet objects
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
//Open connection to database
conn.Open();
//Fill the DataSet
myAdapter.Fill(ds, "AllDomains");
//Close connection to database
conn.Close();
//Place the DataSet in the cache
context.Cache["AllDomains"] = ds;
//Return the DataSet
return ds;
}
}
|