
C# ПІДРУЧНИКИ / c# / Premier Press - C# Professional Projects
.pdf

DEVELOPING WEB SERVICE CLIENTS |
Chapter 30 |
679 |
|
|
|
|
|
FIGURE 30-5 The Search form
Once the form is created, you can rename the form SearchForm.
Creating the Construction Form
To create the construction form, add a hyperlink control and two label controls to the form. Rename the form ConstructionForm. Next, you need to change the properties of the controls, as shown in Table 30-5.
Table 30-5 Properties for the Controls Added to the Construction Form
Control |
Property |
Value |
Label1 |
ID |
Label1 |
|
Text |
This page is under construction. |
|
ForeColor |
Purple |
|
Font |
Verdana, Large |
Label2 |
ID |
Label2 |
|
Text |
Check out later... |
|
ForeColor |
Purple |
|
Font |
Verdana, Large |
HyperLink1 |
ID |
HyperLink1 |
|
Text |
Home |
|
NavigateURL |
MainForm.aspx |
|
|
|

680 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE
Having created the form, look at the form as shown in Figure 30-6.
FIGURE 30-6 The ConstructionForm form
Adding Code to the Web Forms
After creating the forms, add the code to the forms to make them functional.The following section discusses writing code for the Web forms that you have created.
Adding Code to the Main Form
To begin with, write the code for the Main form. Adding functionality to the Main form includes writing code for the button control in the Main form. To add the functionality to the button control, add the following code in the Click event of the button control.
private void btnGo_Click(object sender, System.EventArgs e)
string strList; string strText;
strList = lstType.SelectedItem.Text ; if(String.Compare(strList, “ALL”)==0)

DEVELOPING WEB SERVICE CLIENTS |
Chapter 30 |
681 |
|
|
|
|
|
{
strText=”Search ALL”;
}
else
{
strText = txtSearch.Text;
}
if(strText.Length != 0)
{
Response.Redirect (“DispResultForm.aspx?Cat=” + strList + “& str=” + strText);
}
else
{
Response.Redirect (“SearchForm.aspx”);
}
}
The preceding code for the Click event of the Go button declares two string type variables, strList and strText. The strList variable is used to store the value selected by the user in the list box control. To do this, you use the Text property of the ListItem class.The Text property is used to specify or retrieve values in the list box that is created. To retrieve the selected item, use the SelectedItem property of the ListControl class.
Next,the Compare() method of the String class is used to compare the value stored in the strList variable to zero. If the value stored in the variable is zero, then the text Search ALL is stored in the variable strText. However, if the value stored in the variable strList is not equal to zero, the value entered by the user in the txtSearch text box is assigned to the variable strText. Doing this helps you to store the value entered by the user for the selected criteria.
However, there may be cases where the user forgets to specify a value in the txtSearch text box. In this case, the user is taken to the SearchForm form. Otherwise, the user is taken to the DispResultForm where the records matching a given criteria are displayed. To do this, you use an if statement that checks whether the length of the value stored in the strText variable is zero or not.The length of the variable is found out by using the Length property of the String class.

682 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE
To display the form based on the result of the if statement, you can use the Redirect() method. The Redirect() method redirects the user to a new page. The URL of the resultant page is passed as a parameter to the Redirect() method.
After writing the code for the Click event of the Go button, you can see the code for the MainForm form. The code for the MainForm form is as shown:
using System;
using System.Collections; using System.ComponentModel; using System.Data;
using System.Drawing; using System.Web;
using System.Web.SessionState; using System.Web.UI;
using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
namespace BookersClient
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtSearch; protected System.Web.UI.WebControls.Button btnGo; protected System.Web.UI.WebControls.DropDownList lstType; protected System.Web.UI.WebControls.Table Table2; protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.HyperLink HyperLink1; protected System.Web.UI.WebControls.HyperLink HyperLink2; protected System.Web.UI.WebControls.HyperLink HyperLink3; protected System.Web.UI.WebControls.HyperLink HyperLink5; protected System.Web.UI.WebControls.HyperLink HyperLink4; protected System.Web.UI.WebControls.Label Label2; protected System.Web.UI.WebControls.Label Label3; protected System.Web.UI.WebControls.Label Label4; protected System.Web.UI.WebControls.Table Table1;

