博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Webform Repeater的灵活运用
阅读量:7174 次
发布时间:2019-06-29

本文共 5742 字,大约阅读时间需要 19 分钟。

案例:模拟购物列表

封装实体类:

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 }
View Code

数据访问类:

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 List
Select()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 }
View Code

用Repeater展示:

1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  2   3   4   5   6   7     
8 9 83 84 85 86
87
88
89
90
91
92
" /> 93
<%#Eval("name") %>
94
价格:<%#Eval("nowPrice") %>
<%#Eval("oldPrice") %>
95
<%#Eval("context") %>
96
97
98
99
100
101
102 103
104
105 106
界面:
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     
8 9 79 80 81 82
83
84
85 86
87 88
89
90 91
92
93 94
界面
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         List
glist = new gouwuData().Select();21 foreach (gouwu g in glist)22 {23 if(g.name=="香蕉")24 {25 continue;26 }27 end += "
";28 end += "
";29 end += "
" + g.name + "
";30 end += "
价格:" + g.nowPrice + "
" + g.oldPrice + "
";31 end += "
" + g.context + "
";32 end += "
删除";33 end += "
";34 }35 36 return end;37 }38 }
后台
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 }
删除界面后台

 

转载于:https://www.cnblogs.com/maxin991025-/p/6261662.html

你可能感兴趣的文章
《Kotin 极简教程》第7章 面向对象编程(OOP)(1)
查看>>
Chrome吃内存的能力可不是说着玩的!
查看>>
iStaing获500万美元投资,VR室内设计离我们还远吗?
查看>>
Java日志框架-Spring中使用Logback(Spring/Spring MVC)
查看>>
读书笔记--101个shell脚本 之#12--函数
查看>>
TCP/IP之(四)Delay ack 和 Nagle算法
查看>>
linux学习:selinux 禁用后(disabled)Linux系统无法正常启动解决
查看>>
关于tomcat和jetty对比(不喜欢jetty的勿看)
查看>>
grafana使用详解
查看>>
linux 文件同步 rsync+crontab
查看>>
git如何删除远程仓库的某次错误提交
查看>>
LAMP架构讲解(续一)
查看>>
linux下查看文件编码及修改编码
查看>>
VC中的延时
查看>>
算法与数据结构知识点
查看>>
在单位成功实验的PIX配置
查看>>
centos6.x使用dd命令制作u盘启动
查看>>
如何使用Wireshark抓包
查看>>
mysql 时间函数用法 集合
查看>>
技术宅男既要提升编程技术也要加强沟通能力
查看>>