Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

Members Cross Page Values Postback and Callback in ASP.NET 2.0
Author: Unknown
Published: 7/2/2005 3:49 PM
Category: ADO.NET, ASP.NET, Authentication, Caching, Database, Datalist, Debugging, DTD, Schema, SQL, SQLXML, Validation Controls, VB.NET, VS.NET, XHTML, XML, XQuery
Summary: Members postback Techniques was one of my project that I work on in ASP.NET 2.0 course.

 

1-Callback.aspx

<%@ Page AutoEventWireup="false" ClassName="Callback_aspx" CompileWith="Callback.aspx.vb" Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Callback</title>

</head>

<body>

<form id="form1" runat="server">

<h3>Enter New Member</h3>

<table>

<tr>

<td style="width: 100px">

<asp:Label ID="Label1" Runat="server" Height="15px" Width="125px" Text="Member Name"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtMemberName" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label2" Runat="server" Height="15px" Width="125px" Text="Street Address"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtAddress" Runat="server"></asp:TextBox></td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label3" Runat="server" Height="15px" Width="125px" Text="City"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtCity" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label4" Runat="server" Height="15px" Width="125px" Text="Region/State"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtRegion" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label5" Runat="server" Height="15px" Width="125px" Text="Country"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtCountry" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px; height: 26px" align="right">

</td>

<td style="width: 100px; height: 26px" align="right">

<asp:Button ID="btnNewMember" Runat="server" Text="Submit" Height="24px" Width="81px" />

</td>

</tr>

</table>

<h3>Query Existing Members</h3>

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT [memberID], [memberName] FROM [members] UNION SELECT '', ' -- Select member --' ORDER BY [memberName]"

ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>">

</asp:SqlDataSource>

<table style="height: 48px" align="left">

<tr>

<td style="width: 286px; height: 58px;" valign="baseline" align="right">

<asp:DropDownList ID="ddlMembers" Runat="server" DataSourceID="SqlDataSource1"

DataTextField="MemberName" DataValueField="MemberID" AutoPostBack="False" Width="223px"

Height="22px" >

</asp:DropDownList>

</td>

<td style="width: 233px; height: 58px;" valign="baseline" align="left">

<asp:Label ID="lblAddress" Runat="server" Height="19px" Width="239px"></asp:Label>

</td>

</tr>

<tr>

<td style="width: 286px; height: 26px" valign="baseline" align="left">

</td>

<td style="width: 233px; height: 26px" valign="baseline" align="left">

   

<asp:Button ID="btnMembers" Runat="server" Text="Show Members" OnClick="btnMembers_Click" />

</td>

</tr>

</table><br />

 <br />

<br />

<br />

<br />

<asp:HyperLink ID="HyperLink1" Runat="server" NavigateUrl="Default.aspx">Home</asp:HyperLink>

</form>

</body>

</html>

<script language="javascript">

// This function will be called after

// the callback method on the server

// returns the members address.

function fillmemberAddress (result, context)

{

document.getElementById("lblAddress").innerHTML = result;

}

</script>

2-Callback.aspx.vb

Imports System.Data.SqlClient

Partial Class Callback_aspx

Implements System.Web.UI.ICallbackEventHandler

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Get the client-side HTML for

' for calling back to the server.

' Pass in a reference to the page,

' the script that should be added

' to generate the argument passed

' back to the server, and the

' name of the server-side method.

' (The final parameters, context and clientErrorCallback,

' aren't used in this example.)

Dim strCallBack As String = _

GetCallbackEventReference(Me, _

"this.value", _

"fillMemberAddress", Nothing, Nothing)

' Add the client-side onchange behavior

' to the drop-down list, to invoke the

' call back to the server.

ddlMembers.Attributes.Add( _

"onchange", strCallBack)

End Sub

' Implement the code that will run

' when called from the client.

Public Overridable Function RaiseCallbackEvent(ByVal argFromClient As String) _

As String Implements ICallbackEventHandler.RaiseCallbackEvent

Return GetAddress(argFromClient)

End Function

Private Function GetAddress( _

ByVal memberID As String) As String

' If passed an empty string,

' return an empty string.

If memberID = String.Empty Then

Return String.Empty

End If

Dim cnn As New SqlConnection()

Try

' Use value from config file to

' set up a database connection.

cnn.ConnectionString = _

ConfigurationSettings.ConnectionStrings("NorthwindConnection").ConnectionString

' Create a command to get

' address info for the

' specified member.

Dim commandText As String = _

"SELECT Address, City, Region, PostalCode, Country " _

& "FROM dbo.Members " _

& "WHERE memberID = @MemberID"

Dim cmd As New SqlCommand(commandText, cnn)

Dim prm As New SqlParameter( _

"memberID", Data.SqlDbType.NChar, 5)

prm.Value = memberID

cmd.Parameters.Add(prm)

' Retrieve the address info

' and format it as one

' multi-line string.

Dim rdr As SqlDataReader

cnn.Open()

rdr = cmd.ExecuteReader

If rdr.Read Then

Dim sbAddress As New StringBuilder()

If rdr("Address").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}</br>", rdr("Address").ToString)

End If

If rdr("City").ToString.Length > 0 Then

If rdr("Region").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}, {1} ", rdr("City").ToString, rdr("Region"))

Else

sbAddress.AppendFormat("{0} ", rdr("City").ToString)

End If

End If

If rdr("PostalCode").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}</br>", rdr("PostalCode").ToString)

End If

sbAddress.Append(rdr("Country").ToString)

Return sbAddress.ToString

Else 'no rows returned

Return "No address found for the specified MemberID."

End If

Catch

Throw New ApplicationException("Error retrieving member address.")

Finally

cnn.Close()

End Try

End Function

Sub btnmembers_Click(ByVal sender As Object, ByVal e As System.EventArgs)

' Use Response.Redirect

Response.Redirect("~/membersByRedirect.aspx?MemberID=" & ddlmembers.SelectedValue)

End Sub

End Class

3-CrossPage.aspx

<%@ Page AutoEventWireup="false" ClassName="CrossPage_aspx" CompileWith="CrossPage.aspx.vb" Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Cross-Page Posting</title>

</head>

<body>

<form id="form1" runat="server">

<h3>Enter New Member</h3>

<table>

<tr>

<td style="width: 100px">

