All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
File.Delete不能删除文件的问题_[Asp.Net教程]
在ASP.NET中如何防范SQL注入式攻击_[Asp.Net教程]
asp.net在ie里调试_.net资料_编程技术
xml的解析处理以及其他_[XML教程]
学习:如何在HTML中引用XML数据_[XML教程]
在PHP中全面阻止SQL注入式攻击之三_[PHP教程]
ASP.NET技巧:DataGrid的多行提交_[Asp.Net教程]
asp.net2.0中Server对象的方法和属性
.NET开发Silverlight程序:界面和对象模型_.net资料_编程技术
C#,中的常用正则表达式总结_[Asp.Net教程]
Reading number is top 10 pictures
超级大兔子
Group of female porn in 《westwards》, uninhibited woman threatened to not the bottom line2
The money of more than 100 countries and regions14
西游四格漫画(六)
Sora aoi possession photo2
2012 national geographic daily picture1
Forced sex girl living abroad1
王艳写真温柔如水2
某某人向找小三的人宣战了
The little girl with long hair2
Download software ranking
WebService在.NET中的实战应用教学视频 → 第1集
Tram sex maniac 2 (H) rar bag4
Sora aoi, the nurse, uniform ,nursing assistant
Popkart Cracked versions Mobile phone games
网络管理员第三版
Prostitutes diary
Desire a peach blossom
asp.netWeb服务器高级编程
Unix video tutorial7
Be there or be square
归海一刀 published in(发表于) 2014/1/30 1:13:14 Edit(编辑)
第六篇续:动态创建,ListView,模板_[Asp.Net教程]

第六篇续:动态创建,ListView,模板_[Asp.Net教程]

第六篇续:动态创建 ListView 模板_[Asp.Net教程]

上一篇,想必大家已经喜欢上了ListView的布局方式,在LayoutTemplate中定义展现方式,在其它模板中定义数据绑定,使得我们设计数据表现页更简单了。


不过我最近在想一个问题,如果listview中要显示的列并非固定的,或者我们要在其中实现列的重新排列该怎么办呢?本篇文章将要讲述如何动态的为listview创建模板。


首先是第一种简单方式 this.ListView1.LayoutTemplate = this.LoadTemplate("MyTemplate.ascx");这是最简单的一种方式,但是相对的灵活性也不足,他只能是为我们的listview提供可预知的集中模板。并不能在用户操作的时候实现改变。那么该怎么办呢?


若要创建动态模板,就要先创建模板类,然后在需要时实例化该类。



创建实现ITemplate接口的新类,并实现接口的InstantiateIn方法。
该方法提供将文本实例或控件实例插入容器的方法。
我们以LayoutTemplate为例
public class MyLayoutTemplate : System.Web.UI.ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
PlaceHolder ph = new PlaceHolder();
Table t = new Table();
TableRow r = new TableRow();


r.Cells.Add(new TableCell() { Text = "Title1" });
r.Cells.Add(new TableCell() { Text = "Title2" });
r.Cells.Add(new TableCell() { Text = "Title3" });
t.Rows.Add(r);
TableRow itemPlaceholderRow = new TableRow();


Table itemPlaceholderTable = new Table();
itemPlaceholderTable.ID = "itemPlaceholder";
itemPlaceholderRow.Cells.Add(new TableCell());
itemPlaceholderRow.Cells[0].Controls.Add(itemPlaceholderTable);


t.Rows.Add(itemPlaceholderRow);
ph.Controls.Add(t);
container.Controls.Add(ph);
}
}
然后我们只需要创建这个类的实例并将其赋给LayoutTemplate属性就OK了。



MyLayoutTemplate myLayoutTemplate = new MyLayoutTemplate();


this.ListView1.LayoutTemplate = myLayoutTemplate;
下面我们创建ItemTemplate,在这里我们还要额外多处理下DataBinding事件。
结果代码如下
public class MyItemTemplate : System.Web.UI.ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
PlaceHolder ph = new PlaceHolder();
TableRow row = new TableRow();
row.Cells.Add(new TableCell() { ID = "cell1" });
row.Cells.Add(new TableCell() { ID = "cell2" });
ph.Controls.Add(row);
ph.DataBinding += new EventHandler(ph_DataBinding);
container.Controls.Add(ph);
}


void ph_DataBinding(object sender, EventArgs e)
{
PlaceHolder ph = (PlaceHolder)sender;
IDataItemContainer ri = (IDataItemContainer)ph.NamingContainer;
object itemValue1 = DataBinder.Eval(ri.DataItem, "字段1");
((TableCell)ph.FindControl("cell1")).Text = itemValue1.ToString();
object itemValue2 = DataBinder.Eval(ri.DataItem, "字段2");
((TableCell)ph.FindControl("cell2")).Text = itemValue2.ToString();
}
}


同样的,使用this.ListView1.ItemTemplate = new MyItemTemplate();完成ItemTemplate的编辑。


如果你愿意多花点时间,您肯定可以举一反三的搞定其它类型的模板。

来源:http://www.cnblogs.com/tianyamoon







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.