Tải bản đầy đủ (.pdf) (12 trang)

Tài liệu Make a Generic Search Form in an ASP.NET docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (43.65 KB, 12 trang )

8.8 Make a Generic Search Form in an ASP.NET Web Application
This tutorial will show you how to add a unique search Web Form to your Web
application that can be set up using session variables and used for various tables.
Creating search forms for Web applications is pretty common. You would like to be able
to use the same Web Form for different tables in the same application.
This How-To will demonstrate using the same Web Form for searching various tables,
taking advantage of the Session object.
Technique
This How-To will use the Session object with a Web Form, something that has been done
throughout the book. You will see a good example of using the Session object to pass
values-in this case, the record source, the key search field, and the display search field-to
the search form. Finally, the return value will be passed back to the calling Web Form
using the Session object.
One new control that is used in this How-To is the Panel control. It will be used to group
the controls on the search Web Form. The Panel control will be used simply by throwing
controls into it and letting it control the layout.
You will see the paging used with the DataGrid control as well.
Steps
Open and run the VB.NET -Chapter 8 solution. From the main Web Form, click on the
hyperlink with the caption How-To 8.8: Make a Generic Search Form Using a Web
Form. This page is a simple one that contains text boxes for the Customer table in
Northwind (see Figure 8.15).
Figure 8.15. This Customers page is a common one used to demonstrate calling the
search form.

Click on the Search button to open the search page, and then click on the button labeled
B. You will see the DataGrid object displayed in the bottom of the form filled with the
CompanyID and CompanyName fields of the Customer table, which begin with B (see
Figure 8.16).
Figure 8.16. This form can be used for searching within any of the tables in your
databases.



Click the Select button for one of the customers who is displayed in the grid, and then
click Accept. The customer page will be displayed, and the fields will be filled in with the
data from the chosen record.
1. Create a Web Form. Then place the controls shown in Figure 8.16 of the form
calling the search form, with the properties set forth in Table 8.10.
Table 8.10. Label, TextBox, and Command Button Controls Property
Settings for the Calling Form
Object Property Setting
DOCUMENT bgColor buttonface
Label Caption Customer ID
Label Caption Company Name
Label Caption Contact
Label Caption Contact Title
Label Caption Address
Label Caption City
Label Caption Region
Label Caption Country
Label Caption Phone
Label Caption Fax
TextBox Name txtCustomerID
TextBox Name txtCompanyName
TextBox Name txtContactName
TextBox Name txtContactTitle
TextBox Name txtAddress
TextBox Name txtCity
TextBox Name txtRegion
TextBox Name txtPostalCode
TextBox Name txtCountry
TextBox Name txtPhone

TextBox Name txtFax
Button Name btnSearch
Caption &Search
2. On btnSearch, add the code in Listing 8.45 to the Click event. This sets up the
search Web Form as far as telling it what it needs to know, including how to get
back to this page, which is the calling page. The search page, in this case
"wfrmHowTo8_8b.aspx," is then opened.
Listing 8.45 frmHowTo8_8a.vb: Storing Values to the Session Object for Use
on Another Page
Private Sub btnSearch_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnSearch.Click

Session.Item("SearchRecordSource") = "Customers"
Session.Item("SearchField") = "CompanyName"
Session.Item("KeyField") = "CustomerID"
Session.Item("CallingPage") = "wfrmHowTo8_8a.aspx"

Server.Transfer("wfrmHowTo8_8b.aspx")

End Sub
3. Add the code in Listing 8.46 to the Load event of the Web Form. If the Session
object has an entry for ResultValue, then the LoadIndividual routine is executed
and the ResultValue is passed. The LoadIndividual routine is described in the next
step. This routine is coded so that when the page is reloaded after the search form
has been used, the Session object entry will exist. When you first come into the
page, the entry doesn't exist.
Listing 8.46 frmHowTo8_8a.vb: Loading an Individual Record into Text
Boxes on the Form
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load


If Not (Session("ResultValue") Is Nothing) Then
LoadIndividual(Session("ResultValue"))
End If

End Sub
4. Create the LoadIndividual routine by entering the code shown in Listing 8.47 in
the form. Taking the strCustID passed from the results of the search, a data adapter
is created, and a data table is filled. Last, each of the TextBox controls is loaded
with the value from the column with the corresponding name.
Listing 8.47 frmHowTo8_8a.vb: Loading an Individual Record into Text
Boxes on the Form
Private Sub LoadIndividual(ByVal strCustID As String)

Dim odaCustIndiv As New _
OleDb.OleDbDataAdapter("Select * From Customers Where CustomerID =
'" &
strCustID & "'", BuildCnnStr("(local)", "Northwind"))

Dim dtCustIndiv As New DataTable()

×