<asp:Label ID="Label1" Runat="server" Height="15px" Width="125px" Text="Member Name"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtMemberName" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label2" Runat="server" Height="15px" Width="125px" Text="Street Address"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtAddress" Runat="server"></asp:TextBox></td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label3" Runat="server" Height="15px" Width="125px" Text="City"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtCity" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label4" Runat="server" Height="15px" Width="125px" Text="Region/State"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtRegion" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label5" Runat="server" Height="15px" Width="125px" Text="Country"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtCountry" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px; height: 26px" align="right">

</td>

<td style="width: 100px; height: 26px" align="right">

<asp:Button ID="btnNewMember" Runat="server" Text="Submit" Height="24px" Width="81px" />

</td>

</tr>

</table>

<h3>Query Existing Members</h3>

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT [memberID], [memberName] FROM [members] UNION SELECT '', ' -- Select member --' ORDER BY [memberName]"

ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>">

</asp:SqlDataSource>

<table style="height: 48px" align="left">

<tr>

<td style="width: 286px; height: 58px;" valign="baseline" align="right">

<asp:DropDownList ID="ddlMembers" Runat="server" DataSourceID="SqlDataSource1"

DataTextField="MemberName" DataValueField="MemberID" AutoPostBack="False" Width="223px"

Height="22px" >

</asp:DropDownList>

</td>

<td style="width: 233px; height: 58px;" valign="baseline" align="left">

<asp:Label ID="lblAddress" Runat="server" Height="19px" Width="239px"></asp:Label>

</td>

</tr>

<tr>

<td style="width: 286px; height: 26px" valign="baseline" align="left">

</td>

<td style="width: 233px; height: 26px" valign="baseline" align="left">

   

<asp:Button ID="btnMembers" Runat="server" Text="Show Members" OnClick="btnMembers_Click"

PostBackUrl="~/MemberByCrossPage.aspx" />

</td>

</tr>

</table><br />

 <br />

<br />

<br />

<br />

<asp:HyperLink ID="HyperLink1" Runat="server" NavigateUrl="Default.aspx">Home</asp:HyperLink>

</form>

</body>

</html>

<script language="javascript">

// This function will be called after

// the callback method on the server

// returns the members address.

function fillMemberAddress (result, context)

{

document.getElementById("lblAddress").innerHTML = result;

}

</script>

4-CrossPage.aspx.vb

Imports System.Data.SqlClient

Partial Class CrossPage_aspx

Implements System.Web.UI.ICallbackEventHandler

Sub btnMembers_Click(ByVal sender As Object, ByVal e As System.EventArgs)

' Use Cross-Page Posting

' No code required:

' set the PostBackURL property.

End Sub

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Get the client-side HTML for

' for calling back to the server.

' Pass in a reference to the page,

' the script that should be added

' to generate the argument passed

' back to the server, and the

' name of the server-side method.

' (The final parameter, context,

' isn't needed in this case.)

Dim strCallBack As String = _

GetCallbackEventReference(Me, _

"this.value", _

"fillMemberAddress", Nothing)

' Add the client-side onchange behavior

' to the drop-down list, to invoke the

' call back to the server.

ddlMembers.Attributes.Add( _

"onchange", strCallBack & ";return false;")

End Sub

' Implement the code that will run

' when called from the client.

Public Overridable Function RaiseCallbackEvent(ByVal argFromClient As String) _

As String Implements ICallbackEventHandler.RaiseCallbackEvent

Return GetAddress(argFromClient)

End Function

 

Private Function GetAddress( _

ByVal memberID As String) As String

' If passed an empty string,

' return an empty string.

If memberID = String.Empty Then

Return String.Empty

End If

Dim cnn As New SqlConnection()

Try

' Use value from config file to

' set up a database connection.

cnn.ConnectionString = _

ConfigurationSettings.ConnectionStrings("NorthwindConnection").ConnectionString

' Create a command to get

' address info for the

' specified member.

Dim commandText As String = _

"SELECT Address, City, Region, PostalCode, Country " _

& "FROM dbo.members " _

& "WHERE memberID = @memberID"

Dim cmd As New SqlCommand(commandText, cnn)

Dim prm As New SqlParameter( _

"MemberID", Data.SqlDbType.NChar, 5)

prm.Value = memberID

cmd.Parameters.Add(prm)

' Retrieve the address info

' and format it as one

' multi-line string.

Dim rdr As SqlDataReader

cnn.Open()

rdr = cmd.ExecuteReader

If rdr.Read Then

Dim sbAddress As New StringBuilder()

If rdr("Address").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}</br>", rdr("Address").ToString)

End If

If rdr("City").ToString.Length > 0 Then

If rdr("Region").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}, {1} ", rdr("City").ToString, rdr("Region"))

Else

sbAddress.AppendFormat("{0} ", rdr("City").ToString)

End If

End If

If rdr("PostalCode").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}</br>", rdr("PostalCode").ToString)

End If

sbAddress.Append(rdr("Country").ToString)

Return sbAddress.ToString

Else 'no rows returned

Return "No address found for the specified memberID."

End If

Catch

Throw New ApplicationException("Error retrieving member address.")

Finally

cnn.Close()

End Try

End Function

End Class

5-CrossPageStrong.aspx

<%@ Page AutoEventWireup="false" ClassName="CrossPageStrong_aspx" CompileWith="CrossPageStrong.aspx.vb" Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Cross-Page Posting with Strong Typing</title>

</head>

<body>

<form id="form1" runat="server">

<h3>Enter New Member</h3>

<table>

<tr>

<td style="width: 100px">

<asp:Label ID="Label1" Runat="server" Height="15px" Width="125px" Text="member Name"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtmemberName" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label2" Runat="server" Height="15px" Width="125px" Text="Street Address"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtAddress" Runat="server"></asp:TextBox></td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label3" Runat="server" Height="15px" Width="125px" Text="City"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtCity" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label4" Runat="server" Height="15px" Width="125px" Text="Region/State"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtRegion" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label5" Runat="server" Height="15px" Width="125px" Text="Country"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtCountry" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px; height: 26px" align="right">

</td>

<td style="width: 100px; height: 26px" align="right">

<asp:Button ID="btnNewMember" Runat="server" Text="Submit" Height="24px" Width="81px" />

</td>

</tr>

</table>

<h3>Query Existing Members</h3>

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT [memberID], [memberName] FROM [members] UNION SELECT '', ' -- Select member --' ORDER BY [memberName]"

ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>">

</asp:SqlDataSource>

<table style="height: 48px" align="left">

<tr>