DEVELOPING WEB SERVICE CLIENTS |
Chapter 30 |
683 |
|
|
|
|
|
private void Page_Load(object sender, System.EventArgs e)
{
}
private void btnGo_Click(object sender, System.EventArgs e)
{
string strList; string strText;
strList = lstType.SelectedItem.Text ; if(String.Compare(strList, “ALL”)==0)
{
strText=”Search ALL”;
}
else
{
strText = txtSearch.Text;
}
if(strText.Length != 0)
{
Response.Redirect (“DispResultForm.aspx?Cat=” + strList + “& str=” + strText);
}
else
{
Response.Redirect (“SearchForm.aspx”);
}
}
}
}
Adding Code to the DispResultForm Form
When the user is taken to the DispResultForm form, the records matching the criteria specified in the MainForm page are displayed.Therefore, you need to add code to the Page_Load() method as shown:
private void Page_Load(object sender, System.EventArgs e)
{
DTService.Service1 srv1 = new DTService.Service1();

684 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE
DataSet ds1; string strCategory; string strParam;
strCategory = Request.QueryString.Get(0).ToString(); strParam = Request.QueryString.Get(1).ToString();
switch(strCategory)
{
case “ALL”:
ds1 = srv1.SearchALL(); if(ds1.Tables[“Details”].Rows.Count != 0)
{
DataView source= new DataView(ds1.Tables[“Details”]);
DataGrid1.DataSource=source;
DataGrid1.DataBind();
lblInfo.Text = “Your search produced following results”;
}
else
{
DataGrid1.Visible = false;
lblInfo.Text = “No matching records found!!”;
}
break;
case “Title”:
ds1=srv1.SrchTitle (strParam); if(ds1.Tables[“Details”].Rows.Count !=0)
{
DataView source= new DataView(ds1.Tables[“Details”]);
DataGrid1.DataSource=source;
DataGrid1.DataBind();
lblInfo.Text = “Your search produced following results...”;
}
else
{
DataGrid1.Visible = false;
lblInfo.Text = “No matching records found!!”;
}

DEVELOPING WEB SERVICE CLIENTS |
Chapter 30 |
685 |
|
|
|
|
|
break;
case “ISBN Number”: ds1=srv1.SrchISBN (strParam);
if(ds1.Tables[“Details”].Rows.Count !=0)
{
DataView source= new DataView(ds1.Tables[“Details”]);
DataGrid1.DataSource=source;
DataGrid1.DataBind();
lblInfo.Text = “Your search produced following results...”;
}
else
{
DataGrid1.Visible = false;
lblInfo.Text = “No matching records found!!”;
}
break;
case “Author”:
ds1=srv1.SrchAuthor (strParam); if(ds1.Tables[“Details”].Rows.Count !=0)
{
DataView source= new DataView(ds1.Tables[“Details”]);
DataGrid1.DataSource=source;
DataGrid1.DataBind();
lblInfo.Text = “Your search produced following results”;
}
else
{
DataGrid1.Visible = false;
lblInfo.Text = “No matching records found!!”;
}
break;

686 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE
case “Category”: ds1=srv1.SrchCategory(strParam);
if(ds1.Tables[“Details”].Rows.Count !=0)
{
DataView source= new DataView(ds1.Tables[“Details”]);
DataGrid1.DataSource=source;
DataGrid1.DataBind();
lblInfo.Text = “Your search produced following results...”;
}
else
{
DataGrid1.Visible = false;
lblInfo.Text = “No matching records found!!”;
}
break;
default: break;
}
}
The preceding code is used to declare an instance, srv1, of the DTService.Service1 class. In addition, the code declares a dataset object with the name ds1 and two
string type variables, strCategory and strParam. Next, the strCategory variable is
initialized to the QueryString value stored at the index value 0. Similarly, the strParam variable is initialized to the QueryString value stored at the index value 1.
Then, the switch case statements are used to find out the value selected by the user in the list box on the Main page. Based on this value, the records are displayed in the DispResultForm form.
First consider the case in which a user selects the All option. In this case, the object, ds1, of the dataset is used to call the SearchALL() Web method in the Web service that you created in Chapter 29. This will store all the records returned by the SearchALL() Web method in the ds1 dataset object.
However, there may be a case where there are no records to be displayed. In this case, you can display an error message to the user. To do this, you first need to

DEVELOPING WEB SERVICE CLIENTS |
Chapter 30 |
687 |
|
|
|
|
|
check whether the records in the Details data table object are equal to null or not. To find this, you can use the Count property of the InternalDataCollectionBase class. The Count property returns the total number of elements in the data table object. The value that is retuned is then equated to null. If the collection object contains records, then an object, source, of the DataView class is created and initialized to the records in the Details data table object.
Then, the DataSource property of the DataGrid object is used to specify a source for the records in the DataGrid control. In this case, the source for the records is the source object. Finally, the DataBind() method is used to bind the DataGrid control to the source object. Once the records are stored and displayed in the DataGrid control, you can display a message in the lblInfo label control. To display the text in the label control, the Text property of the control is used. Figure 30-7 shows the records displayed in the DataGrid control.
FIGURE 30-7 The records displayed in the DataGrid control
However, in the case where the Details data table object does not contain any records, the DataGrid control is made invisible and an error message is displayed in the lblInfo label control. Figure 30-8 shows an error message in the lblInfo label control.