HomeASP.NET Completing an In-Text Advertising System u...
Completing an In-Text Advertising System under an ASP.NET 3.5 Environment
In the second installment of this series, you saw the core client-side script programming and related server-side code. Now, in this third (and last) part, we will look at the programming associated with the presentation tier, i.e. the website owner- and administrator-related code as well as all of the other web pages.
Contributed by Xianzhong Zhu Rating: / 15 January 28, 2009
Website owners and system administrators are the two most necessary users. In this example, website owners have the ability to obtain the ad code on the fly to place it inside their own websites; also, if logged in, they can view their own ad data. System administrators, however, own the rights to manage the ad keywords and related detailed content. This part will introduce to you the process of logging in, the member center of the website owners, how to manage the ad keywords, and so forth.
Designing the Login.aspx Page
In this system, the Login.aspx page bears the task of handling the login both of the website owners and the system administrator. Figure 1 indicates the related running-time snapshot.
Figure 1—the login page for all the website owners and the administrators
As you’ve seen above, the layout of the login page is rather typical and simple, where a verification code is nearly a MUST HAVE in today’s website construction.
Well, let’s look into the HTML code of the login.aspx page, as shown below.
Obviously, I’ve applied the typical table layout. Looking more closely, you can see that there are only two points worth noticing. One is the following line:
The <iframe> element was frequently used in early web applications that needed to partially update a page. For details on this related programming, continue to read the next section. The other point worth noticing is this:
With this section, when you click the phrase "Refresh the verifying code," the related click event handler will be triggered to execute the related short script sentence, which will partially update the corresponding <iframe> area on the target page. For information on the use of this JavaScript, you can go here .
Next, let's take a look at the crucial code behind the Login.aspx page:
Because the website owners and the system administrator share the same login form, we have to make judgments respectively. When the user clicks the "Login" button, we first resolve the verifying code and judge whether it is correct; and if so, continue to judge whether the administrator is logging in (the correct account name and password are "qucha" and "000000" respectively). If the current user is the administrator, then he is navigated to the "admin1.aspx" page; otherwise, the website owner-related login code is executed.
If the current user has not been registered, then he is immediately registered and redirected to the member center (the siteUser.aspx page). If the current user has been registered, then his password is verified, and if all is okay, he will also be redirected to the member center (the siteUser.aspx page).
Note there is a shortcoming with the procedure above. We’ve used the hard-coded username and password, but in practical cases this is absolutely prohibited.
As hinted above, the verification code is generated through the "Gif.aspx" page. Looking closely, you will find that the HTML coding of this page is empty, and all of the implementation logic is performed by invoking the ValidateCode server-side class, with which we generate the verification code-related picture. Below I've given the code for the gif.aspx.cs file:
Response.BinaryWrite(ms.ToArray());//output in the picture format
Response.End();
}
The principle used to generate the verifying code is: first generate a random number; use this number to generate a picture; put the picture into the memory stream; and finally output the picture from the memory stream to the web page. Since the implementation of the ValidateCode class is so tedious to code, we will not list it; you can refer to the downloadable source code for the details.
The siteUser.aspx page serves as the member center for the website owner, where you can view your ad-related data, as well as obtain the ad code (which directs you as to where and how to put the code inside your own web page to advertise your products). Figure 2 indicates one of the running time snapshots of the page.
Figure 2—The siteUser.aspx page serves as the member center of the website owner
The whole layout of the page is simple, with a GridView control to display the ad- related data and a TextBox control (with the TextMode attribute set to MultiLine) to show the ad code.
Next, let’s take a quick look at the code behind the above page, as follows:
public partial class siteUser : System.Web.UI.Page
string username = User.Identity.Name;//the user account
Sql s = new Sql();
//get the user's ad popularizing related data and show them
GridView1.DataSource = s.getMyDataSet("select * from hit where username='"+username +"'");
GridView1.DataBind();
}
}
Here, if the current user has not logged in, then he is redirected to the "Login.aspx" page; otherwise, the code sets up the contents of the TextBox control with the hard-coded script, and then obtains the current user specific ad data and shows it inside the GridView control. That’s all.
Next, let’s look at the system administrator-related pages.
There are two pages associated with the system administrator: the Admin1.aspx page and Admin2.aspx. The Admin1.aspx page seeks to manipulate the ad keywords, while the Admin2.aspx page takes care of managing the related ad contents. Figure 3 gives one of the running time snapshots for the Admin1.aspx page.
Figure 3—Admin2.aspx: managing the ad contents
As you’ve seen, the "Admin1.aspx" page uses a GridView control to manage the ad keywords. In this page, you can browse, remove, and add any of your favorite ad keywords.
The following responds to the HTML code of the "Admin1.aspx" page:
<body>
<form id="form1" runat="server">
<div><a href ="admin2.aspx">Go to manage the AD contents</a><br /><br />
As you’ve seen, the system first judges whether the current user is the valid administrator with the help of ASP.NET Sesson, and if not, it redirects him to the "login.aspx" page. Next, it makes the necessary preparations for the GridView control, i.e. binds data and adds related operating buttons. Finally, when the user clicks the "Add" button, a typical database INSERT operation is triggered to append the newly-populated ad keyword-related data to the backend database. That’s it.
If you click the "Go to manage the AD contents" hyperlink at the top of the "Admin1.aspx" page, you will be navigated to the "Admin2.aspx" page, where you can manage the specified keyword-related ad contents—browse, add, and delete some items. Figure 4 indicates the running time snapshot of the "Admin2.aspx" page.
Figure 4—Admin2.aspx: managing the ad contents
As you’ve seen from the figure, there are one-to-many relationships between the ad keywords and the contents. For example, the CSS keyword is bound to two items of content: "google" and "search engine." The basic coding logic of this page quite resembles that of the "‘Admin1.aspx" page. Here we only give the server-side code related to the "Admin2.aspx" page:
In the above code, we’ve also provided enough comments. So we won’t waste any more words on details.
Summary
In-text advertising is a new form of advertising through the Internet. In this series of articles, we have constructed a simple in-text ad system. Despite its simplicity, we’ve given you the basic idea and the core routine involved in developing a real-life in-text ad application. And of course, due to its simplicity, it has numerous points that need to be perfected. For example, in practical systems, you’d better add the following supports:
1. Make the user center for the advertisers facilitate the placing of ads as well as allow them to view their own advertising effects.
2. Provide support for preventing practical fraud and spiteful clicking so as to insure the correctness of the system.
3. Add support for the on-line payment so as to facilitate the advertisers in adding ad rates for their ads online, and enable the website owners to obtain their own ad income in the online mode.
4. Categorize the websites and ad keywords. For example, you can have some specified category-related websites which can only exhibit ad keywords relating to this category. The final aim is to better the effect of the advertisement.