您的位置:首页 > 其它

用户控件和服务器控件的数据绑定

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%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: