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 ();
}