Control settings:
<Asp: datalist id = "dledititem" runat = "server" datakeyfield = "employeeid">
<Headertemplate>
Personnel Information
</Headertemplate>
<Footertemplate>
<HR color = "red">
</Footertemplate>
<Itemtemplate>
<Asp: button id = "edit" runat = "server" text = "edit" commandname = "edit"> </ASP: button>
<% # Databinder. eval (container. dataitem, "lastname") %>
<% # Databinder. eval (container. dataitem, "firstname") %>
</Itemtemplate>
<Edititemtemplate>
<Asp: Label runat = "server" id = "lastname">
<% # Databinder. eval (container. dataitem, "lastname") %>
</ASP: Label>
<Asp: Label runat = "server" id = "firstname">
<% # Databinder. eval (container. dataitem, "firstname") %>
</ASP: Label>
<Asp: textbox runat = server id = "title" text = '<% # databinder. eval (container. dataitem, "title") %>'/>
<Asp: textbox runat = server id = "titleofcourtesy" text = '<% # databinder. eval (container. dataitem, "titleofcourtesy") %>'/>
<Asp: button id = "Update" runat = "server" text = "Update" commandname = "Update"/>
<Asp: button id = "cancel" runat = "server" text = "cancel" commandname = "cancel"/>
</Edititemtemplate>
</ASP: datalist>
BackgroundCode:
Private void dledititem_cancelcommand (Object source, system. Web. UI. webcontrols. datalistcommandeventargs E)
{
// Set the index of the edit item of the datalist control to-1.
Dledititem. edititemindex =-1;
// Data Binding
Datalistdatabind ();
}
Private void dledititem_editcommand (Object source, system. Web. UI. webcontrols. datalistcommandeventargs E)
{
// Set the index of the edit item of the datalist control to the current item
Dledititem. edititemindex = E. Item. itemindex;
// Data Binding
Datalistdatabind ();
}
Private void dledititem_updatecommand (Object source, system. Web. UI. webcontrols. datalistcommandeventargs E)
{
// Obtain the value of the key field of the edit row
Int empid = (INT) dledititem. datakeys [E. Item. itemindex];
// Obtain the content entered in the text box
Textbox newtitle = (textbox) E. Item. findcontrol ("title ");
Textbox newtitleofcour = (textbox) E. Item. findcontrol ("titleofcourtesy ");
String sqlcom = "Update employees set Title = '" + newtitle. text + "', titleofcourtesy ='" + newtitleofcour. text + "'where employeeid =" + empid. tostring ();
// Define the data connection object. The database connection string is defined in the web. config file.
Sqlconnection conn = new sqlconnection (system. configuration. configurationsettings. deleettings ["databasecon"]. tostring ());
// Define the command object
Sqlcommand cmd = new sqlcommand (sqlcom, Conn );
// Open the data connection
Conn. open ();
Try
{
// Execute the SQL command
Cmd. executenonquery ();
// Cancel editing
Dledititem. edititemindex =-1;
Datalistdatabind ();
}
Catch (exception ERR)
{
// Enter exception information
Response. Write (ERR. tostring ());
}
Finally
{
// Close the connection object
Conn. Close ();
}
}
private void datalistdatabind ()
{< br> sqlconnection conn = new sqlconnection (system. configuration. configurationsettings. appsettings ["databasecon"]. tostring ();
sqldataadapter da = new sqldataadapter ("select * from employees", Conn);
dataset DS = new dataset ();
try
{< br> da. fill (DS, "testtable");
dledititem. datasource = Ds. tables ["testtable"];
dledititem. databind ();
}< br> catch (Exception error)
{< br> response. write (error. tostring ();
}< BR >}