<td style="width: 286px; height: 58px;" valign="baseline" align="right">

<asp:DropDownList ID="ddlMembers" Runat="server" DataSourceID="SqlDataSource1"

DataTextField="MemberName" DataValueField="MemberID" AutoPostBack="False" Width="223px"

Height="22px" >

</asp:DropDownList>

</td>

<td style="width: 233px; height: 58px;" valign="baseline" align="left">

<asp:Label ID="lblAddress" Runat="server" Height="19px" Width="239px"></asp:Label>

</td>

</tr>

<tr>

<td style="width: 286px; height: 26px" valign="baseline" align="left">

</td>

<td style="width: 233px; height: 26px" valign="baseline" align="left">

   

<asp:Button ID="btnMemberss" Runat="server" Text="Show Members" OnClick="btnMembers_Click"

PostBackUrl="~/MemberByCrossPageStrong.aspx" />

</td>

</tr>

</table><br />

 <br />

<br />

<br />

<br />

<asp:HyperLink ID="HyperLink1" Runat="server" NavigateUrl="Default.aspx">Home</asp:HyperLink>

</form>

</body>

</html>

<script language="javascript">

// This function will be called after

// the callback method on the server

// returns the members address.

function fillmemberAddress (result, context)

{

document.getElementById("lblAddress").innerHTML = result;

}

</script>

6-CrossPageStrong.aspx.vb

Imports System.Data.SqlClient

Partial Class CrossPageStrong_aspx

Implements System.Web.UI.ICallbackEventHandler

Sub btnSales_Click(ByVal sender As Object, ByVal e As System.EventArgs)

' Use Cross-Page Posting with strong typing.

' Set the PostBackURL to a page with a

' PreviousPageType directive.

End Sub

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Get the client-side HTML for

' for calling back to the server.

' Pass in a reference to the page,

' the script that should be added

' to generate the argument passed

' back to the server, and the

' name of the server-side method.

' (The final parameter, context,

' isn't needed in this case.)

Dim strCallBack As String = _

GetCallbackEventReference(Me, _

"this.value", _

"fillCustomerAddress", Nothing)

' Add the client-side onchange behavior

' to the drop-down list, to invoke the

' call back to the server.

ddlMembers.Attributes.Add( _

"onchange", strCallBack & ";return false;")

End Sub

' Implement the code that will run

' when called from the client.

Public Overridable Function RaiseCallbackEvent(ByVal argFromClient As String) _

As String Implements ICallbackEventHandler.RaiseCallbackEvent

Return GetAddress(argFromClient)

End Function

Private Function GetAddress( _

ByVal MemberID As String) As String

' If passed an empty string,

' return an empty string.

If MemberID = String.Empty Then

Return String.Empty

End If

Dim cnn As New SqlConnection()

Try

' Use value from config file to

' set up a database connection.

cnn.ConnectionString = _

ConfigurationSettings.ConnectionStrings("NorthwindConnection").ConnectionString

' Create a command to get

' address info for the

' specified Member.

Dim commandText As String = _

"SELECT Address, City, Region, PostalCode, Country " _

& "FROM dbo.Members " _

& "WHERE MemberID = @MemberID"

Dim cmd As New SqlCommand(commandText, cnn)

Dim prm As New SqlParameter( _

"memberID", Data.SqlDbType.NChar, 5)

prm.Value = MemberID

cmd.Parameters.Add(prm)

' Retrieve the address info

' and format it as one

' multi-line string.

Dim rdr As SqlDataReader

cnn.Open()

rdr = cmd.ExecuteReader

If rdr.Read Then

Dim sbAddress As New StringBuilder()

If rdr("Address").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}</br>", rdr("Address").ToString)

End If

If rdr("City").ToString.Length > 0 Then

If rdr("Region").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}, {1} ", rdr("City").ToString, rdr("Region"))

Else

sbAddress.AppendFormat("{0} ", rdr("City").ToString)

End If

End If

If rdr("PostalCode").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}</br>", rdr("PostalCode").ToString)

End If

sbAddress.Append(rdr("Country").ToString)

Return sbAddress.ToString

Else 'no rows returned

Return "No address found for the specified MemberID."

End If

Catch

Throw New ApplicationException("Error retrieving member address.")

Finally

cnn.Close()

End Try

End Function

' This is added as a workaround.

' ddlCustomers should be accessible from

' cross-post pages that have a PreviousPageType directive.

Public ReadOnly Property memberss() As DropDownList

Get

Return ddlMembers

End Get

End Property

End Class

7-Default.aspx

<%@ Page %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Home</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:HyperLink ID="HyperLink1" Runat="server" NavigateUrl="~/Postback.aspx">Standard Postback</asp:HyperLink><br />

<asp:HyperLink ID="HyperLink2" Runat="server" NavigateUrl="~/Callback.aspx">Client Callback</asp:HyperLink><br />

<asp:HyperLink ID="HyperLink3" Runat="server" NavigateUrl="~/CrossPage.aspx">Cross-Page Posting</asp:HyperLink><br />

<asp:HyperLink ID="HyperLink4" Runat="server" NavigateUrl="~/CrossPageStrong.aspx">Cross-Page Posting with Strong Typing</asp:HyperLink><br />

<asp:HyperLink ID="HyperLink5" Runat="server" NavigateUrl="~/ValidationAndFocusProblem.aspx">Validation and Focus Problem</asp:HyperLink><br />

<asp:HyperLink ID="HyperLink6" Runat="server" NavigateUrl="~/ValidationAndFocusSolution.aspx">Validation and Focus Solution</asp:HyperLink>

</div>

</form>

</body>

</html>

8-Default.aspx.vb

Partial Class Default.aspx

End Class

9-MembersByCrossPage.aspx

<%@ Page AutoEventWireup="false" ClassName="CustomerSalesByCrossPage_aspx" CompileWith="CustomerSalesByCrossPage.aspx.vb" Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Members By Cross-Page Post</title>

</head>

<body>

<form id="form1" runat="server">

<asp:Label ID="lblTitle" Runat="server" Text="Members History" Width="294px" Height="19px" Font-Bold="True" Font-Names="Tahoma"></asp:Label>

<br />

<div>

<asp:GridView ID="gvMembers" Runat="server" BorderWidth="1px" BackColor="White" CellPadding="4"

BorderStyle="None" BorderColor="#3366CC" AutoGenerateColumns="False">

<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>

