Repeater 模板中查找子控件
前言:对于Repeater控件,相信从事NETWeb开发的同仁们再熟悉不过了。因其呈现方式和Literal一样,并不在前端生成任何表单标签元素,所以属于比较轻量级的控件。不过青睐于Repeater的主要原因还是其表单布局的自由性和易控性。下面就来总结一下使用Repeater常用的功能:子控件的查找。
一、在ItemTemplate和AlternatingItemTemplate中查找
这个估计大家应该都知道,不过这里还是要列一下,代码如下:
RepeaterItemCollection ReItems = Repeater1.Items;
foreach (RepeaterItem item in ReItems)
{
Label lbl = (Label)item.FindControl("Label1");//以查找Label为例,下同
}
这里可能有人要说了,是不是需要加Repeater1.Items的类型判断,如下判断:
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
foreach (RepeaterItem item in ReItems)
{
Label lbl = (Label)item.FindControl("Label1");
}
}
在这我想说加判断没任何问题,但是不加也不会有问题,因为Repeater1.Items就是这两种类型(注意:如果是通过其他方式查找,则可能必须加此判断)。
二、在HeaderTemplate和FooterTemplate中查找
根据上面提到的,通过Repeater1.Items集合肯定是找不到了,那怎么找呢?我们知道Repeater是数据控件,是可以包含子控件的,那么自然也就有Controls属性,没错,就是他。
在HeaderTemplate中找
Label lbl = (Label)Repeater1.Controls[].Controls[].FindControl("Label1");
在FooterTemplate中找
Label lbl2 = (Label)Repeater1.Controls[Repeater1.Controls.Count - ].Controls[].FindControl("Label1");
三、在SeparatorTemplate中查找
这个答案下面找
RepeaterItem item; Label lbl; string msg="";
for (int cnt = Repeater1.Controls.Count, i = ; i < cnt; i++)
{
item = (RepeaterItem)Repeater1.Controls[i];
lbl = (Label)item.FindControl("Label1");
if (lbl == null) continue;
switch (item.ItemType)
{
case ListItemType.Header: msg = "Label在Header中"; break;
case ListItemType.Item: msg = "Label在Item" + i + "中"; break;
case ListItemType.AlternatingItem: msg = "Label在AlternatingItem " + i + "中"; break;
case ListItemType.Separator: msg = "Label在Separator" + i + "中"; break;
case ListItemType.Footer: msg = "Label在Footer中"; break;
default: break;
}
ScriptManager.RegisterStartupScript(this, this.GetType(), "" + i, "alert('" + msg + "');", true);
}
四、使用C#3.0的新的语言功能:扩展方法
说明:此处不为讲解扩展方法,直接上代码了,并且都配了详细的使用例子,如果有不懂得地方可以MSDN或google
1.扩展方法
说明:命名空间和类名根据实际需要自己定义
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
namespace SignUp.Web.App_Code
{
/// <summary>
/// 扩展方法
/// </summary>
public static class NetExtensionFunctions
{
/// <summary>
/// 查找控件下的后代控件
/// </summary>
/// <param name="ctrl"></param>
/// <returns></returns>
public static IEnumerable<Control> GetChildren(this Control ctrl)
{
var children = ctrl.Controls.Cast<Control>();
return children.SelectMany(GetChildren).Concat(children);
}
/// <summary>
/// 查找控件下指定类型的后代控件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ctrl"></param>
/// <returns></returns>
public static List<T> FindControlsByType<T>(this Control ctrl)
{
return ctrl.GetChildren().OfType<T>().ToList<T>();
}
/// <summary>
/// 查找控件下符合Predicate条件的后代控件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ctrl"></param>
/// <param name="where"></param>
/// <returns></returns>
public static List<T> FindControlsByType<T>(this Control ctrl, Predicate<T> where)
{
return ctrl.GetChildren().OfType<T>().ToList<T>().FindAll(where);
}
}
}
2.使用扩展方法
注意:必须在使用的地方引用扩展方法所在的命名空间
using SignUp.Web.App_Code;
2.1使用GetChildren()扩展方法
IEnumerable<Control> controls = Repeater1.GetChildren();
foreach (Control ctrl in controls)
{
if (ctrl is Label && ctrl.ID == "Label1")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('找到了Label1');", true);
break;
}
}
2.2使用扩展方法:FindControlsByType<T>(this Control ctrl)
List<RepeaterItem> repeaterItems = Repeater1.FindControlsByType<RepeaterItem>();
foreach (RepeaterItem item in repeaterItems)
{
if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
{
List<Label> Labels = item.FindControlsByType<Label>();
//do others...
}
}
2.3使用扩展方法:FindControlsByType<T>(this Control ctrl, Predicate<T> where)
List<RepeaterItem> repeaterItems = Repeater1.FindControlsByType<RepeaterItem>(x => (x.ItemType == ListItemType.Item || x.ItemType == ListItemType.AlternatingItem));
foreach (RepeaterItem item in repeaterItems)
{
List<Label> Labels = item.FindControlsByType<Label>();
//do others...
}
总结:对于Repeater相信已是老生常谈,这里我想需要留意的应该是扩展方法的应用。虽然代码量不多,但知识点可不少。如要很熟练的编写和使用扩展方法,至少要熟悉Linq编程,对泛型有较深入的理解,还有对Lambda和Delegate熟练的应用等。最后希望本文对大家有所帮助。
补充纠错:在扩展方法3中,使用了C#上下文关键字Where作为参数名,确实不宜!(感谢alert(dong)在评论中的指出);另外这位朋友还指出扩展方法里没有对NULL进行控制,下面给出两个控制
1.如果为NULL返回空集合(使用中只怕会混淆)
if (object.Equals(null, ctrl))
return new List<Control>().AsEnumerable<Control>();
2.如果为NULL抛出异常(略显多余,NET本就会引发异常)
if (object.Equals(null, ctrl))
throw new ArgumentNullException("Control", "未将对象引用设置到对象实例");
3.启用Try Catch机制(还是抛出异常的好)
4.还请浏览过本贴的大侠,雁过留声,给予指点。
Repeater 模板中查找子控件的更多相关文章
- 初步探讨WPF的ListView控件(涉及模板、查找子控件) - GavinJun
本文结合模板的应用初步介绍ListView的应用 一.Xaml中如何建立数据资源 大部分数据都会来自于后台代码,如何Xaml同样的建立数据源呢?比如建立一个学生List: 首先引入命名空间: xmln ...
- 初步探讨WPF的ListView控件(涉及模板、查找子控件)
本文结合模板的应用初步介绍ListView的应用 一.Xaml中如何建立数据资源 大部分数据都会来自于后台代码,如何Xaml同样的建立数据源呢?比如建立一个学生List: 首先引入命名空间: xmln ...
- 【转】WPF查找子控件和父控件方法
一.查找某种类型的子控件,并返回一个List集合 public List<T> GetChildObjects<T>(DependencyObject obj, Type ty ...
- WPF查找子控件和父控件方法
一.查找某种类型的子控件,并返回一个List集合 public List<T> GetChildObjects<T>(DependencyObject obj, Type ty ...
- JS获取用户控件中的子控件Id
用户控件 <asp:HiddenField ID="hfGradeId" runat="server" /> <asp:HiddenField ...
- Activity中获取当前Fragment 中的子控件
XXXAdapter中 增加 public Fragment currentFragment; @Override public void setPrimaryItem(ViewGroup conta ...
- WPF 列表控件中的子控件上下文绑定
<DataGrid Grid.ColumnSpan=" Height="Auto" SelectedItem="{Binding Path=SelectP ...
- pyqt5-复合控件中的子控件
天子骄龙
- 【WPF】查找父/子控件(元素、节点)
整理一下项目中常用的找控件功能,包括找父/子控件.找到所有同类型子控件(比如ListBox找到所有Item). using System; using System.Collections.Gener ...
随机推荐
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(19)-用户信息的修改和浏览
原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(19)-用户信息的修改和浏览 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1):框架搭建 ...
- LeetCode: Best Time to Buy and Sell Stock II [122]
[题目] Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- 泛型方法动态生成表达式树 Expression
public string GetGridJSON(TraderInfo model) { IQueryable<TraderInfo> Temp = db.TraderInfo; if ...
- 用了TextMate才知道什么叫神级Editor
用了TextMate才知道什么叫神级Editor 一直用Eclipse作为开发Ruby和Java项目的IDE,但是太耗内存,再开个Firefox和虚拟机就可以直接将MBP弄残了..看到大家都对Mac下 ...
- asp.net 给按钮 增加事件
一个页面,有查询,审核,删除,取消审核 按钮,每次结尾 处都要 调用 Initdata方法,重新刷新数据 繁琐哇,我的解决方法是 protected void Page_Load(object sen ...
- The Swift Programming Language-官方教程精译Swift(6)控制流--Control Flow
Swift提供了类似C语言的流程控制结构,包括可以多次执行任务的for和while循环,基于特定条件选择执行不同代码分支的if和switch语句,还有控制流程跳转到其他代码的break和continu ...
- 基于tomcat为了应对高并发模型实现webserver
在博客上,一个简单的AIOweb来样加工.查看AIO异步处理,依靠操作系统完成IO操作Proactor处理模型确实很强大,它可以实现高并发.高响应server一个很好的选择,但在tomcat中间con ...
- struts2 通配符简化配置
在struts映射中反复出现的模式 动作方法 描写叙述 下一个动作方法 add 为save准备网页 save save 提交INSERT list edit 为update准备网页 update up ...
- PHP程序猿必备的七种武器
作为一个程序猿(又或者是程序媛),出来闯荡江湖,没有几种必杀技在手,那是肯定无法赢得江湖名声的,除了必杀技之外,武器也是很重要的,但是一把青钢剑,肯定是无法赢得万千少男少女的欢心的.就连小李飞刀,人家 ...
- js 文字预写匹配
效果图: demo如下: <!DOCTYPE html> <html> <head> <title>文字预写</title> </he ...