http://blog.csdn.net/zhang_xinxiu/article/details/21872433

想起来,公司的aspx页面前台数据展示除了datagrid以为还有Repeater控件,现在温习温习这个控件

1:// 从一个数据项中获得相应的控件
        TextBox txtTitle = (TextBox)e.Item.FindControl("txtTitle");

得记住这样获取值的方式,(控件类型)的转换

2:CheckBox chkInTheaters = (CheckBox)e.Item.FindControl("chkInTheaters");

<asp:Repeater ID="userRepeat" runat="server" OnItemCommand="userRepeat_ItemCommand" OnItemDataBound="userRepeat_ItemDataBound">

标红的是 Repeater控件命令事件
<ItemTemplate>
<tr>
<td><%#Eval("ID") %></td>
<td><%#Eval("Title") %></td>
<td><%#Eval("Cont") %></td>
<td><%#Eval("Keys") %></td>
<td><%#Eval("Des") %></td>
<td><%#Eval("AddTime") %></td>
<td>

<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Del" CommandArgument='<%#Eval("ID") %>'>删除</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update" CommandArgument='<%#Eval("ID") %>'>修改</asp:LinkButton>

<asp:Label ID="Label1" runat="server" Text='<%#Eval("NewID") %>' Visible="false"></asp:Label>
<asp:LinkButton ID="LinkButton1" runat="server" CssClass="btn btn82 btn_del" CommandName="del" OnClientClick="return confirm('要删除吗?')">删除</asp:LinkButton>
&nbsp; &nbsp; &nbsp; &nbsp;
<asp:Label ID="Label2" runat="server" Text='<%#Eval("NewID") %>' Visible="false"></asp:Label>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update" CommandArgument='<%#Eval("NewID")%>'>修改</asp:LinkButton>

前台代码通过设置控件的CommandArgument属性来传递后台所需要判断的id号。

两种不同的颜色相互对应,当点击删除时,获取"Del"的指令  然后获取到要删除数据的ID值

