Adding Comments and Search to an ASP.NET AJAX Server-Centric Based Online Shopping Website
(Page 1 of 4 )
In real scenarios, an online shopping city must support the users to make free comments on the goods as well as leave suggestions to help the city owners to further improve their services. In this example in the sixth part of an eleven-part series, we have simulated these functionalities. We also set up the search functions.
A
downloadable .rar file is available for this article.
Making comments on the Products
This function is performed through the "comment.aspx" page. Lets take a quick look at the related design-time snapshot, as illustrated in the following Figure 19.
Figure 19the design-time snapshot for making comments
/Building_ASP.NET_AJAX_Server-Centric_Shopping_Website(6)_html_71678b37.png)
Apparently, this is a simple yet representative design concerning the ASP.NET AJAX server-centric pages. At the top lies the ScriptManager controlto schedule the relations between ASP.NET and the MS AJAX framework itself. The middle part is a GridView circled by the UpdatePanel controlto gain the partial update effect as the GridView control changes its contents. The lower part is two TextBox controls for the user to populate with his comments. At the bottom are two buttons: "Submit my comment" for submitting the comment and "Close" for closing the current browser window.
There are two points here worth pointing out. First, we purposely omit the utilization of ASP.NET AJAX server control namedUpdateProgessand the ASP.NET AJAX Control Toolkit Extender control namedUpdatePanelAnimation,both here and in the rest of this sample, partially to save writing time. Second, despite the scope of the UpdatePanel controlwe can still not securely use the sentence we mentioned earlier: Response.Write(<script>
</script>);.
Now, lets omit the analysis of binding data to the GridView "CommentView," but leave room for submitting the comment. Clicking the "Submit my comment" button will trigger the SureBtn_Click event handler. The following shows the related code:
protected void SureBtn_Click(object sender,EventArgs e){
if(Session["UserID"] == null) {
ScriptManager.RegisterStartupScript(up1, typeof(UpdatePanel),
"ScriptName1", "alert('Not logged in. Please login first!')", true);
return;
}
Comment comment = new Comment();
comment.AddComment(Desn.Text,Body.Text,nProductID,Int32.Parse(Session
["UserID"].ToString()));
///rebind data
BindCommentData(nProductID);
}
protected void ReturnBtn_Click(object sender,EventArgs e){
Response.Write("<script>window.close();</script>");
}
Note here in the SureBtn_Click function, we still use the static method named RegisterStartupScriptof ScriptManagerto report to the user an error message instead of using the Response.Write(<script>
</script>);sentence, or else you will surely meet the error shown in the following Figure 20.
Figure 20the typical error message when Response.Write(
) conflicts with UpdatePanel
/Building_ASP.NET_AJAX_Server-Centric_Shopping_Website(6)_html_28375429.png)
In contrast, in the event handler for the "ReturnBtn" button, we have used Response.Write("<script>window.close();</script>");. To our astonishment, there is nothing wrong and the page closes in the normal way. Why? According to the result of my simple study, if you use Response.Write()to directly write JavaScript functions such as alert()to the page during the course of a asynchronous postback, these functions often do not run as expected and even result in some error. Therefore, in the first function above if we use Response.Write() instead of ScriptManager.RegisterStartupScript(
), then the asynchronous postback action triggered by the subsequent sentences will of course lead to the error in Figure 20. However, in the second function, there is only one simple sentence Response.Write(), this of course will not result in an error occurring.
Next: Leaving Words >>
More ASP.NET Articles
More By Xianzhong Zhu