<PagerStyle ForeColor="#003399" HorizontalAlign="Left" BackColor="#99CCCC"></PagerStyle>

<HeaderStyle ForeColor="#CCCCFF" Font-Bold="True" BackColor="#003399"></HeaderStyle>

<Columns>

<asp:BoundField HeaderText="Date" DataField="DateEnter" DataFormatString="{0:d}"></asp:BoundField>

<asp:BoundField HeaderText="member ID" DataField="member ID"></asp:BoundField>

<asp:BoundField HeaderText="MemberName" DataField="MemberName"></asp:BoundField>

</Columns>

<SelectedRowStyle ForeColor="#CCFF99" Font-Bold="True" BackColor="#009999"></SelectedRowStyle>

<RowStyle ForeColor="#003399" BackColor="White"></RowStyle>

</asp:GridView><br />

<asp:HyperLink ID="HyperLink1" Runat="server" NavigateUrl="Default.aspx">Home</asp:HyperLink>

</div>

</form>

</body>

</html>

10-MembersByCrossPage.aspx

Imports System.Data.SqlClient

Partial Class membersByCrossPage_aspx

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Get memberID from PreviousPage.

' (Note that, when no member is selected in ddlmembers,

' the value is actually formatted as 5 spaces.)

Dim strMemberID As String

If PreviousPage IsNot Nothing _

AndAlso PreviousPage.IsCrossPagePostBack _

AndAlso CType(PreviousPage.FindControl("ddlmembers"), DropDownList).SelectedValue <> New String(" "c, 5) Then

strMemberID = CType(PreviousPage.FindControl("ddlmembers"), DropDownList).SelectedValue

Else

lblTitle.Text = "No member Specified"

gvmembers.Visible = False

Return

End If

' Set up data access objects

Dim cnn As New SqlConnection(ConfigurationSettings.ConnectionStrings("NorthwindConnection").ConnectionString)

Dim cmd As New SqlCommand("SELECT MemberName FROM members WHERE memberID=@MemberID", cnn)

Dim prm As New SqlParameter("@MemberID", Data.SqlDbType.NChar, 5)

prm.Value = strMemberID

cmd.Parameters.Add(prm)

Dim rdr As SqlDataReader

' Retrieve and bind data

cnn.Open()

rdr = cmd.ExecuteReader

If rdr.Read Then

lblTitle.Text = "Members for " & rdr.GetString(0)

rdr.Close()

cmd.CommandText = _

"SELECT members.EnterDate, Members.MemberID, [Members].memberName As MemberName " _

& "FROM dbo.[members] INNER JOIN dbo.Members ON dbo.[members].memberID = dbo.Members.memberID " _

& "WHERE memberID = @memberID " _

& "ORDER BY DateEnter DESC "

rdr = cmd.ExecuteReader

gvmembers.DataSource = rdr

gvmembers.DataBind()

Else

lblTitle.Text = "No member Matches the Specified memberID"

gvmembers.Visible = False

End If

cnn.Close()

End Sub

End Class

11-MembersByCrossPageStrong.aspx

<%@ Page AutoEventWireup="false" ClassName="CustomerSalesByCrossPageStrong_aspx" CompileWith="CustomerSalesByCrossPageStrong.aspx.vb" Language="VB" %>

<%@ PreviousPageType VirtualPath="~/CrossPageStrong.aspx"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Members by Cross-Page Post with Strong Typing</title>

</head>

<body>

<form id="form1" runat="server">

<asp:Label ID="lblTitle" Runat="server" Text="Members History" Width="294px" Height="19px" Font-Bold="True" Font-Names="Tahoma"></asp:Label>

<br />

<div>

<asp:GridView ID="gvMembers" Runat="server" BorderWidth="1px" BackColor="White" CellPadding="4"

BorderStyle="None" BorderColor="#3366CC" AutoGenerateColumns="False">

<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>

<PagerStyle ForeColor="#003399" HorizontalAlign="Left" BackColor="#99CCCC"></PagerStyle>

<HeaderStyle ForeColor="#CCCCFF" Font-Bold="True" BackColor="#003399"></HeaderStyle>

<Columns>

<asp:BoundField HeaderText="Date" DataField="EnterDate" DataFormatString="{0:d}"></asp:BoundField>

<asp:BoundField HeaderText="member ID" DataField="MemberID"></asp:BoundField>

<asp:BoundField HeaderText="membername" DataField="membername"></asp:BoundField>

</Columns>

<SelectedRowStyle ForeColor="#CCFF99" Font-Bold="True" BackColor="#009999"></SelectedRowStyle>

<RowStyle ForeColor="#003399" BackColor="White"></RowStyle>

</asp:GridView><br />

<asp:HyperLink ID="HyperLink1" Runat="server" NavigateUrl="Default.aspx">Home</asp:HyperLink>

</div>

</form>

</body>

</html>

12-MembersByCrossPageStrong.aspx.vb

Imports System.Data.SqlClient

Partial Class MembersCrossPageStrong_aspx

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Get MemberID from PreviousPage.

' (Note that, when no member is selected in ddlMembers,

' the value is actually formatted as 5 spaces.)

Dim strmemberID As String

' In Beta 1, protected control properties from

' the prevous page are not accessible,

' so a public property is used.

If PreviousPage IsNot Nothing _

AndAlso PreviousPage.IsCrossPagePostBack _

AndAlso PreviousPage.members.SelectedValue <> New String(" "c, 5) Then

strmemberID = PreviousPage.Members.SelectedValue

Else

lblTitle.Text = "No member Specified"

gvmembers.Visible = False

Return

End If

' Set up data access objects

