<1> database binding:
Con. open ();
Sqldataadapter SDA = new sqldataadapter ();
SDA. selectcommand = new sqlcommand ("select * From person", con );
Dataset DS = new dataset ();
SDA. Fill (DS, "person ");
This. datalist1.datakeyfield = "PID ";
This. datalist1.datasource = Ds. Tables ["person"];
This. datalist1.databind ();
<2> datalist property Generator
Edit update, delete, and undo commandnames are edit update Delete cancel. In this way, you can directly respond to these events in datalist events for operations.
the style list is as follows:
View Details
edit
Delete <% # databinder. eval (container. dataitem, "pname") %> <% # databinder. eval (container. dataitem, "0000x") %>
</Itemtemplate>
<Selecteditemtemplate>
<% # Databinder. eval (container. dataitem, "PID", "serial number: {0}") %> <br>
<% # Databinder. eval (container. dataitem, "pname") %>
<% # Databinder. eval (container. dataitem, "iner X") %>
</Selecteditemtemplate>
<Edititemtemplate>
<Asp: linkbutton id = "linkbutton3" runat = "server" commandname = "cancel"> cancel </ASP: linkbutton>
<Asp: linkbutton id = "linkbutton4" runat = "server" commandname = "Update"> Save </ASP: linkbutton>
<Asp: textbox id = "textbox1" runat = "server" text = '<% # Databinder. eval (container. dataitem, "pname ") %> '> </ASP: textbox>
</Edititemtemplate>
<Alternatingitemstyle font-bold = "false" font-italic = "false" font-overline = "false"
Font-strikeout = "false" font-underline = "false" forecolor = "maroon"/>
<Itemstyle font-bold = "false" font-italic = "false" font-overline = "false" font-strikeout = "false"
Font-underline = "false" forecolor = "green"/>
</ASP: datalist>
ProgramCode:
Protected void datalist1_itemcommand (Object source, datalistcommandeventargs E)
{
If (E. commandname = "select ")
{
This. datalist1.selectedindex = E. Item. itemindex; // select
This. datalist1.databind ();
}
}
Protected void datalist1_editcommand (Object source, datalistcommandeventargs E)
{
This. datalist1.edititemindex = E. Item. itemindex; // Edit
This. datalist1.databind ();
}
Protected void datalist1_cancelcommand (Object source, datalistcommandeventargs E)
{
This. datalist1.edititemindex =-1; // cancel
This. datalist1.databind ();
}
Protected void datalist1_updatecommand (Object source, datalistcommandeventargs e) // update
{
String pid = This. datalist1.datakeys [E. Item. itemindex]. tostring ();
String name = (textbox) E. Item. findcontrol ("textbox1"). text;
Sqlconnection con = connecttion. Ado. sqldb ();
Con. open ();
Sqlcommand cmd = new sqlcommand ("Update person set pname = '" + name + "'where pid ='" + PID + "'", con );
Cmd. executenonquery ();
This. datalist1.edititemindex =-1;
This. datalist1.databind ();
Response. Write (PID + name );
}
}