前台:
1:利用静态页面制作母版页和首页index.aspx
2:用Repeater控件绑定数据
aspx代码:
<ul id="ul_yqlj"> <asp:Repeater ID="replink" runat="server"> <ItemTemplate> <li><a href='<%#Eval("url") %>'><%#Eval("linkname") %></a></li> </ItemTemplate> </asp:Repeater> </ul>
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { repLink.DataSource = new MyShop.DAL.link().GetList(""); repLink.DataBind(); } }
至此,友情链接前台页面显示已经弄好了。
后台:
1:新建一个文件夹admin,存放后台页面。
2:添加default.aspx,作为后台主页。
在设计视图中,利用表格布局,两行两列:
aspx代码:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .style1 { width: 100%; height: 612px; } .style2 { width: 111px; height: 479px; } .style3 { height: 58px; } .style4 { height: 479px; } </style> </head> <body> <form id="form1" runat="server"> <table class="style1" border="1px"> <tr> <td class="style3" colspan="2"> <h1>网站后台管理</h1> </td> </tr> <tr> <td class="style2" align="center" valign="top"> 友情链接管理<br /> <a href="link_add.aspx" target="frm">增加</a> </td> <td class="style4"> <iframe src="" id="frm" name="frm" style="width:100%;height:100%"></iframe> </td> </tr> </table> </form> </body> </html>
3:新建一个添加友情链接的页面link_add.aspx:
aspx代码:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> 增加友情链接<br /> <br /> 友情链接名称:<asp:TextBox ID="txtlinkname" runat="server"></asp:TextBox> <br /> 友情链接地址:<asp:TextBox ID="txturl" runat="server"></asp:TextBox> <br /> <asp:Button ID="btnadd" runat="server" Text="增加" /> </div> </form> </body> </html>
protected void btnadd_Click(object sender, EventArgs e) { string name = txtlinkname.Text.Trim(); string url = txturl.Text.Trim(); if (name.Length==0&&url.Length==0) { litmsg.Text = "<span style='color:red'>请把信息填写完整!</span>"; return;//注意,要返回,没有这一句的话,下面的代码依然会执行!! } int res = new MyShop.DAL.LinkDAO().Add(new MyShop.Model.Link() { linkname = name, url = url, createDate = DateTime.Now }); if (res > 0) { litmsg .Text= "<span style='color:blue'>增加成功!</span>"; txtlinkname.Text = ""; txturl.Text = ""; } else { litmsg.Text = "<span style='color:red'>增加失败,请联系管理员!</span>"; } }
/// <summary> /// 是否存在该记录 /// </summary> public bool Exists(int id) { Database db = DatabaseFactory.CreateDatabase(); StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) from Shop_link where id=@id "); DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()); db.AddInParameter(dbCommand, "id", DbType.Int32,id); int cmdresult; object obj = db.ExecuteScalar(dbCommand); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { cmdresult = 0; } else { cmdresult = int.Parse(obj.ToString()); } if (cmdresult == 0) { return false; } else { return true; } }
/// <summary> /// 是否存在该记录 /// </summary> public bool Exists(string name) { Database db = DatabaseFactory.CreateDatabase(); StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) from Shop_link where linkname=@linkname "); DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()); db.AddInParameter(dbCommand, "linkname", DbType.String, name);//DbType.int更改为DbType.String int cmdresult; object obj = db.ExecuteScalar(dbCommand); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { cmdresult = 0; } else { cmdresult = int.Parse(obj.ToString()); } if (cmdresult == 0) { return false; } else { return true; } }
调用这个函数,进一步完善增加友情链接:
protected void btnadd_Click(object sender, EventArgs e) { string name = txtlinkname.Text.Trim(); string url = txturl.Text.Trim(); MyShop.DAL.LinkDAO dao = new MyShop.DAL.LinkDAO(); //验证输入框是否为空 if (name.Length==0&&url.Length==0) { litmsg.Text = "<span style='color:red'>请把信息填写完整!</span>"; return;//注意,要返回,没有这一句的话,下面的代码依然会执行!! } if (dao.Exists(name)) { litmsg.Text = "<span style='color:red'>此超链接已经存在!</span>"; return; } int res = dao.Add(new MyShop.Model.Link() { linkname = name, url = url, createDate = DateTime.Now }); if (res > 0) { litmsg .Text= "<span style='color:blue'>增加成功!</span>"; txtlinkname.Text = ""; txturl.Text = ""; } else { litmsg.Text = "<span style='color:red'>增加失败,请联系管理员!</span>"; } }