Repeater与GridView等数据列表一样,都是用来显示数据库的信息的,其中Repeater是最基本的列表形式,其用法也比较灵活。

一、Repeater的位置:

工具箱-数据-Repeater

拖拽进入页面后的显示:

切换到源视图会发现他只有两句代码:

<asp:Repeater ID="Repeater1" runat="server">
</asp:Repeater>

其他的格式等需要我们用代码来实现。

二、Repeater基本列表格式:

<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate><!--头部列表的标签-->
<table>
<tr>
<td>地名</td>
<td>邮编</td>
<td>管理</td>
<td>删除</td>
</tr>
</HeaderTemplate>
<ItemTemplate><!--表主题内容标签—>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</ItemTemplate>
<FooterTemplate><!--表的脚部标签-->
</table>
</FooterTemplate>
</asp:Repeater>

表头需要写入<HeaderTemplate>..</HeaderTemplate>标签中,即表的第一行放在此标签中。

主要数据放在<ItemTemplate>..</ItemTemplate>标签中,即表的主要数据内容的显示,只需要一行即可,绑定数据后系统会自动生成其他的行。

表的尾部<FooterTemplate>..</FooterTemplate>标签,尾部注解等。

三、Repeater的数据绑定。

需要先在HTML的页面中指定绑定的表的列名

(还需要创建LINQ to SQL的类,来连接数据库,附数据库表图,只需要类似的表即可)

使用<%#Eval("列名") %>,格式如下(这里只绑定了两个列):

            <ItemTemplate>
<tr>
<td><%#Eval("Name") %></td>
<td><%#Eval("PostCode") %></td>
<td></td>
<td></td>
</tr>
</ItemTemplate>

成功绑定后在设计视图中的显示:

cs中的代码:

    private DiquDataContext diqu;//外部定义LINQ方便使用
protected void Page_Load(object sender, EventArgs e)
{
diqu = new DiquDataContext();//初始化LINQ
if (!IsPostBack)//第一次加载时
{
BindData();
}
}
//创建了一个数据绑定的方法以方便使用
public void BindData()
{
//按条件查询数据
var que = from m in diqu.Members where m.ParentId.ToString().Length == select m;//按字符个数确定省级地区
//绑定数据
Repeater1.DataSource = que;
Repeater1.DataBind();
}

绑定完成:

四、Repeater的编辑和删除按钮

注意:由于无法获取到已显示的repeater的值,所以我们需要在点击上一级或下一级时使用一个Label标签来记录它点击的是哪一行,然后只需要将Label隐藏即可。

在表格中相应的位置添加LinkButton控件,然后给控件设置 CommandName(为控件设置名字)  CommandArgument(绑定数据,索引)

            <ItemTemplate>
<tr>
<td><%#Eval("Name") %></td>
<td><%#Eval("PostCode") %></td>
<td>
<asp:LinkButton ID="LinkButton_guanli" CommandArgument='<%#Eval("ParentId") %>' CommandName="xiaji" runat="server">下级管理</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton_shanchu" CommandArgument='<%#Eval("ParentId")  OnClientClick='return confirm("确定删除吗?")' %>' CommandName="shanchu" runat="server">删除</asp:LinkButton>
</td>
</tr>
</ItemTemplate>

在Repeater控件的属性--事件—ItemCommand(单击Repeater任意按钮时触发),双击自动生成事件

cs中的代码:

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//删除
if (e.CommandName == "shanchu")//如果点击的是删除按钮
{//表中ParentId列的数据类型是int
int code = int.Parse(e.CommandArgument.ToString());
Members cdata = diqu.Members.Single(r => r.ParentId == code);
diqu.Members.DeleteOnSubmit(cdata);
diqu.SubmitChanges();
binddata();
}
//展示下级
if (e.CommandName == "xiaji")
{
int cou = e.CommandArgument.ToString().Length;//获取字符串长度,以判断省市地区的级别
if (cou == )//省或者直辖市为两位数
{
Repeater1.DataSource = null;//清空数据
string code = e.CommandArgument.ToString();
//按照字符长度并截取字符串进行匹配(搜索的位置是 市)
var xia = from m in diqu.Members where m.ParentId.ToString().Length == where m.ParentId.ToString().Substring(, ) == code select m;
Repeater1.DataSource = xia;
Repeater1.DataBind();
}
if (cou == )//市 为4位数
{
Repeater1.DataSource = null;//清空数据
string code = e.CommandArgument.ToString();
//按照字符长度并截取字符串进行匹配(搜索的位置是 地区)
var xia = from m in diqu.Members where m.ParentId.ToString().Length == where m.ParentId.ToString().Substring(, ) == code select m;
Repeater1.DataSource = xia;
Repeater1.DataBind();
}
if (cou == )//地区 为6位数,最小级别
{
//弹出提示信息
Response.Write("<script>alert('已到最小级别!');</script>");
}
}
}

