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

DEVELOPING WEB SERVICE CLIENTS |
Chapter 30 |
689 |
|
|
|
|
|
Response.Redirect (“OrdersForm.aspx?ISBN=” + strISBN + “ & Title=” + strTitle
+ “ & Author=” + strAuthor);
}
}
The preceding code uses the CommandName property in an if loop to check whether the CommandName specified for the button controls in the DataGrid control is Ord. I have discussed the CommandName property earlier in this chapter.
Inside the if loop, three string variables are declared with the names strISBN, strTitle, and strAuthor. These variables store the text in the cells of the DataGrid control. These variables are then passed as parameters to the Redirect() method of the HTTPResponse class. The page to be displayed using the Redirect() method is also passed as a parameter to the Redirect() method. In this case, the page to be displayed is the Orders form.
After adding the previously mentioned code to DispResultForm form,have a look at the complete code for the DispResultForm form. The complete code for the DispResultForm 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 WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblInfo; protected System.Web.UI.WebControls.HyperLink HyperLink1; protected System.Web.UI.WebControls.DataGrid DataGrid1;

690 |
Project 5 |
CREATING A WEB PORTAL FOR A BOOKSTORE |
|
|
|
private void Page_Load(object sender, System.EventArgs e) |
|
|
|
||
|
|
{ |
|
|
|
DTService.Service1 srv1 = new DTService.Service1(); |
|
|
|
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...”;
}

DEVELOPING WEB SERVICE CLIENTS |
Chapter 30 |
691 |
|
|
|
|
|
else
{
DataGrid1.Visible = false;
lblInfo.Text = “No matching records found!!”;
}
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!!”;
}

692 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE
break;
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;
}
}
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls
.DataGridCommandEventArgs e)
{
if(e.CommandName == “Ord”)
{
string strISBN; string strTitle; string strAuthor;
strISBN = e.Item.Cells[1].Text ; strTitle = e.Item.Cells[2].Text ; strAuthor = e.Item.Cells[3].Text;
Response.Redirect (“OrdersForm.aspx?ISBN=” + strISBN + “ & Title=” + strTitle + “ & Author=” + strAuthor);
}
}

DEVELOPING WEB SERVICE CLIENTS |
Chapter 30 |
693 |
|
|
|
|
|
}
}
Adding Code to the Search Form
The Search form prompts the user to specify criteria and the value for the criteria. After specifying the criteria and the value, the user needs to click on the Search Now button. Clicking on the Search Now button will display the matching records in the DispResultForm form. In addition, the user can select the Home button to visit the Home page for the Web site of Bookers Paradise.
In order for the Web service to return the required records, you need to track the radio button selected by the user. In addition, you need to track the value specified for the criteria. To do this, you need to add the following code to the Click event of the btnSearch button:
private void btnSearch_Click(object sender, System.EventArgs e)
{
string strText, strCriteria; strText=””;
strCriteria=””;
if(txtISBN.Text.Trim() == “” & txtAuthor.Text.Trim() ==”” & txtCategory
.Text.Trim() ==”” & txtTitle.Text.Trim() ==””)
{
lblInfo.Text =”Please enter a value!!”; return;
}
if(radISBN.Checked == true)
{
strText = txtISBN.Text; strCriteria = “ISBN Number”;
}
else if(radAuthor.Checked == true)
{
strText = txtAuthor.Text; strCriteria = “Author”;
}
else if(radCategory.Checked == true)


DEVELOPING WEB SERVICE CLIENTS |
Chapter 30 |
695 |
|
|
|
|
|
When a user selects any of the radio buttons, you need to track the radio button clicked. To do this, the Checked property of the CheckBox class is used.The Checked property returns a Boolean value. If the radio button is clicked, the value returned by the Checked property is True. Otherwise, the Checked property returns a value
False.
For the radio button that is selected, the code uses the strText variable to store the text in the corresponding text box. In addition, the criteria specified by the user is stored in the strCriteria variable. Finally, the Redirect() method is used to display the DispResultForm form. The values in the strText and strCriteria variables are passed to the Redirect() method as a parameter.
As already discussed, the Search page contains a Home button. When the Home button is clicked,the Home page of the Web site of Bookers Paradise is displayed. To add this functionality, write the following code for the Click event of the Home button:
private void btnHome_Click(object sender, System.EventArgs e)
{
Response.Redirect (“MainForm.aspx”);
}
The preceding code uses the Redirect() method to redirect the user to the MainForm form, which is the Home page in this case.
After adding the preceding code to the SearchForm page, look at the complete code for the SearchForm page.
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;

696 Project 5 CREATING A WEB PORTAL FOR A BOOKSTORE
namespace BookersClient
{
public class SearchForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.RadioButton radISBN; protected System.Web.UI.WebControls.RadioButton radAuthor; protected System.Web.UI.WebControls.RadioButton radTitle; protected System.Web.UI.WebControls.RadioButton radCategory; protected System.Web.UI.WebControls.TextBox txtISBN; protected System.Web.UI.WebControls.TextBox txtAuthor; protected System.Web.UI.WebControls.TextBox txtTitle; protected System.Web.UI.WebControls.Button btnSearch; protected System.Web.UI.WebControls.Label lblInfo; protected System.Web.UI.WebControls.Button btnHome; protected System.Web.UI.WebControls.TextBox txtCategory;
private void Page_Load(object sender, System.EventArgs e)
{
}
private void btnSearch_Click(object sender, System.EventArgs e)
{
string strText, strCriteria; strText=””;
strCriteria=””;
if(txtISBN.Text.Trim() == “” & txtAuthor.Text.Trim() ==”” & txtCategory.Text.Trim() ==”” & txtTitle.Text.Trim() ==””)
{
lblInfo.Text =”Please enter a value!!”; return;
}
if(radISBN.Checked == true)
{
strText = txtISBN.Text; strCriteria = “ISBN Number”;
}

DEVELOPING WEB SERVICE CLIENTS |
Chapter 30 |
697 |
|
|
|
|
|
else if(radAuthor.Checked == true)
{
strText = txtAuthor.Text; strCriteria = “Author”;
}
else if(radCategory.Checked == true)
{
strText = txtCategory.Text; strCriteria = “Category”;
}
else if(radTitle.Checked == true)
{
strText = txtTitle.Text; strCriteria = “Title”;
}
Response.Redirect (“DispResultForm.aspx?Cat=”
+ strCriteria + “& str=” + strText);
}
private void btnHome_Click(object sender, System.EventArgs e)
{
Response.Redirect (“MainForm.aspx”);
}
}
}
Adding Code to the Orders Form
The Orders form accepts the information about the customer who orders a book on the Web site.This information, along with the information about the book, is added to the database of the publishing house.
When the Orders page is displayed, it contains the information about the book to be ordered. To do this, add the following code to the Page_Load() method. The Page_Load() method is executed when the page is loaded at run time.
private void Page_Load(object sender, System.EventArgs e)
{
txtISBN.Text = Request.QueryString.Get(0).ToString();