Dim cnn As New SqlConnection(ConfigurationSettings.ConnectionStrings(NorthwindConnection").ConnectionString)

Dim cmd As New SqlCommand("SELECT memberName FROM members WHERE MemberID=@MemberID", cnn)

Dim prm As New SqlParameter("@memberID", Data.SqlDbType.NChar, 5)

prm.Value = strmemberID

cmd.Parameters.Add(prm)

Dim rdr As SqlDataReader

' Retrieve and bind data

cnn.Open()

rdr = cmd.ExecuteReader

If rdr.Read Then

lblTitle.Text = "Members for " & rdr.GetString(0)

rdr.Close()

cmd.CommandText = _

"SELECT EnterDate, members.memberID, [Members].MemberName As Membername " _

& "FROM dbo.[members] INNER JOIN dbo.members ON dbo.[members].MemberID = dbo.members.memberID " _

& "WHERE memberID = @MemberID " _

& "ORDER BY EnterDate DESC "

rdr = cmd.ExecuteReader

gvMembers.DataSource = rdr

gvMembers.DataBind()

Else

lblTitle.Text = "No member Matches the Specified memberID"

gvmembers.Visible = False

End If

cnn.Close()

End Sub

End Class

13-MembersByRedirect.aspx

<%@ Page AutoEventWireup="false" ClassName="CustomerSalesByRedirect_aspx" CompileWith="CustomerSalesByRedirect.aspx.vb" Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Members By Redirect</title>

</head>

<body>

<form id="form1" runat="server">

<asp:Label ID="lblTitle" Runat="server" Text="Members History" Width="294px" Height="19px" Font-Bold="True" Font-Names="Tahoma"></asp:Label>

<br />

<div>

<asp:GridView ID="gvMembers" Runat="server" BorderWidth="1px" BackColor="White" CellPadding="4"

BorderStyle="None" BorderColor="#3366CC" AutoGenerateColumns="False">

<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>

<PagerStyle ForeColor="#003399" HorizontalAlign="Left" BackColor="#99CCCC"></PagerStyle>

<HeaderStyle ForeColor="#CCCCFF" Font-Bold="True" BackColor="#003399"></HeaderStyle>

<Columns>

<asp:BoundField HeaderText="Date" DataField="EnterDate" DataFormatString="{0:d}"></asp:BoundField>

<asp:BoundField HeaderText="member ID" DataField="MemberID"></asp:BoundField>

<asp:BoundField HeaderText="membername" DataField="membername"></asp:BoundField>

</Columns>

<SelectedRowStyle ForeColor="#CCFF99" Font-Bold="True" BackColor="#009999"></SelectedRowStyle>

<RowStyle ForeColor="#003399" BackColor="White"></RowStyle>

</asp:GridView><br />

<asp:HyperLink ID="HyperLink1" Runat="server" NavigateUrl="Default.aspx">Home</asp:HyperLink>

</div>

</form>

</body>

</html>

14-MembersByRedirect.aspx.vb

Imports System.Data.SqlClient

Partial Class MembersRedirect_aspx

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Get memberID from Query String

Dim strmemberID As String

If Request.QueryString("memberID") IsNot Nothing _

AndAlso Request.QueryString("memberID").Length > 0 Then

strmemberID = Request.QueryString("memberID")

Else

lblTitle.Text = "No Members Specified"

gvmembers.Visible = False

Return

End If

' Set up data access objects

Dim cnn As New SqlConnection(ConfigurationSettings.ConnectionStrings("NorthwindConnection").ConnectionString)

Dim cmd As New SqlCommand("SELECT memberName FROM members WHERE memberID=@memberID", cnn)

Dim prm As New SqlParameter("@memberID", Data.SqlDbType.NChar, 5)

prm.Value = strmemberID

cmd.Parameters.Add(prm)

Dim rdr As SqlDataReader

' Retrieve and bind data

cnn.Open()

rdr = cmd.ExecuteReader

If rdr.Read Then

lblTitle.Text = "Members for " & rdr.GetString(0)

rdr.Close()

cmd.CommandText = _

"SELECT EnterDate, members.memberID, [members].Membername As membername " _

& "FROM dbo.[members] INNER JOIN dbo.Members ON dbo.[Members].MemberID = dbo.members.memberID " _

& "WHERE memberID = @MemberID " _

& "ORDER BY Enterdate DESC "

rdr = cmd.ExecuteReader

gvmembers.DataSource = rdr

gvmemberss.DataBind()

Else

lblTitle.Text = "No member Matches the Specified memberID"

gvMembers.Visible = False

End If

cnn.Close()

End Sub

End Class

15-Postback.aspx

<%@ Page AutoEventWireup="false" ClassName="Postback_aspx" CompileWith="Postback.aspx.vb" Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Postback</title>

</head>

<body>

<form id="form1" runat="server">

<h3>Enter New Member</h3>

<table>

<tr>

<td style="width: 100px">

<asp:Label ID="Label1" Runat="server" Height="15px" Width="125px" Text="Member Name"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtMemberName" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label2" Runat="server" Height="15px" Width="125px" Text="Street Address"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtAddress" Runat="server"></asp:TextBox></td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label3" Runat="server" Height="15px" Width="125px" Text="City"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtCity" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label4" Runat="server" Height="15px" Width="125px" Text="Region/State"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtRegion" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label5" Runat="server" Height="15px" Width="125px" Text="Country"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtCountry" Runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 100px; height: 26px" align="right">

</td>

<td style="width: 100px; height: 26px" align="right">

<asp:Button ID="btnNewNember" Runat="server" Text="Submit" Height="24px" Width="81px" />

</td>

</tr>

</table>

<h3>Query Existing Members</h3>

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT [memberID], [memberName] FROM [members] UNION SELECT '', ' -- Select member --' ORDER BY [memberName]"

ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>">

</asp:SqlDataSource>

<table style="height: 48px" align="left">

<tr>

<td style="width: 286px; height: 58px;" valign="baseline" align="right">

<asp:DropDownList ID="ddlMembers" Runat="server" DataSourceID="SqlDataSource1"

DataTextField="MemberName" DataValueField="MemberID" AutoPostBack="True" Width="223px"

Height="22px" OnSelectedIndexChanged="ddlMembers_SelectedIndexChanged">

</asp:DropDownList>

</td>

<td style="width: 233px; height: 58px;" valign="baseline" align="left">

<asp:Label ID="lblAddress" Runat="server" Height="19px" Width="239px"></asp:Label>

</td>

</tr>

<tr>

<td style="width: 286px; height: 26px" valign="baseline" align="left">

</td>

<td style="width: 233px; height: 26px" valign="baseline" align="left">

   

<asp:Button ID="btnMembers" Runat="server" Text="Show members" OnClick="btnMembers_Click" />

</td>

</tr>

</table><br />

 <br />

<br />

<br />

<br />

<asp:HyperLink ID="HyperLink1" Runat="server" NavigateUrl="Default.aspx">Home</asp:HyperLink>

</form>

</body>

</html>

 

16-Postback.aspx.vb

Imports System.Data.SqlClient

Partial Class Postback_aspx

Sub ddlmembers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

lblAddress.Text = GetAddress(ddlmembers.SelectedValue)

End Sub

Private Function GetAddress( _

ByVal memberID As String) As String

' If passed an empty string,

' return an empty string.

If memberID = String.Empty Then

Return String.Empty

End If

Dim cnn As SqlConnection

Try

' Use value from config file to

' set up a database connection.

cnn = New SqlConnection( _

ConfigurationSettings.ConnectionStrings("NorthwindConnection").ConnectionString)

' Create a command to get

' address info for the

' specified member.

Dim commandText As String = _

"SELECT Address, City, Region, PostalCode, Country " _

& "FROM dbo.members " _

& "WHERE memberID = @memberID"

Dim cmd As New SqlCommand(commandText, cnn)

Dim prm As New SqlParameter( _

"memberID", Data.SqlDbType.NChar, 5)

prm.Value = memberID

cmd.Parameters.Add(prm)

' Retrieve the address info

' and format it as one

' multi-line string.

Dim rdr As SqlDataReader

cnn.Open()

rdr = cmd.ExecuteReader

If rdr.Read Then

Dim sbAddress As New StringBuilder()

If rdr("Address").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}</br>", rdr("Address").ToString)

End If

If rdr("City").ToString.Length > 0 Then

If rdr("Region").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}, {1} ", rdr("City").ToString, rdr("Region"))

