Webfrom --Repeater超链接传值
效果图:
、
添加一个主页面(shou.aspx)
将整个表显示在里面,
★ 往视图里添加一个Repeater的控件。
将修改的超链接连接了编号上,点击编号就可以进入到修改的界面。
<div> <asp:Repeater ID ="Repeater1" runat="server"> <HeaderTemplate> <table><tr><td>编号</td><td>名称</td><td>删除</td></tr> </HeaderTemplate> <AlternatingItemTemplate> <tr><td><a href="Update.aspx?code=<%#Eval("Code") %>"><%#Eval("Code") %></a></td> <td><%#Eval("Name") %></td> <td><a href="Delete.aspx?code=<%#Eval("Code") %>">删除</a></td> </tr> </AlternatingItemTemplate> <ItemTemplate> <tr bgcolor="#EEAEEE"><td ><a href="Update.aspx?code=<%#Eval("code") %>"><%#Eval("Code") %></a></td> <td><%#Eval("Name") %>></td> <td><a href="Delete.aspx?code=<%#Eval("Code") %>">删除</a></td> </tr> </ItemTemplate> <FooterTemplate> <tr> <td> <a href="insert.aspx?code=<%#Eval("code") %>">增加</a> </td> </tr> </table> </FooterTemplate> </asp:Repeater> </div>
<查看代码>
namespace 查表_新方法 { public partial class shou : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { MYDBDataContext context = new MYDBDataContext(); Repeater1.DataSource = context.Car.ToList(); Repeater1.DataBind(); } } }
(一) 连接Car表写一个操作的方法(mydb)
namespace 查表_新方法 { public class mydb { private MYDBDataContext context = new MYDBDataContext(); public List<Car> Select()//全部查询 { return context.Car.ToList(); } public bool Update(Car a)//修改 { var query = context.Car.Where(p=>p.Code==a.Code); if (query.Count()>0) { Car b = query.First(); b.Code = a.Code; b.Name = a.Name; context.SubmitChanges(); return true; } else { return false; } } public Car Select1(string code)//挨个查询 { var query = context.Car.Where(p => p.Code == code); if (query.Count() > 0) { return query.First(); } return null; } public void Delete(string code)//删除 { var query = context.Car.Where(p=>p.Code==code); if (query.Count()>0) { Car a = query.First(); context.Car.DeleteOnSubmit(a); context.SubmitChanges(); } } public void Insert(Car a)//增加 { context.Car.InsertOnSubmit(a); context.SubmitChanges(); } } }
(二) 创造一个添加的Web窗口
效果图:
《视图》
<div> <table> <tr> <td>编号:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> </tr> <tr> <td>名称:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <asp:Button ID="Button1" runat="server" Text="确认增加" OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="返回主页" /> </td> </tr> </table> </div>
《代码》
namespace 查表_新方法 { public partial class insert : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Car a = new Car(); a.Code = TextBox1.Text; a.Name = TextBox2.Text; new mydb().Insert(a); Response.Redirect("shou.aspx"); } protected void Button2_Click(object sender, EventArgs e) { Response.Redirect("shou.aspx"); } } }
(三)添加一个修改的update(update.aspx)
效果图:
《视图》
<table> <tr> <td>编号:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> </tr> <tr> <td>名称:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <asp:Button ID="Button1" runat="server" Text="修改" OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="返回主页" /> </td> </tr> </table>
<操作代码>
namespace 查表_新方法 { public partial class Update : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string s = Request["Code"].ToString(); Car a = new mydb().Select1(s); Session["car"] = a; TextBox1.Text = a.Code; TextBox2.Text = a.Name; } } protected void Button1_Click(object sender, EventArgs e) { Car a = (Car)Session["car"]; a.Code = TextBox1.Text; a.Name = TextBox2.Text; bool isok = new mydb().Update(a); if (isok) { Response.Write("<script> alert('修改成功')</script>"); } else { Response.Write("<script> alert('修改失败')</script>"); } } protected void Button2_Click(object sender, EventArgs e) { Response.Redirect("shou.aspx"); } } }
(四) 添加一个删除的Web窗口(delete.aspx)
namespace 查表_新方法 { public partial class Delete : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { new mydb().Delete(Request["code"].ToString()); Response.Redirect("shou.aspx"); } } }