Use user controls in datalist

Source: Internet
Author: User

Datalist is more flexible than DataGrid in displaying data (or in vs2003). It should provide several templates for datalist, while some custom user controls can be flexibly placed in the template. The specific implementation is as follows:

Create a user control first:

<% @ Control Language = "C #" autoeventwireup = "true" codefile = "authornormal. ascx. cs" inherits = "authornormal" %>
<Table border = "1" cellpadding = "0" cellspacing = "0" style = "border-left-color: Aqua; border-bottom-color: Aqua; border-top-style: solid; border-top-color: Aqua; border-right-style: solid; border-left-style: solid; border-right-color: Aqua; border-bottom-style: solid "width =" 400 ">
<Tr>
<TD style = "width: 100px">
Author No.: </TD>
<TD style = "width: 100px">
<Asp: Label id = "lblid" runat = "server"> </ASP: Label> </TD>
</Tr>
<Tr>
<TD style = "width: 100px; Height: 21px">
Author name: </TD>
<TD style = "width: 100px; Height: 21px">
<Asp: Label id = "lblname" runat = "server"> </ASP: Label> </TD>
</Tr>
</Table>

Background code:

Private string au_id;
Public String au_id
{
Set
{
Au_id = value;
}
Get
{
Return au_id;
}
}
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
This. BIND ();
}
}
Private void BIND ()
{
Sqldatareader SDR = new author (). Show (this. au_id );
While (SDR. Read ())
{
This. lblid. Text = SDR ["au_id"]. tostring ();
This. lblname. Text = SDR ["au_lname"]. tostring (). Trim () + "" + SDR ["au_fname"]. tostring (). Trim ();
}
}
}

Note: To define an attribute of a user control, this attribute can be written only to receive information bound to datalist.

Code of the front-end page:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "usercontrolindatalist. aspx. cs" inherits = "usercontrolindatalist" %>

<% @ Register src = "authornormal. ascx" tagname = "authornormal" tagprefix = "uc1" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>

</Div>
<Div style = "text-align: Center">
<Table>
<Tr>
<TD style = "width: 100px; Height: 37px;">
<Asp: datalist id = "datalist1" runat = "server" repeatcolumns = "2">
<Itemtemplate>
<Uc1: authornormal id = "authornormal1" runat = "server" au_id = <% # databinder. eval (container. dataitem, "au_id") %>
</Uc1: authornormal>
</Itemtemplate>
</ASP: datalist> </TD>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>

Background code:

Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
This. datalist1.datasource = new author (). BIND ();
This. datalist1.databind ();
This. datalist1.datakeyfield = "au_id ";
}
}

When binding the page content, you can use any data (after query, memory, or load from an XML file ).

It should be particularly emphasized that, after condition-based query, viewstate or session is often used for transmission. If there are too many transmitted data, you can first upload the data in an arraylist and then transmit it, then, when binding to the datalist page, you can use a virtual table.

 

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.