Else

sbAddress.AppendFormat("{0} ", rdr("City").ToString)

End If

End If

If rdr("PostalCode").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}</br>", rdr("PostalCode").ToString)

End If

sbAddress.Append(rdr("Country").ToString)

Return sbAddress.ToString

Else 'no rows returned

Return "No address found for the specified memberID."

End If

Catch

Throw New ApplicationException("Error retrieving member address.")

Finally

If Not cnn Is Nothing Then

cnn.Close()

End If

End Try

End Function

Sub btnmembers_Click(ByVal sender As Object, ByVal e As System.EventArgs)

' Use Response.Redirect

Response.Redirect("~/membersByRedirect.aspx?memberID=" & ddlmembers.SelectedValue)

End Sub

End Class

17-Posting.sln

Microsoft Visual Studio Solution File, Format Version 9.00

# Visual Studio 2005

Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "C:\Mai\MemberPosting\", "C:\Mai\MemberPosting\", "{2BE02297-39F9-45BC-AC33-12F117054F7D}"

ProjectSection(WebsiteProperties) = preProject

VWDPort = 1286

EndProjectSection

EndProject

Global

GlobalSection(SolutionConfigurationPlatforms) = preSolution

Debug|.NET = Debug|.NET

EndGlobalSection

GlobalSection(ProjectConfigurationPlatforms) = postSolution

{2BE02297-39F9-45BC-AC33-12F117054F7D}.Debug|.NET.ActiveCfg = Debug|.NET

{2BE02297-39F9-45BC-AC33-12F117054F7D}.Debug|.NET.Build.0 = Debug|.NET

EndGlobalSection

GlobalSection(SolutionProperties) = preSolution

HideSolutionNode = FALSE

EndGlobalSection

EndGlobal

18-StyleSheet.css

body

{

font-family: Verdana;

}

.Grid

{

font-size: 10pt;

font-family: Verdana;

}

H1

{

font-size: 24pt;

font-family: Verdana;

font-weight: bold;

}

.Button

{

font-size: 10pt;

font-family: Verdana;

}

INPUT

{

font-size: 10pt;

font-family: Verdana;

}

SELECT

{

font-size: 10pt;

font-family: Verdana;

}

19-ValidationAndFocusProblem.aspx

<%@ Page AutoEventWireup="false" ClassName="ValidationAndFocusProblem_aspx" CompileWith="ValidationAndFocusProblem.aspx.vb" Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Validation and Focus Problem</title>

</head>

<body>

<form id="form1" runat="server">

<h3>Enter New members</h3>

<table>

<tr>

<td style="width: 100px">

<asp:Label ID="Label1" Runat="server" Height="15px" Width="125px" Text="member Name"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtMemberName" Runat="server"></asp:TextBox>

</td>

<td style="width: 74px">

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" Runat="server" ErrorMessage="*Required"

ControlToValidate="txtMemberName">

</asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label2" Runat="server" Height="15px" Width="125px" Text="Street Address"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtAddress" Runat="server"></asp:TextBox></td>

<td style="width: 74px">

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" Runat="server" ErrorMessage="*Required"

ControlToValidate="txtAddress">

</asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label3" Runat="server" Height="15px" Width="125px" Text="City"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtCity" Runat="server"></asp:TextBox>

</td>

<td style="width: 74px">

<asp:RequiredFieldValidator ID="RequiredFieldValidator3" Runat="server" ErrorMessage="*Required"

ControlToValidate="txtCity">

</asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label4" Runat="server" Height="15px" Width="125px" Text="Region/State"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtRegion" Runat="server"></asp:TextBox>

</td>

<td style="width: 74px">

 

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label5" Runat="server" Height="15px" Width="125px" Text="Country"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtCountry" Runat="server"></asp:TextBox>

</td>

<td style="width: 74px">

<asp:RequiredFieldValidator ID="RequiredFieldValidator5" Runat="server" ErrorMessage="*Required"

ControlToValidate="txtCountry">

</asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td style="width: 100px; height: 26px" align="right">

</td>

<td style="width: 100px; height: 26px" align="right">

<asp:Button ID="btnNewMember" Runat="server" Text="Submit" Height="24px" Width="81px" />

</td>

<td style="width: 74px; height: 26px" align="right">

</td>

</tr>

</table>

<h3>Query Existing Memberss</h3>

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT [memberID], [memberName] FROM [members] UNION SELECT '', ' -- Select member --' ORDER BY [memberName]"

ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>">

<table style="height: 48px" align="left">

<tr>

<td style="width: 286px; height: 58px;" valign="baseline" align="right">

<asp:DropDownList ID="ddlMembrds" Runat="server" DataSourceID="SqlDataSource1"

DataTextField="MemberName" DataValueField="MemberID" AutoPostBack="False" Width="201px"

Height="22px" >

</asp:DropDownList> 

</td>

<td style="width: 174px; height: 58px;" valign="baseline" align="left">

<asp:Label ID="lblAddress" Runat="server" Height="19px" Width="150px"></asp:Label>

</td>

<td style="width: 350px; height: 58px" valign="baseline" align="left">

</td>

</tr>

<tr>

<td style="width: 286px; height: 26px" valign="baseline" align="left">

</td>

