Edit (Update, cancel), delete in datalist webcontrol

Source: Internet
Author: User
1
We must set different template of the datalist object as diffirent appearance, such as itemplate, editplate, headertemplate, footertemplate. in edit template, update function shoshould have the update command name as 'update', and the same to delete command. at the same time, we 'd better set the datakeyfield of the datalist so as to pick one item's key value.
2
For different command, writing concerned codes. Such as datalist1_editcomamnd, datalist1_updatecommand, datalist1_deletecommand

3 examples:
/// This is a datalist object HTML code for its appearance
<Asp: datalist id = "datalist1" datakeyfield = "username" runat = "server" Height = "129px" width = "775px"Onitemcreated = "datalist1_itemcreated" gridlines = "both"Ondeletecommand = "datalist1_deletecommand"Oneditcommand = "datalist1_editcommand"Onupdatecommand = "datalist1_updatecommand"Onitemcreated = "datalist1_itemcreated">
<Itemtemplate>
<Asp: Table borderwidth = 0px bordercolor = brown cellpadding = 0 cellspacing = 0 runat = server Height = 20 id = "tablefuck">
<Asp: tablerow width = 800px Height = 100%>
<Asp: tablecell width = 200px horizontalalign = center> <% # databinder. eval (container. dataitem, "username") %> </ASP: tablecell>
<Asp: tablecell width = 300px horizontalalign = center> <% # databinder. eval (container. dataitem, "Site") %> </ASP: tablecell>
<Asp: tablecell width = 300px horizontalalign = center> <% # databinder. eval (container. dataitem, "planner") %> </ASP: tablecell>
<Asp: tablecell width = 300px horizontalalign = center>
<Asp: linkbutton text = "edit" runat = server commandname = "edit"
>
 <Asp: linkbutton text = "Update" runat = server commandname = "Update"
>
</ASP: linkbutton> </ASP: tablecell>
</ASP: tablerow>
</ASP: Table>

</Itemtemplate>
<Edititemtemplate>
<Asp: Table runat = server id = "Table1" Height = "36px">

<Asp: tablerow Height = 100% runat = Server>
<Asp: tablecell runat = server width = 200px horizontalalign = center> <% # databinder. eval (container. dataitem, "username") %> </ASP: tablecell>
<Asp: tablecell runat = server width = 300px horizontalalign = center>
<Asp: textbox runat = server id = "tb_site" text = '<% # databinder. eval (container. dataitem, "Site") %>'> </ASP: textbox>
</ASP: tablecell>
<Asp: tablecell runat = server width = 300px horizontalalign = center>
<Asp: textbox runat = server id = "tb_planner" text = '<% # databinder. eval (container. dataitem, "planner") %>'/>
</ASP: tablecell>
<Asp: tablecell runat = server width = 300px horizontalalign = center>
<Asp: linkbutton text = "Update" commandname = "Update" runat = server id = "lb_update"/>
<Asp: linkbutton text = "cancel" commandname = "cancel" runat = server/>
</ASP: tablecell>
</ASP: tablerow>
</ASP: Table>

</Edititemtemplate>
<Headertemplate>

<Asp: Table runat = server id = Fuck cellpadding = "0" cellspacing = "0" Height = "22px" backcolor = black forecolor = white>
<Asp: tablerow width = 800px borderwidth = 0px runat = "server">
<Asp: tablecell width = 200px runat = server horizontalalign = center> username </ASP: tablecell>
<Asp: tablecell width = 300px runat = server horizontalalign = center> site </ASP: tablecell>
<Asp: tablecell width = 300px runat = server horizontalalign = center> planner </ASP: tablecell>
<Asp: tablecell id = "tablecell1" width = 300px runat = server horizontalalign = center> operation </ASP: tablecell>
</ASP: tablerow>

</ASP: Table>
</Headertemplate>

</ASP: datalist> </div>

/// Corresponding codes for Edit (Update, cancel), delete

Protected void datalist1_editcommand (Object source, datalistcommandeventargs E)
{
Datalist1.edititemindex = E. Item. itemindex;
Databind ();
Repaintallcontrol (E. Item. itemindex );
}

private void repaintallcontrol (INT itemindex)
{< br> # If hrg_version
// Add properties to contronl dialog box
imagebutton Lb;
lB = (imagebutton) datalist1.items [itemindex]. findcontrol ("lb_update");
system. diagnostics. debug. assert (Lb! = NULL);
lb. Attributes. Add ("onclick", "Return confirm ('Are you sure to update? ') ");
textbox DDL;

DDL = (textbox) datalist1.items [itemindex]. findcontrol ("tb_planner");
DDL = (dropdownlist) datalist1.items [itemindex]. findcontrol ("ddl_category");
........
.......
# endif
}

protected void datalistincluitemcreated (Object sender, datalistitemeventargs e)
{< br> // response. write (E. item. itemindex);
imagebutton Ib;
IB = (imagebutton) E. item. findcontrol ("ib_delete");
If (ib! = NULL)
{< br> IB. attributes. add ("onclick", "Return confirm ('Are you sure to delete the bug which ID is" + datalist1.datakeys [E. item. itemindex] + "')");
}< BR >}

protected void datalist1_deletecommand (Object source, datalistcommandeventargs e)
{< br> response. write (" ");
}< br> protected void datalist1_updatecommand (Object source, datalistcommandeventargs E)
{< br> connopen ();
sqlcommand cmd = new sqlcommand ("pr_updateuser", Conn);
cmd. commandtype = commandtype. storedprocedure;

Response. Write (textbox) E. Item. findcontrol ("tb_site"). Text. Trim ());

Sqlparameter username = new sqlparameter ("@ username", sqldbtype. varchar, 20 );
Username. value = datalist1.datakeys [E. Item. itemindex]. tostring (). Trim ();
Cmd. Parameters. Add (username );

Sqlparameter site = new sqlparameter ("@ site", sqldbtype. varchar, 10 );
Site. value = (textbox) E. Item. findcontrol ("tb_site"). Text. Trim ();
Cmd. Parameters. Add (SITE );

Sqlparameter planner = new sqlparameter ("@ planner", sqldbtype. varchar, 10 );
Planner. value = (textbox) E. Item. findcontrol ("tb_planner"). Text. Trim ();
Cmd. Parameters. Add (planner );

Cmd. executenonquery ();
Datalist1.edititemindex =-1;
Databind ();
}

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.