Completing an In-Text Advertising System under an ASP.NET 3.5 Environment - Manage the Ad Keywords
(Page 4 of 5 )
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
_html_2bc685cc.png)
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 />
<asp:GridView id="GridView1" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCommand="GridView1_RowCommand" AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None">
<Columns>
<asp:BoundField DataField="id" HeaderText="Code" />
<asp:BoundField DataField="name" HeaderText="Keyword" />
<asp:BoundField DataField="notes" HeaderText="Note" />
<asp:TemplateField HeaderText="delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandName ="del" runat="server" CommandArgument='<%# Bind("id") %>'>Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<br />
Keyword:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
Note:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
</div>
</form>
</body>
What about the code behind it? Below is the crucial part of the code:
protected void Page_Load(object sender, EventArgs e)
{
//not logged, then force to login first
if (Session["admin"]==null||Session["admin"].ToString() != "true") Response.Redirect("login.aspx");
if (!IsPostBack)
{
myDataBind();//data binding
}
}
//paging
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
myDataBind();
}
//delete
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "del")
{
string id = e.CommandArgument.ToString();
try
{
Sql s = new Sql();
s.ExecuteSql("delete from keyarray where id=" + id);//delete
myDataBind();
}
catch
{ }
}
}
//datasource binding
private void myDataBind()
{
Sql s = new Sql();
DataSet ds = s.getMyDataSet("select * from keyarray order by id desc");
GridView1.DataSource = ds;
GridView1.DataBind();
}
//add the ad keyword
protected void Button1_Click(object sender, EventArgs e)
{
int result;
Sql s = new Sql();
//add the ad keyword
result = s.ExecuteSql("insert into keyarray([name],notes) values('"+TextBox1.Text+"','"+ TextBox2.Text+"')");
string msg = "alert('Operation failed!')";
if (result == 1)//if peration succeeded,then prompt
{
TextBox2.Text = TextBox1.Text = "";
msg = "alert('Operation succeeded!')";
myDataBind();
}
//the client side prompt
ClientScript.RegisterStartupScript(this.GetType(), "asdf", msg, true);
}
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.
Next: Manage the Ad Contents >>
More ASP.NET Articles
More By Xianzhong Zhu