购物网第一阶段总结笔记4:友情链接模块之 友情链接的查询和修改
4.友情链接Aspx页面绑定代码
<table border="1"> <tr> <th> 链接标题 </th> <th> 链接地址 </th> <th> 创建时间 </th> <th> 操作 </th> </tr> <asp:Repeater ID="rep" runat="server"> <ItemTemplate> <tr> <td> <%#Eval("linkname") %> </td> <td> <%#Eval("url") %> </td> <td> <%#Eval("createdate","{0:yyyy年MM月dd日 HH时mm分ss秒}") %> </td> <td> //传入链接名称的id来进行修改 <a href='link_add.aspx?id=<%#Eval("id") %>'>修改</a> //用LinkButton控件来做删除 <asp:LinkButton ID="lbtnDel" OnClick="Del" CommandArgument='<%#Eval("id") %>' runat="server" OnClientClick="return confirm('是否确认删除?')"> 删除 </asp:LinkButton> </td> </tr> </ItemTemplate> </asp:Repeater>
??1:友情链接的查询
aspx代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Web.admin { public partial class link_list : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { anp.RecordCount = new MyShop.DAL.LinkDAO().CalcCount(GetCond()); BindRep(); } } //sql中的where语句 private string GetCond() { string cond = ""; if (txtkey.Text.Trim().Length!=0) { string key = txtkey.Text.Trim(); cond = "linkname like '%" + key + "%' or url like '%" + key + "%'"; } return cond; } //数据控件绑定函数 private void BindRep() { replist.DataSource = new MyShop.DAL.LinkDAO().GetList("*", "createDate", "desc", anp.PageSize, anp.CurrentPageIndex, GetCond()); replist.DataBind(); } //删除友情链接 protected void Del(object sender, EventArgs e) { //利用linkButton的CommandArgument获取前台传入的参数 string id = (sender as LinkButton).CommandArgument; new MyShop.DAL.LinkDAO().Delete(int.Parse(id));//删除 //重新绑定数据 BindRep(); } //分页事件 protected void anp_PageChanged(object sender, EventArgs e) { BindRep(); } //查询 protected void btnSearch_Click(object sender, EventArgs e) { anp.RecordCount = new MyShop.DAL.LinkDAO().CalcCount(GetCond());//把cond参数传给CalcCount BindRep(); } } }
?
2:友情链接的修改
当点击修改按钮时候,链接到link_add.aspx ,然后传递给这个页面一个id,通过这个id,取得和此id相关的数据显示到页面的TextBox中。
<a href='link_add.aspx?id=<%#Eval("id") %>'>修改</a>
?然后再link_add.asp中处理修改事件:
当点击修改按钮,进入link_add.asp中的时候,如果id存在的话,就把数据加载到文本框中:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string id=Request.QueryString["id"]; int x; if (!string.IsNullOrEmpty(id)&&int.TryParse(id,out x)) { MyShop.Model.Link model = new MyShop.DAL.LinkDAO().GetModel(x); if (model!=null) { txtlinkname.Text = model.linkname; txturl.Text = model.url; btnadd.Text = "修改"; litH1.Text = "修改友情链接"; } } } }
?
修改按钮的点击事件:
if (btnadd.Text == "修改") { string id=Request.QueryString["id"]; int x; if (!string.IsNullOrEmpty(id)&&int.TryParse(id,out x)) { MyShop.Model.Link model = new MyShop.DAL.LinkDAO().GetModel(x); if (model!=null) { model.linkname = txtlinkname.Text.Trim(); model.url = txturl.Text.Trim(); model.createDate = DateTime.Now; new MyShop.DAL.LinkDAO().Update(model); txtlinkname.Text = ""; txturl.Text = ""; litmsg.Text = "<span style='color:blue'>修改成功!</span>"; return; } } }
?最终的link_add.asp的cs代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Web.admin { public partial class link_add : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string id=Request.QueryString["id"]; int x; if (!string.IsNullOrEmpty(id)&&int.TryParse(id,out x)) { MyShop.Model.Link model = new MyShop.DAL.LinkDAO().GetModel(x); if (model!=null) { txtlinkname.Text = model.linkname; txturl.Text = model.url; btnadd.Text = "修改"; litH1.Text = "修改友情链接"; } } } } protected void btnadd_Click(object sender, EventArgs e) { string name = txtlinkname.Text.Trim(); string url = txturl.Text.Trim(); //修改友情链接 if (btnadd.Text == "修改") { string id = Request.QueryString["id"]; int x; if (!string.IsNullOrEmpty(id) && int.TryParse(id, out x)) { MyShop.Model.Link model = new MyShop.DAL.LinkDAO().GetModel(x); if (model != null) { model.linkname = txtlinkname.Text.Trim(); model.url = txturl.Text.Trim(); model.createDate = DateTime.Now; new MyShop.DAL.LinkDAO().Update(model); txtlinkname.Text = ""; txturl.Text = ""; litmsg.Text = "<span style='color:blue'>修改成功!</span>"; return; } } } //增加友情链接 else { 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>"; } } } } }
?至此,友情链接模块做好。