<td style="width: 174px; height: 26px" valign="baseline" align="left">

   

<asp:Button ID="btnmembers" Runat="server" Text="Show Members" OnClick="btnmembers_Click" />

</td>

<td style="width: 350px; height: 26px" valign="baseline" align="left">

</td>

</tr>

</table><br />

 <br />

<br />

<br />

<br />

<asp:HyperLink ID="HyperLink1" Runat="server" NavigateUrl="Default.aspx">Home</asp:HyperLink>

</form>

</body>

</html>

<script language="javascript">

// This function will be called after

// the callback method on the server

// returns the member address.

function fillMemberAddress (result, context)

{

document.getElementById("lblAddress").innerHTML = result;

}

</script>

20-ValidationAndFocusProblem.aspx.vb

Imports System.Data.SqlClient

Partial Class ValidationAndFocusProblem.aspx

Implements System.Web.UI.ICallbackEventHandler

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Get the client-side HTML for

' for calling back to the server.

' Pass in a reference to the page,

' the script that should be added

' to generate the argument passed

' back to the server, and the

' name of the server-side method.

' (The final parameter, context,

' isn't needed in this case.)

Dim strCallBack As String = _

GetCallbackEventReference(Me, _

"this.value", _

"fillmemberAddress", Nothing)

' Add the client-side onchange behavior

' to the drop-down list, to invoke the

' call back to the server.

ddlmembers.Attributes.Add( _

"onchange", strCallBack & ";return false;")

End Sub

' Implement the code that will run

' when called from the client.

Public Overridable Function RaiseCallbackEvent(ByVal argFromClient As String) _

As String Implements ICallbackEventHandler.RaiseCallbackEvent

Return GetAddress(argFromClient)

End Function

Private Function GetAddress( _

ByVal memberID As String) As String

' If passed an empty string,

' return an empty string.

If memberID = String.Empty Then

Return String.Empty

End If

Dim cnn As New SqlConnection()

Try

' Use value from config file to

' set up a database connection.

cnn.ConnectionString = _

ConfigurationSettings.ConnectionStrings("membersConnection").ConnectionString

' Create a command to get

' address info for the

' specified member.

Dim commandText As String = _

"SELECT Address, City, Region, PostalCode, Country " _

& "FROM dbo.members " _

& "WHERE memberID = @memberID"

Dim cmd As New SqlCommand(commandText, cnn)

Dim prm As New SqlParameter( _

"memberID", Data.SqlDbType.NChar, 5)

prm.Value = memberID

cmd.Parameters.Add(prm)

' Retrieve the address info

' and format it as one

' multi-line string.

Dim rdr As SqlDataReader

cnn.Open()

rdr = cmd.ExecuteReader

If rdr.Read Then

Dim sbAddress As New StringBuilder()

If rdr("Address").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}</br>", rdr("Address").ToString)

End If

If rdr("City").ToString.Length > 0 Then

If rdr("Region").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}, {1} ", rdr("City").ToString, rdr("Region"))

Else

sbAddress.AppendFormat("{0} ", rdr("City").ToString)

End If

End If

If rdr("PostalCode").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}</br>", rdr("PostalCode").ToString)

End If

sbAddress.Append(rdr("Country").ToString)

Return sbAddress.ToString

Else 'no rows returned

Return "No address found for the specified memberID."

End If

Catch

Throw New ApplicationException("Error retrieving member address.")

Finally

cnn.Close()

End Try

End Function

Sub btnSales_Click(ByVal sender As Object, ByVal e As System.EventArgs)

' Use Response.Redirect

Response.Redirect("~/membersByRedirect.aspx?memberID=" & ddlmembers.SelectedValue)

End Sub

End Class

22-ValidationAndFocusSolution.aspx

<%@ Page AutoEventWireup="false" ClassName="ValidationAndFocusSolution_aspx" CompileWith="ValidationAndFocusSolution.aspx.vb" Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Validation and Focus Solution</title>

</head>

<body>

<form id="form1" runat="server"

defaultFocus="txtMemberName"

defaultButton="btnMembers">

<h3>Enter New Member</h3>

<table>

<tr>

<td style="width: 100px">

<asp:Label ID="Label1" Runat="server" Height="15px" Width="125px" Text="Member Name"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtMemberName" Runat="server"></asp:TextBox>

</td>

<td style="width: 74px">

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" Runat="server" ErrorMessage="*Required"

ControlToValidate="txtMemberName" ValidationGroup="New" SetFocusOnError="True">

</asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label2" Runat="server" Height="15px" Width="125px" Text="Street Address"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtAddress" Runat="server"></asp:TextBox></td>

<td style="width: 74px">

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" Runat="server" ErrorMessage="*Required"

ControlToValidate="txtAddress" ValidationGroup="New" SetFocusOnError="True">

</asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label3" Runat="server" Height="15px" Width="125px" Text="City"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtCity" Runat="server"></asp:TextBox>

</td>

<td style="width: 74px">

<asp:RequiredFieldValidator ID="RequiredFieldValidator3" Runat="server" ErrorMessage="*Required"

ControlToValidate="txtCity" ValidationGroup="New" SetFocusOnError="True">

</asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label4" Runat="server" Height="15px" Width="125px" Text="Region/State"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtRegion" Runat="server"></asp:TextBox>

</td>

<td style="width: 74px">

 

</td>

</tr>

<tr>

<td style="width: 100px">

<asp:Label ID="Label5" Runat="server" Height="15px" Width="125px" Text="Country"></asp:Label>

</td>

<td style="width: 100px">

<asp:TextBox ID="txtCountry" Runat="server"></asp:TextBox>

</td>

<td style="width: 74px">

<asp:RequiredFieldValidator ID="RequiredFieldValidator5" Runat="server" ErrorMessage="*Required"

ControlToValidate="txtCountry" ValidationGroup="New" SetFocusOnError="True">

</asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td style="width: 100px; height: 26px" align="right">

</td>

<td style="width: 100px; height: 26px" align="right">

<asp:Button ID="btnNewMember" Runat="server" Text="Submit" Height="24px" Width="81px" ValidationGroup="New" />

</td>

<td style="width: 74px; height: 26px" align="right">

</td>

</tr>

</table>

<h3>Query Existing Memberss</h3>

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT [memberID], [memberName] FROM [members] UNION SELECT '', ' -- Select member --' ORDER BY [memberName]"

ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>">

</asp:SqlDataSource>

