用户控件和服务器控件的数据绑定
2011-10-05 17:50
316 查看
一、绑定Repeater控件的数据源
aspx.cs文件中绑定Repeater控件的数据源在BindDataSource()中:
protectedoverridevoidBindDataSource() { this.rpID.DataSource=this.dataList; this.rpID.DataBind(); }
Repeater控件事件OnItemDataBound,表示在循环加载<ItemTemplate>列表时候,会对每一项Item进行具体的操作。 例子:
Protectedvoidrp_ItemDataBound(objectsender,RepeaterItemEventArgse) { if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.Item) { List<Info>Infos=this.rpID.DataSourceasList<Info>; Infoinfo=e.Item.DataItemasInfo;
Literallt=e.Item.FindControl("lt")asLiteral;
lt.Text=Info.Title; } }
可以看出来对于Repeater控件中的<ItemTemplate>里面的用户控件、服务器控件的赋值是在Repeater控件事件OnItemDataBound中进行的。
二、用户控件
用户控件T.ascx代码有:
字段: privatestringtitle;
属性: publicstringTitle { get { returnthis.title; } }
方法: SetTitle() { this.title="Helloworld!"; } 其实这里的字段赋值也是可以用属性进行赋值的,不过要在属性声明中加上set部分。 那么这个T的用户控件的要是在Repeater控件中的<ItemTemplate>里面出现的话,是要在 Repeater控件事件OnItemDataBound里面进行具体的赋值的。 例子: Protectedvoidrp_ItemDataBound(objectsender,RepeaterItemEventArgse) { if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.Item) { List<Info>Infos=this.rpID.DataSourceasList<Info>; Infoinfo=e.Item.DataItemasInfo;
Tt=e.Item.FindControl("t")asT;
t.SetTitle(info.Title); } } 可以看到上面的操作已经把数据存到title字段里面了,在用户空间的T.ascx代码中用<%=Title%>来访问Title属性, 因为之前已经声明了Title属性的get部分。
[b]三、一个简单的Reapter控件的使用全貌[/b]
在aspx页面中:
<asp:RepeaterID="rpID"runat="server"OnItemDataBound="rp_ItemDataBound"> <ItemTemplate> <asp:LiteralID="lt"runat="server"></asp:Literal> </ItemTemplate> </asp:Repeater>
在aspx.cs页面中:
protectedoverridevoidBindDataSource() { this.rpID.DataSource=this.dataList; this.rpID.DataBind(); }
Protectedvoidrp_ItemDataBound(objectsender,RepeaterItemEventArgse) { if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.Item) { List<Info>Infos=this.rpID.DataSourceasList<Info>; Infoinfo=e.Item.DataItemasInfo;
Literallt=e.Item.FindControl("lt")asLiteral;
lt.Text=Info.Title; } }
注意我前面已经提到了服务器控件和用户控件在这里面的使用方式其实是一摸一样的。
可以看出来BindDataSource()是对aspx页面中最外层的用户控件、服务器控件的数据源进行绑定,
而OnItemDataBound事件是对Repeater控件中的<ItemTemplate>里面的用户控件、服务器控件的数据源
进行绑定。
[b]四、在Repeater控件中嵌套使用Repeater控件[/b]
这其实是一个特例: 在aspx页面中:
<asp:RepeaterID="rpID"runat="server"OnItemDataBound="rp_ItemDataBound"> <ItemTemplate> <asp:RepeaterID="rp_rpID"runat="server"> <asp:LiteralID="lt"runat="server"></asp:Literal> </asp:Repeater> </ItemTemplate> </asp:Repeater>
在aspx.cs页面中:
protectedoverridevoidBindDataSource() { this.rpID.DataSource=this.dataList; this.rpID.DataBind(); }
Protectedvoidrp_ItemDataBound(objectsender,RepeaterItemEventArgse) { if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.Item) { List<Info>Infos=this.rpID.DataSourceasList<Info>; Infoinfo=e.Item.DataItemasInfo;
Repeaterrp_rpID=e.Item.FindControl("rp_rpID")asRepeater;
rp_rpID.DataSource=dataList;
rp_rpID.DataBind(); } }
[b]五、一般用户控件的赋值情况[/b]
在aspx页面中:
<UCCommon:TID="UCT"runat="server"/>
在aspx.cs页面中:
protectedoverridevoidBindDataSource()
{
this.UCT.ItemList=dataList;
this.UCT.Title="helloworld!";
}
是在给用户控件的属性赋值。
用户控件隐藏文件T.ascx.cs代码有:
字段:
privateList<Info>itemList;
privatestringtitle;
属性:
publicstringTitle
{
set
{
this.title=value;
}
}
publicList<Info>ItemList
{
set
{
this.itemList=value;
}
}
这里面就是用户控件定义了属性对字段进行赋值的。
如果用户控件中还有用户控件的赋值情况:
用户控件隐藏文件T.ascx.cs代码有:
protectedoverridevoidBindDataSource()
{
T_T.SelectList=dataList;
T_T.Title="helloworld!";
}
这里和aspx页面一样是在BindDataSource()中对外层的用户控件进行赋值的。
当然T_T中会声明相应的字段和属性保存数据。 最后会在自己的BindDataSource()里面再次赋值给需要的控件,或者直接在页面上显示所得的数据。
可以在用户控件的T.ascx代码中直接获取属性例如:
<%=Title%>
在aspx页面中Repeater控件中的<ItemTemplate>里面绑定数据:
<%#Eval("Detail")==null||Eval("Detail").ToString()==string.Empty?" ":Eval("Detail")%>
<%#(Container.DataItemasBannerInfo).BannerTitle%>
在aspx页面中Repeater控件中Repeater控件的<ItemTemplate>里面绑定数据:
<%#Container.DataItem==null||Container.DataItem.ToString()==string.Empty?" ":Container.DataItem%>
在aspx页面直接赋值和在用户控件中一样:
<%=Title%>
相关文章推荐
- 用户控件和服务器控件的数据绑定
- 用户控件和服务器控件的数据绑定
- ASP.NET数据绑定控件数据项中的服务器控件注册JS方法
- 服务器控件1:控件数据绑定及页面数据绑定
- 数据绑定服务器控件
- 数据绑定服务器控件
- ASP.NET 数据绑定 Web 服务器控件概述
- 数据绑定控件展示、编辑、新建用户列表
- 数据绑定服务器控件
- ASP.NET 非服务器控件绑定数据
- 非服务器控件绑定数据
- 关于Asp.Net 服务器控件OnClientClick属性数据绑定
- 数据绑定服务器控件
- 绑定用户控件赋值对象(非基本数据类型)
- 地磅称量系统之(37~39) 直接向数据库的数据表WtBill添加测试数据以及绑定用户控件和使用编码的方式控制dataGridView控件的每列属性和添加数据数据库的表中不存在的字段(非绑定列)
- Repeater中的CheckBox绑定和存储数据之服务器控件
- ASP.NET - 将数据绑定到 TreeView Web 服务器控件
- 《ASP.NET1200例》嵌套在DataLisT控件中的其他服务器控件---DropDownList控件的数据绑定
- wpf绑定用户控件及listview(gridview)数据源
- datarow[] 数组绑定到.net服务器数据控件(repeate、gridview等)