Data Control-datalist Data Control

Source: Internet
Author: User
Datalist Data Control is extended based on the repeater data control. In addition to the Repeater control function, you can also set the number of display rows.
The data output formats of the datalist control and Repeater control depend on the template definition. The difference is that the datalist control is output as a table in the browser. <Asp: datalist id = "DL" runat = "server" repeatcolumns = "3" repeatdirection = "horizontal">
<Itemtemplate>
<B>
<% # Databinder. eval (container. dataitem, "name") %>
(<% # Databinder. eval (container. dataitem, "student ID") %>) </B> mathematical score: <% # databinder. eval (container. dataitem, "Mathematics") %> <br>
</Itemtemplate>
</ASP: datalist>

Protected void page_load (Object sender, eventargs E)
{
String provider, database, connstr, SQL;
Provider = "Microsoft. Jet. oledb.4.0 ;";
Database = server. mappath ("person. mdb ");
Connstr = "provider =" + provider + "Data Source =" + database;
SQL = "select * from Grade ";
Oledbdataadapter da;
DA = new oledbdataadapter (SQL, connstr );
Dataset DS = new dataset ();
Da. Fill (DS, "Grade ");
DL. datasource = Ds. Tables ["Grade"]. defaultview;
DL. databind ();
}

In addition to displaying items in the data source by using templates, you can also define event processing in the datalist control. For example, "onitemcommand =" dl_itemcommand "defines that when an event occurs in the datalist control, the dl_itemcommand function is called for processing. <Asp: datalist id = "DL" runat = "server" bordercolor = "black" borderwidth = "1px" cellpadding = "2"
Cellspacing = "0" headerstyle-backcolor = "# aaaadd" alternatingitemstyle-backcolor = "lightgray"
Selecteditemstyle-backcolor = "yellow" onitemcommand = "dl_itemcommand">
<Headertemplate>
Name (student ID) <Itemtemplate>
<% # Databinder. eval (container. dataitem, "name") %>
(<% # Databinder. eval (container. dataitem, "student ID") %>)
<Asp: linkbutton id = "detail" runat = "server" text = "query results"/>
</Itemtemplate>
<Selecteditemtemplate>
Name: <% # databinder. eval (container. dataitem, "name") %> <br>
Student ID: <% # databinder. eval (container. dataitem, "student ID") %> </B>
<Br>
Mathematical score: <% # databinder. eval (container. dataitem, "Mathematics") %> <br>
<Asp: linkbutton id = "title" runat = "server" text = "Close query"/>
</Selecteditemtemplate>
</ASP: datalist>

Protected void page_load (Object sender, eventargs E)
{
Bindgrid ();
}

Private void bindgrid ()
{
String provider, database, connstr, SQL;
Provider = "Microsoft. Jet. oledb.4.0 ;";
Database = server. mappath ("person. mdb ");
Connstr = "provider =" + provider + "Data Source =" + database;
SQL = "select * from Grade ";
Oledbdataadapter da;
DA = new oledbdataadapter (SQL, connstr );
Dataset DS = new dataset ();
Da. Fill (DS, "Grade ");
DL. datasource = Ds. Tables ["Grade"]. defaultview;
DL. databind ();
}

Protected void dl_itemcommand (Object source, datalistcommandeventargs E)
{
If (linkbutton) E. commandsource). Text = "query results ")
DL. selectedindex = E. Item. itemindex;
Else if (linkbutton) E. commandsource). Text = "Close query ")
DL. selectedindex =-1;
Bindgrid ();
}

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.