1,The data list datalist is similar to the repeator, but datalist is more widely used because it can select and modify the content of a data item.
The data display and layout of datalist are controlled by the "template" like the repeator control.
(Note: The template must define at least one "data item template" (itemtemplate) to specify the display layout)
2. template types supported by datalist:
Template |
Name |
Description |
Itemtemplate |
Data item Template |
Required. It defines the data item and its representation. |
Alternatingitemplate |
Data item alternate Template |
To make the adjacent data items different, you can define an alternate template, which makes the adjacent data items look obviously different. By default, it is consistent with the itemtemplate template definition, that is, the adjacent data is left unrepresented and differentiated. |
Separatortemplate |
Separator Template |
Define the delimiter between data items |
Selecteditemtemplate |
Selected Template |
Defines the presentation content and layout of the selected data items. When the "selecteditemtemplate" template is not defined, the presentation content and format of the selected items are not special, which is determined by the itemtemplate template definition. |
Edititemtemplate |
Modify option Template |
Defines the display content and layout of the data items to be modified. By default, the modify option template is the definition of the data item template (itemtemplate ). |
Headertemplate |
Header definition Template |
Define the table header monetization form |
Footertemplate |
Table end definition Template |
|
Instance:
Student_info table in the database charge_sys used:
Studentno |
Studentname |
Grade |
Cash |
1 |
Han xueyang |
Freshman |
123.000 |
2 |
Han |
Sophomore year |
123.000 |
3 |
Han Hong |
Junior |
222.000 |
4 |
Han xuemin |
Senior |
20000.000 |
The program is in default. aspx:
In default. aspx:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %> <! 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">
:
In default. aspx. CS:
Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; using system. data; using system. data. sqlclient; public partial class _ default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {If (! This. ispostback) {This. databindtodatalist () ;}// private void databindtodatalist () {sqlconnection con = dB. createcon (); sqldataadapter SDA = new sqldataadapter (); SDA. selectcommand = new sqlcommand ("select * From student_info", con); dataset DS = new dataset (); SDA. fill (DS, "studentinfo"); this. datalist1.datakeyfield = "cardno"; this. datalist1.datasource = Ds. tables ["studentinfo"]; this. datalist1.databind ();} protected void datalist1_itemcommand (Object source, datalistcommandeventargs e) {If (E. commandname = "select") {This. datalist1.selectedindex = E. item. itemindex; this. databindtodatalist (); // call method databindtodatalist ()} protected void datalist1_editcommand (Object source, datalistcommandeventargs e) {This. datalist1.edititemindex = E. item. itemindex; this. databindtodatalist ();} protected void datalist1_cancelcommand (Object source, datalistcommandeventargs e) {This. datalist1.edititemindex =-1; this. databindtodatalist ();} protected void datalist1_updatecommand (Object source, datalistcommandeventargs e) {string cardno = This. datalist1.datakeys [E. item. itemindex]. tostring (); string grade = (textbox) E. item. findcontrol ("txtgrade ")). text; sqlconnection con = dB. createcon (); sqlcommand cmd = new sqlcommand ("Update student_info set grade = '" + grade + "'where cardno ='" + cardno + "'", con); con. open (); cmd. executenonquery (); this. datalist1.edititemindex =-1; this. databindtodatalist ();}}
Running result:
Page loading:
Click "View Details:
Click "edit:
Conclusion: "hands-on" is the final principle