Completing an ASP.NET AJAX Server-Centric Based Online Shopping Website - Ajaxifying the Operation Module
(Page 2 of 5 )
Ajaxifying the Operation Module
The foreground operation module is handled by using the "OperationUC.ascx" UserControl which is responsible for providing common operations to the users, such as logging in, registering, product searching and advanced searching, etc. Figure 40 gives the design-time snapshot of this control.
Figure 40—the design-time snapshot for the foreground operation module
/Building_ASP.NET_AJAX_Server-Centric_Shopping_Website(11)_html_3bf88f32.png)
First, substantially, an ASP.NET UserControlis very much like a Pageobject, with a great deal of properties and methods that are quite similar. Here, when users click the "Login" button, we’ll use UpdatePanel1to display the login-related information as well as gain the local update effect.
Second, we’ve still used an ASP.NET AJAX Toolkit TextBoxWatermarkExtendercontrol to gain the watermark effect with the textbox control below the "Searching Keyword" label.
Third, we’ve leveraged UpdatePanel2to enclose the area inside which lies an ASP.NET Panelcontrol which is used later to load one of the three modules (i.e. AdminOperationUC.ascx, CustomerOperationUC.ascx, and MyOperationUC.ascx) when the users log into the system to also gain the locally updated effect.
Now, let’s take a look at the initialization of the control.
protected void Page_Load(object sender, EventArgs e){
if(Session["UserID"] != null && Session["RoleID"] != null){
OperationPanel.Controls.Clear();
if(Session["RoleID"].ToString() == "1") {
OperationPanel.Controls.Add(Page.LoadControl
("~/UserControl/AdminOperationUC.ascx"));
}
if(Session["RoleID"].ToString() == "2") {
OperationPanel.Controls.Add(Page.LoadControl
("~/UserControl/CustomerOperationUC.ascx"));
}
if(Session["RoleID"].ToString() == "3"){
OperationPanel.Controls.Add(Page.LoadControl
("~/UserControl/MyOperationUC.ascx"));
}
}
if (Session["UserName"] != null) {
logged.Text = "Welcome:" + Session["UserName"].ToString();
logged.Visible = true;
unlogged.Visible = false;
LogoutBtn.Visible = true;
LoginBtn.Visible = false;
}
}
As you can see from the code above, this function first judges whether the user has logged into the system, i.e. decides if the value of Session["UserID"]is empty. If the value of Session["UserID"]is not empty, then it means the user has already logged into the system. Therefore, the related module is loaded according to the different role of the user: if the role is "Super Administrator", the "AdminOperationUC.ascx" module is loaded, if it is "Supplier" then "CustomerOperationUC.ascx" and if "Common Customer" then "MyOperationUC.ascx."
Here since all the buttons related to event handlers are simple to follow up, we will only take a short look at the ShowMessage helper function:
private void ShowMessage(string sMsg){
//Response.Write("<script>window.alert('" + sMsg + "')</script>");
ScriptManager.RegisterStartupScript(Page, GetType(), "MyScript1",
"alert('" + sMsg +"');", true);
}
This function is invoked in the above-mentioned buttons related to event handlers. And as has been discussed in the previous sections, the reason we’ve used the RegisterStartupScript static member function of the ScriptManager controlinstead of "Response.Write(…)" is because using "Response.Write(…)" during the course of an asynchronous postback will lead to non-execution of the inner script or even a fatal script error.
Next, let’s shift our attention to the notification module.
Next: Notification Module >>
More ASP.NET Articles
More By Xianzhong Zhu