if (e.CommandName == "Del")
{
//string strid = ((Label)e.Item.FindControl("Label1")).Text;
// int Id = Convert.ToInt32(strid);
int id = Convert.ToInt32(e.CommandArgument.ToString());

绑定数据:

  1. /// <summary>
  2. /// 将数据源绑定Repeater控件上
  3. /// </summary>
  4. private void DataBindToRepeater() {
  5. //使用using语句进行数据库连接
  6. using (SqlConnection sqlCon=new SqlConnection("server=.;database=MyBlog;uid=sa;pwd=1"))
  7. {
  8. sqlCon.Open();  //打开数据库连接
  9. SqlCommand sqlcom = new SqlCommand();   //创建数据库命令对象
  10. sqlcom.CommandText = "select * from match"; //为命令对象指定执行语句
  11. sqlcom.Connection = sqlCon; //为命令对象指定连接对象
  12. this.userRepeat.DataSource = sqlcom.ExecuteReader();    //为Repeater对象指定数据源
  13. this.userRepeat.DataBind(); //绑定数据源
  14. }
  15. }
    1. protected void userRepeat_ItemCommand(object source, RepeaterCommandEventArgs e)
    2. {
    3. //获取命令文本,判断发出的命令为何种类型,根据命令类型调用事件
    4. if (e.CommandName=="Edit")  //编辑命令
    5. {
    6. id = int.Parse(e.CommandArgument.ToString());   //获取命令ID号
    7. }
    8. else if (e.CommandName=="Cancel")   //取消更新命令
    9. {
    10. id = -1;
    11. }
    12. else if(e.CommandName=="Delete")    //删除行内容命令
    13. {
    14. id = int.Parse(e.CommandArgument.ToString());   //获取删除行的ID号
    15. //删除选定的行,并重新指定绑定操作
    16. this.DeleteRepeater(id);
    17. }
    18. else if (e.CommandName == "Update") //更新行内容命令
    19. {
    20. //获取更新行的内容和ID号
    21. string strText = ((TextBox)e.Item.FindControl("txtName")).Text.Trim();
    22. int intId=int.Parse(((Label)e.Item.FindControl("lblID")).Text);
    23. //更新Repeater控件的内容
    24. this.UpdateRepeater(strText,intId);
    25. }
    26. //重新绑定控件上的内容
    27. this.DataBindToRepeater();
    28. }
      1. SqlCommand sqlcom = new SqlCommand();   //创建数据库命令对象
      2. sqlcom.CommandText = "update match set name=@str where id=@id"; //为命令对象指定执行语句
      3. sqlcom.Connection = sqlCon; //为命令对象指定连接对象
      4. //创建参数集合,并向sqlcom中添加参数集合
      5. SqlParameter[] sqlParam = { new SqlParameter("@str", strText), new SqlParameter("@id", intId) };
      6. sqlcom.Parameters.AddRange(sqlParam);

Repeater控件的的更多相关文章

  1. ASP.Net中通过Jquery前端对Repeater控件绑定的数据进行操作

    说明:由于Repeater控件是动态绑定,通过Id获取数据只能默认获取第一行: 1.对Repeater中div设置样式 2.通过$(".css").each(function(){ ...

  2. Repeater 控件

    Repeater 控件是一个容器控件,可用于从网页的任何可用数据中创建自定义列表.Repeater 控件没有自己内置的呈现功能,这意味着用户必须通过创建模板来提供 Repeater 控件的布局.当网页 ...

  3. WebForm(四)——Repeater控件(重要、好用)

    Repeater控件,可以用来一次显示一组数据项.比如,可以用它们显示一个数据表中的所有行.             Repeater控件完全由模板驱动,提供了最大的灵活性,可以任意设置它的输出格式. ...

  4. Repeater控件用法

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Repeater.aspx. ...

  5. Repeater控件使用中的一些小问题

    网页上用来展示列表的数据,发现还是Repeater比GridView,DetailView之类的要灵活些,所以近期用到了就总结下遇到的一些情况,保留下来以备之后查阅,不用现问度娘了... 自己摸索的, ...

  6. Repeater控件 ---表格展示数据

    简介: Repeater控件是Web 服务器控件中的一个容器控件,它使您可以从页的任何可用数据中创建出自定义列表. Repeater 控件不具备内置的呈现功能,这表示用户必须通过创建模板为 Repea ...

  7. Repeater控件使用(含删除,分页功能)

    Repeater控件使用(含删除,分页功能) 摘自:http://www.cnblogs.com/alanliu/archive/2008/02/25/914779.html 前臺代碼 <%@ ...

  8. asp.net学习之Repeater控件

    asp.net学习之Repeater控件 文章摘自:http://www.cnblogs.com/shipfi/archive/2009/10/19/1585703.html Repeater控件和D ...

  9. Webform(Repeater控件)

    一.Repeater控件 有五大模板 ItemTemplate :有多少条数据,执行多少遍        AlternatingItemTemplate : 对交替数据项进行格式设置       Se ...

  10. [ASP.NET]asp.net Repeater控件的使用方法

    asp.net Repeater控件的使用方法 -- : 4770人阅读 评论() 收藏 举报 asp.netserveraspdatasetdeletexhtml 今天学习了,Repeater控件 ...

随机推荐

  1. spring context对象

    在 java 中, 常见的 Context 有很多, 像: ServletContext, ActionContext, ServletActionContext, ApplicationContex ...

  2. tf.cast(ndarray,dtype)

    转化为指定的类型,一般是将bool类型转化为其他的数据类型,例如:tf.float32

  3. mybatis-generator-core快速生成实体类和Mapper

    日常使用Mybatis少不了和实体类和 Mapper 打交道.除了我们手写来实现,还可以使用 mybatis-generator-core 来快速生成 实体类和 Mapper. 步骤如下: 1.下载 ...

  4. NodeJS学习笔记 进阶 (4)基于express+muter的文件上传(ok)

    个人总结:这篇文章主要讲了multer插件的使用,类似于formidable,可以用来处理post表单中的文件上传,读完这篇文章需要10分钟. 摘选自网络 概览 图片上传是web开发中经常用到的功能, ...

  5. CSS display属性学习

    ---恢复内容开始--- http://www.w3school.com.cn/cssref/pr_class_display.asp 所有主流浏览器都支持 display 属性,如IE,Firefo ...

  6. CF37E Trial for Chief(最短路)

    题意 题意是给你一张 NMNMNM 的图,每个点有黑色和白色,初始全为白色,每次可以把一个相同颜色的连续区域染色,求最少的染色次数:(n,m<=50) 题解 转化为最短路.对于每一个点与它相邻的 ...

  7. cookie 实现记住用户名演示 通过代码迅速理解cookie

    // 登录页 可直接 tomcat部署 测试 1 package com.itheima.login; import java.io.IOException; import java.io.Print ...

  8. 使用SVN+Axure RP 8.0创建团队项目

    一.使用到的工具:VisualSVN Server --SVN服务器:https://www.visualsvn.com/server/ Axure RP 8.0  :http://www.downc ...

  9. STM32中断名词

    1.NVIC的优先级概念    占先式优先级 (pre-emption priority):    高占先式优先级的中断事件会打断当前的主程序/中断程序运行— —抢断式优先响应,俗称中断嵌套.    ...

  10. 基于Linux的智能家居的设计(2)

    1  系统整体设计方案 智能家居系统的是一个实时查询家庭的温湿度.照明控制.自己主动控制的设定.集家庭娱乐.智能安防为一体,大量数据快处理.可靠的系统,因此在硬件和软件上都有非常大的要求,因此在这里进 ...