Data Binding between user control and Server Control

Source: Internet
Author: User

1. Data Source bound to the Repeater control

 
The data source bound to the Repeater control in the Aspx. CS file is in binddatasource:
 
Protected override void binddatasource ()
{
This. rpid. datasource = This. datalist;
This. rpid. databind ();
}
 
The Repeater control event onitemdatabound indicates that each item is operated upon when the <itemtemplate> list is cyclically loaded.
Example:
Protected void rp_itemdatabound (Object sender, repeateritemeventargs E)
{
If (E. Item. itemtype = listitemtype. alternatingitem | E. Item. itemtype = listitemtype. item)
{
List <info> Infos = This. rpid. datasource as list <info>;

Info info = E. Item. dataitem as Info;
 
Literal lT = E. Item. findcontrol ("LT") as literal;
Lt. Text = info. title;
}
}
 
It can be seen that the user control and server control values in the <itemtemplate> of the Repeater control are assigned in the Repeater control event onitemdatabound.
 
Ii. User Controls
User Control T. ascxCodeInclude:
 
Field:
Private String title;
 
Attribute:
Public String title
{
Get
{
Return this. title;
}
}
 Method:
Settitle ()
{
This. Title = "Hello world! ";
}
In fact, attribute assignment can also be used for field assignment, but you must add the set part in the attribute declaration.
If the T User Control appears in of the Repeater control, you must assign a value to the
Repeater control event onitemdatabound.
example:
protected void rp_itemdatabound (Object sender, repeateritemeventargs E)
{< br> If (E. item. itemtype = listitemtype. alternatingitem | E. item. itemtype = listitemtype. ITEM)
{< br> List Infos = This. rpid. datasource as list ;< br>
info = E. item. dataitem as Info;
T = E. Item. findcontrol ("T") as t;
 
T. settitle (info. Title );
}
}
You can see that the above operation has saved the data to the title field. In the user space T. ascx code, use <% = title %> to access the title attribute,
Because the get part of the title attribute has been declared before.
Iii. Full view of the use of a simple reapter Control
 
On the ASPX page:
 
<Asp: repeater id = "rpid" runat = "server" onitemdatabound = "rp_itemdatabound">
<Itemtemplate>
<Asp: literal id = "LT" runat = "server"> </ASP: literal>
</Itemtemplate>
</ASP: repeater>
 
On the Aspx. CS page:
Protected override void binddatasource ()
{
This. rpid. datasource = This. datalist;
This. rpid. databind ();
}
 
Protected void rp_itemdatabound (Object sender, repeateritemeventargs E)
{
If (E. Item. itemtype = listitemtype. alternatingitem | E. Item. itemtype = listitemtype. item)
{
List <info> Infos = This. rpid. datasource as list <info>;

Info info = E. Item. dataitem as Info;
Literal lT = E. Item. findcontrol ("LT") as literal;
 
Lt. Text = info. title;
}
}
Note that I have mentioned that server controls and user controls are used in the same way.
It can be seen that binddatasource () is to bind the data sources of the oldest user control and server control on the ASPX page,
The onitemdatabound event is a data source for the user control and Server Control in the <itemtemplate> of the Repeater control.
.
4. nested repeater controls
 
This is actually a special case:
On the ASPX page:
<Asp: repeater id = "rpid" runat = "server" onitemdatabound = "rp_itemdatabound">
<Itemtemplate>
<Asp: repeater id = "rp_rpid" runat = "server">
<Asp: literal id = "LT" runat = "server"> </ASP: literal>
</ASP: repeater>
</Itemtemplate>
</ASP: repeater>
 
On the Aspx. CS page:
 
Protected override void binddatasource ()
{
This. rpid. datasource = This. datalist;
This. rpid. databind ();
}
Protected void rp_itemdatabound (Object sender, repeateritemeventargs E)
{
If (E. Item. itemtype = listitemtype. alternatingitem | E. Item. itemtype = listitemtype. item)
{
List <info> Infos = This. rpid. datasource as list <info>;

Info info = E. Item. dataitem as Info;
 
Repeater rp_rpid = E. Item. findcontrol ("rp_rpid") as repeater;
Rp_rpid.datasource = datalist;
 
Rp_rpid.databind ();
}
}
 
 
V. Assignment of User Controls
 
On the ASPX page:
<Uccommon: T id = "uct" runat = "server"/>
 
On the Aspx. CS page:
Protected override void binddatasource ()
{
This. uct. itemlist = datalist;
This. uct. Title = "Hello world! ";
}
Is to assign a value to the properties of the user control.
 
User Control hidden file T. ascx. CS code:
Field:
 
Private list <info> itemlist;
Private String title;
           attributes:  
           Public String title 
{< br> set
{< br> This. title = value;
}< BR >}< br> public list itemlist
{< br> set
{< br> This. itemlist = value;
}< BR >}
The user control defines attributes to assign values to fields.
 
If the user control has a User Control Value assignment:
 
User Control hidden file T. ascx. CS code:
           protected override void binddatasou RCE () 
{< br> t_t.selectlist = datalist;
t_t.title = "Hello world! ";
}< br> in the same way as the ASPX page, the outer user control is assigned a value in binddatasource.
Of course, the corresponding fields and attributes will be declared in t_t to save the data.
Finally, the required control is assigned again in your binddatasource (), or the obtained data is directly displayed on the page.
 
You can directly obtain attributes in the T. ascx code of the user control, for example:
<% = Title %>
 
Bind data to the <itemtemplate> In the Repeater control on the ASPX page:
           <% # eval (" detail ") = NULL | eval (" Detail "). tostring () = string. Empty? "& Nbsp;": eval ("detail ") %>  
            
<% # (Container. dataitem as bannerinfo). bannertitle %>
 
On the ASPX page, bind data to the <itemtemplate> of the Repeater control:
<% # Container. dataitem = NULL | container. dataitem. tostring () = string. Empty? "& Nbsp;": container. dataitem %>
 
The direct assignment on the ASPX page is the same as that in the user control:
            <% = title %>  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.