<table style="height: 48px" align="left">

<tr>

<td style="width: 286px; height: 58px;" valign="baseline" align="right">

<asp:DropDownList ID="ddlMembers" Runat="server" DataSourceID="SqlDataSource1"

DataTextField="MemberName" DataValueField="MemberID" AutoPostBack="False" Width="201px"

Height="22px" >

</asp:DropDownList> 

</td>

<td style="width: 174px; height: 58px;" valign="baseline" align="left">

<asp:Label ID="lblAddress" Runat="server" Height="19px" Width="150px"></asp:Label>

</td>

<td style="width: 350px; height: 58px" valign="baseline" align="left">

</td>

</tr>

<tr>

<td style="width: 286px; height: 26px" valign="baseline" align="left">

</td>

<td style="width: 174px; height: 26px" valign="baseline" align="left">

   

<asp:Button ID="btnmembers" Runat="server" Text="Show Members" OnClick="btnMembers_Click" />

</td>

<td style="width: 350px; height: 26px" valign="baseline" align="left">

</td>

</tr>

</table><br />

 <br />

<br />

<br />

<br />

<asp:HyperLink ID="HyperLink1" Runat="server" NavigateUrl="Default.aspx">Home</asp:HyperLink>

</form>

</body>

</html>

<script language="javascript">

// This function will be called after

// the callback method on the server

// returns the member address.

function fillMemberAddress (result, context)

{

document.getElementById("lblAddress").innerHTML = result;

}

</script>

23-ValidationAndFocusSolution.aspx

Imports System.Data.SqlClient

Partial Class ValidationAndFocusSolution.aspx

Implements System.Web.UI.ICallbackEventHandler

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Get the client-side HTML for

' for calling back to the server.

' Pass in a reference to the page,

' the script that should be added

' to generate the argument passed

' back to the server, and the

' name of the server-side method.

' (The final parameter, context,

' isn't needed in this case.)

Dim strCallBack As String = _

GetCallbackEventReference(Me, _

"this.value", _

"fillmemberAddress", Nothing)

' Add the client-side onchange behavior

' to the drop-down list, to invoke the

' call back to the server.

ddlmembers.Attributes.Add( _

"onchange", strCallBack & ";return false;")

End Sub

' Implement the code that will run

' when called from the client.

Public Overridable Function RaiseCallbackEvent(ByVal argFromClient As String) _

As String Implements ICallbackEventHandler.RaiseCallbackEvent

Return GetAddress(argFromClient)

End Function

Private Function GetAddress( _

ByVal memberID As String) As String

' If passed an empty string,

' return an empty string.

If memberID = String.Empty Then

Return String.Empty

End If

Dim cnn As New SqlConnection()

Try

' Use value from config file to

' set up a database connection.

cnn.ConnectionString = _

ConfigurationSettings.ConnectionStrings("membersConnection").ConnectionString

' Create a command to get

' address info for the

' specified member.

Dim commandText As String = _

"SELECT Address, City, Region, PostalCode, Country " _

& "FROM dbo.members " _

& "WHERE memberID = @memberID"

Dim cmd As New SqlCommand(commandText, cnn)

Dim prm As New SqlParameter( _

"memberID", Data.SqlDbType.NChar, 5)

prm.Value = memberID

cmd.Parameters.Add(prm)

' Retrieve the address info

' and format it as one

' multi-line string.

Dim rdr As SqlDataReader

cnn.Open()

rdr = cmd.ExecuteReader

If rdr.Read Then

Dim sbAddress As New StringBuilder()

If rdr("Address").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}</br>", rdr("Address").ToString)

End If

If rdr("City").ToString.Length > 0 Then

If rdr("Region").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}, {1} ", rdr("City").ToString, rdr("Region"))

Else

sbAddress.AppendFormat("{0} ", rdr("City").ToString)

End If

End If

If rdr("PostalCode").ToString.Length > 0 Then

sbAddress.AppendFormat("{0}</br>", rdr("PostalCode").ToString)

End If

sbAddress.Append(rdr("Country").ToString)

Return sbAddress.ToString

Else 'no rows returned

Return "No address found for the specified memberID."

End If

Catch

Throw New ApplicationException("Error retrieving member address.")

Finally

cnn.Close()

End Try

End Function

Sub btnmembers_Click(ByVal sender As Object, ByVal e As System.EventArgs)

' Use Response.Redirect

Response.Redirect("~/membersByRedirect.aspx?CustomerID=" & ddlmembers.SelectedValue)

End Sub

End Class

24-Web.config

<?xml version="1.0"?>

<!-- Note: As an alternative to hand editing this file you can use the web admin tool to

configure settings for your application. Use the Website->Asp.Net Configuration option

in Visual Studio.

A full list of settings and comments can be found in machine.config.comments usually

located in \Windows\Microsft.Net\Frameworks\v2.x\Config -->

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

<appSettings/>

<connectionStrings>

<add name="NorthwindConnection" connectionString="Server=Lab;Integrated Security=True;Database=Northwind"

providerName="System.Data.SqlClient" />

</connectionStrings>

<system.web>

<!--

Set compilation debug="true" to insert debugging symbols into the compiled page.

Because this affects performance, set this value to true only during development.

-->

<compilation debug="true"/>

<!--

The <authentication> section enables configuration of the security authentication

mode used by ASP.NET to identify an incoming user.

-->

<authentication mode="Windows"/>

<!--

The <customErrors> section enables configuration of what to do if/when an unhandled

error occurs during the execution of a request. Specifically, it enables developers

to configure html error pages to be displayed in place of a error stack trace.

-->

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">

<!--

<error statusCode="403" redirect="NoAccess.htm"/>

<error statusCode="404" redirect="FileNotFound.htm"/>

-->

</customErrors>

</system.web>

</configuration>

 

 

 

 

 

 


 

 
Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!”



Advertise on XMLPitstop

Advertise on XMLPitstop


EggHead Cafe
Web Servicee development
DotNetSlackers
Buy text ads here!
remote online storage
Color Laser Printer
Chanel sunglasses
Skype vs. sipcall
VoIP Internettelefonie
Buy text ads here!

Interested in Text ads?
2,223 Total Members
36 members(last 30 days)
8 members(last 7 days)
2 members(today)

1,609 Total Discussions
12 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

17,260 Total Blog Posts
1,839 Blogs(last 30 days)
342 Blogs(last 7 days)
49 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,786 Total Resources
5 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139