Use the editing template of the datalist Control

Source: Internet
Author: User

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 >}

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.