20150301—ASP.NET的Repeater的更多相关文章

  1. asp.net中Repeater控件用法笔记

    大家可能都对datagrid比较熟悉,但是如果在数据量大的时候,我们就得考虑使用 repeater作为我们的数据绑定控件了.Repeater控件与DataGrid (以及DataList)控件的主要区 ...

  2. asp控件Repeater运用

    双层repeater嵌套 <asp:Repeater ID="rpt_dataRepeatgroup" runat="server" OnItemData ...

  3. Asp.Net:Repeater 详情 备用

    页面 repeator就想for循环一样,没有编辑模板,有删除delete和详情detail模板 <%@ Page Language="C#" AutoEventWireup ...

  4. ASP.NET(C#)--Repeater中生成“序号”列

    需求介绍:在Repeater(Table)中加入“序号”列,从1开始自增,步长为1. 思路:因为“序号”跟Repeater的行号有关,所以要在Repeater的ItemDataBound事件中输出“序 ...

  5. asp.net:repeater嵌套(常用于新闻等在首页归类显示)

    using System;using System.Configuration;using System.Collections.Generic;using System.Linq;using Sys ...

  6. asp.net关于Repeater控件中的全选,批量操作

    今天在Repeater控件中碰到一个全选的操作,于是上网查了一下,找到一个觉得比较好,便记录下来, 界面代码简化之后(全选操作): <script type="text/javascr ...

  7. asp.net 将repeater上数据导出到excel

    1,首先得到一个DataTable public DataTable GetTable(string sql) { SqlConnnection con=new SqlConnection(Confi ...

  8. asp.net中Repeater结合js实现checkbox的全选/全不选

    前台界面代码: <input name="CheckAll" type="checkbox" id="CheckAll" value= ...

  9. 自己写的一个ASP.NET服务器控件Repeater和GridView分页类

    不墨迹,直接上代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...

随机推荐

  1. Gridview中绑定DropDownList

    1.页面代码                         <asp:TemplateField HeaderText="等级">                   ...

  2. JavaScript实现http地址自动检测并添加URL链接

    一.天生我材必有用 给http字符自动添加URL链接是比较常见的一项功能.举两个我最近常用到的自动检测http://地址并添加链接的例子吧,首先是QQ邮箱,在使用QQ邮箱时,如果输入了URL地址(ht ...

  3. Golang学习 - 学习资源列表

    Golang 学习资源: <Go 语言圣经(中文版)>  - 书籍 http://shinley.com/index.html <学习 Go 语言> - 书籍 http://w ...

  4. Python编码相关文章推荐

    Table of Contents 1. 分享 分享 python2里面编码对很多人来说是个很头疼的问题,今天分享一篇编码相关的文章,是前几天读到的相比于大部分解释编码问题的一知半解模糊不清,这篇写得 ...

  5. LeetCode9 Palindrome Number

    题意: Determine whether an integer is a palindrome. Do this without extra space.  (Easy) 分析: 自己考虑的方法是利 ...

  6. iOS缓存框架-PINCache解读

    文/Amin706(简书作者)原文链接:http://www.jianshu.com/p/4df5aad0cbd4著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 在项目中总是需要缓存一 ...

  7. 使用post方式提交数据

    post提交代码 public class MainActivity extends Activity { @Override protected void onCreate(Bundle saved ...

  8. Excel两行交换及两列交换,快速互换相邻表格数据的方法

    经常使用办公软件的人可能有遇到过需要将Excel相邻两行数据相互交换的情况,需要怎么弄才最方便呢?您还是像大家通常所做的那样先在Excel文件相应位置插入一个新的空白行然后在复制粘贴数据然后删除原来那 ...

  9. Linux重定向的理解

    /* 重定向的实例 dup2函数 利用filefd来代替STDOUT(标准输出流),write写入filefd的数据,重定向写出到STDOUT中: */ #include <stdio.h> ...

  10. python 基础——generate生成器

    通过列表表达式可以直接生成列表,不过列表一旦生成就需要为所有元素分配内存,有时候会很消耗资源. 所以,如果列表元素可以按照某种算法推算出来,这样就不必创建完整的list,从而节省大量的内存空间. 在P ...