Data Connection-Use of datalist (view, edit, and delete)

Source: Internet
Author: User

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

 

 

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.