案例:模拟购物列表
封装实体类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 ///7 /// gouwu 的摘要说明 8 /// 9 public class gouwu10 {11 public int ids { get; set; }12 public string pic { get; set; }13 public string name { get; set; }14 public decimal nowPrice { get; set; }15 public decimal oldPrice { get; set; }16 public string context { get; set; }17 18 public string qx {19 get20 {21 string eee = "";22 if (this.name == "猕猴桃")23 {24 eee = " display:none;";25 }26 27 return eee;28 }29 }30 31 }
数据访问类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Data.SqlClient; 6 7 ///8 /// gouwuData 的摘要说明 9 /// 10 public class gouwuData11 {12 SqlConnection conn = null;13 SqlCommand cmd = null;14 public gouwuData()15 {16 conn = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123");17 cmd = conn.CreateCommand();18 }19 20 public ListSelect()21 {22 List glist = new List ();23 cmd.CommandText = "select *from gouwu";24 25 conn.Open();26 SqlDataReader dr = cmd.ExecuteReader();27 if (dr.HasRows)28 {29 while (dr.Read())30 {31 gouwu g = new gouwu();32 g.ids = Convert.ToInt32(dr[0]);33 g.pic = dr[1].ToString();34 g.name = dr[2].ToString();35 g.nowPrice = Convert.ToDecimal(dr[3]);36 g.oldPrice = Convert.ToDecimal(dr[4]);37 g.context = dr[5].ToString();38 39 glist.Add(g);40 41 }42 }43 44 45 conn.Close();46 47 return glist;48 }49 50 51 public void Delete(int ids)52 {53 cmd.CommandText = "delete from gouwu where ids='" + ids + "'";54 55 conn.Open();56 cmd.ExecuteNonQuery();57 conn.Close();58 }59 60 61 }
用Repeater展示:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 2 3 4 5 6 7 8105 1069 83 84 85 86
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 public partial class _Default : System.Web.UI.Page 9 {10 protected void Page_Load(object sender, EventArgs e)11 {12 if (!IsPostBack)13 {14 Repeater1.DataSource = new gouwuData().Select();15 Repeater1.DataBind();16 }17 //点击Repeater1中的按钮时发生18 Repeater1.ItemCommand += Repeater1_ItemCommand;19 }20 21 void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)22 {23 if (e.CommandName == "Delete")24 {25 new gouwuData().Delete(Convert.ToInt32(e.CommandArgument));26 27 Repeater1.DataSource = new gouwuData().Select();28 Repeater1.DataBind();29 }30 }31 }
不用Repeater展示:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> 2 3 4 5 6 7 893 949 79 80 81 82
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 public partial class Default2 : System.Web.UI.Page 9 {10 protected void Page_Load(object sender, EventArgs e)11 {12 if (!IsPostBack)13 {14 Literal1.Text = DataBind();15 }16 }17 public string DataBind()18 {19 string end = "";20 Listglist = new gouwuData().Select();21 foreach (gouwu g in glist)22 {23 if(g.name=="香蕉")24 {25 continue;26 }27 end += " ";28 end += " ";29 end += "";34 }35 36 return end;37 }38 }" + g.name + "";30 end += "价格:" + g.nowPrice + " " + g.oldPrice + "";31 end += "" + g.context + "";32 end += " 删除";33 end += "
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 public partial class Delete : System.Web.UI.Page 9 {10 protected void Page_Load(object sender, EventArgs e)11 {12 new gouwuData().Delete(Convert.ToInt32(Request["id"]));13 Response.Write("");14 }15 }