Dynamic generation of able binding to datalist

Source: Internet
Author: User

Default. aspx:

Program code <asp: datalist id = "datalist1" runat = "server" oncancelcommand = "login" ondeletecommand = "datalist1_delete" oneditcommand = "datalist1_edit" onupdatecommand = "datalist1_update" datakeyfield = "ID">
<Itemtemplate>
<Div style = "background-color: red;"> <% # eval ("ID") %>, <% # eval ("sitename") %>, <% # eval ("url") %> <asp: button id = "button1" commandname = "edit" runat = "server"
TEXT = "edit"/> <asp: button id = "button2" commandname = "delete" runat = "server" text = "delete"/> </div>
</Itemtemplate>
<Separatortemplate>
<HR style = "height: 1px;"/>
</Separatortemplate>
<Edititemtemplate>
<Div> <asp: textbox id = "textbox1" runat = "server" text = '<% # eval ("sitename") %>'> </ASP: textbox> <asp: textbox id = "textbox2" runat = "server" text = '<% # eval ("url") %>'> </ASP: textbox>
<Asp: button
Id = "button7" commandname = "Update" runat = "server" text = "Update"/> <asp: button id = "button8" commandname = "cancel" runat = "server"
TEXT = "cancel"/> </div>
</Edititemtemplate>
</ASP: datalist>

Default. aspx. CS:

Program code using system;
Using system. Data;
Using system. Data. Sqlclient;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Dataset DS = new dataset ();
Datatable dt = new datatable ();
Datacolumn DC;
Datarow Dr;

// --------------------- Add the field ----------------

// Create Field 1
Dc = new datacolumn ();
DC. datatype = system. type. GetType ("system. int32 ");
DC. columnname = "ID ";
DC. autoincrement = true; // ID
DC. autoincrementseed = 1; // identify the seed
DC. autoincrementstep = 1; // ID Increment
DT. Columns. Add (DC );
// Create Field 2
Dc = new datacolumn ();
DC. datatype = system. type. GetType ("system. String ");
DC. columnname = "sitename ";
DC. allowdbnull = true;
DT. Columns. Add (DC );
// Create Field 3
Dc = new datacolumn ();
DC. datatype = system. type. GetType ("system. String ");
DC. columnname = "url ";
DC. allowdbnull = true;
DT. Columns. Add (DC );
// Set the primary key
Datacolumn [] newdc = new datacolumn [1]; // you can set multiple fields as primary keys.
Newdc [0] = DT. Columns ["ID"];
DT. primarykey = newdc;

// --------------------- Add record ----------------

// Record 1
Dr = DT. newrow ();
Dr ["sitename"] = "Shang net ";
Dr ["url"] = "http://www.jjshang.com /";
DT. Rows. Add (DR );
// Record 2
Dr = DT. newrow ();
Dr ["sitename"] = "wooden house ";
Dr ["url"] = "http://www.mzwu.com /";
DT. Rows. Add (DR );
// Record 3
Dr = DT. newrow ();
Dr ["sitename"] = "Netease ";
Dr ["url"] = "http://www.126.com /";
DT. Rows. Add (DR );

// ---- Add the table to dataset and bind it to datalist ----
DS. Tables. Add (DT );
DS. Tables [0]. tablename = "Homepage ";
Session ["ds"] = Ds;
Binddata ();
}
}
Private void binddata ()
{
(Dataset) session ["ds"]). Tables ["Homepage"]. defaultview. Sort = "id DESC ";
Datalist1.datasource = (Dataset) session ["ds"]). Tables ["Homepage"]. defaultview;
Datalist1.databind ();
}
Protected void datalist1_cancel (Object source, datalistcommandeventargs E)
{
Datalist1.edititemindex =-1;
Binddata ();
}
Protected void datalist1_delete (Object source, datalistcommandeventargs E)
{
Int id = (INT) datalist1.datakeys [E. Item. itemindex];

Datarow DR = (Dataset) session ["ds"]). Tables ["Homepage"]. Rows [getrow (ID)];
Dr. Delete ();

Datalist1.edititemindex =-1;
Binddata ();
}
Protected void datalist1_edit (Object source, datalistcommandeventargs E)
{
Datalist1.edititemindex = E. Item. itemindex;
Binddata ();
}
Protected void datalist1_update (Object source, datalistcommandeventargs E)
{
String sitename = (textbox) E. Item. findcontrol ("textbox1"). text;
String url = (textbox) E. Item. findcontrol ("textbox2"). text;
Int id = (INT) datalist1.datakeys [E. Item. itemindex];

Datarow DR = (Dataset) session ["ds"]). Tables ["Homepage"]. Rows [getrow (ID)];
Dr ["sitename"] = sitename;
Dr ["url"] = URL;

Datalist1.edititemindex =-1;
Binddata ();
}
Private int getrow (int id)
{
Datarow Dr;
For (INT I = 0; I <(Dataset) session ["ds"]). Tables ["Homepage"]. Rows. Count; I ++)
{
Dr = (Dataset) session ["ds"]). Tables ["Homepage"]. Rows [I];
If (INT) Dr ["ID"] = ID) return I;
}
Return 0;
}
}

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.