(Original) Example of updating data pagination using dynamic template Columns

Source: Internet
Author: User

Http://www.cnblogs.com/lovecherry/archive/2005/03/25/125526.html

The last time I gave an example of a dynamic template column, a user asked me how to perform the update operation and paging. The following code is provided.


Front-end:

<% @ Page Language = "C #" codebehind = "webform30.aspx. cs" autoeventwireup = "false" inherits = "csdn. webform30" %>

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">

<HTML>

<Head>

<Title> webform30 </title>

<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">

<Meta name = "code_language" content = "C #">

<Meta name = "vs_defaultclientscript" content = "JavaScript">

<Link href = "css.css" rel = "stylesheet" type = "text/CSS">

<Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5">

</Head>

<Body ms_positioning = "gridlayout">

<Form ID = "form1" method = "Post" runat = "server">

<Asp: DataGrid id = "datagrid1" runat = "server"> </ASP: DataGrid>

</Form>

</Body>

</Html>

Background:

Using system;

Using system. collections;

Using system. componentmodel;

Using system. Data;

Using system. Data. sqlclient;

Using system. drawing;

Using system. Web;

Using system. Web. sessionstate;

Using system. Web. UI;

Using system. Web. UI. webcontrols;

Using system. Web. UI. htmlcontrols;

Namespace csdn

{

/// <Summary>

/// Summary of webform30.

/// </Summary>

Public class webform30: system. Web. UI. Page

{

Protected system. Web. UI. webcontrols. DataGrid datagrid1;



Private void page_load (Object sender, system. eventargs E)

{

// Place user code here to initialize the page

If (! Ispostback)

{

Bindgrid ();

}

Createdatagrid (); // you can set the DataGrid.

}

 

Protected void createdatagrid ()

{

Datagrid1.autogeneratecolumns = false; // disable auto-generated Columns

Datagrid1.cssclass = "border"; // border Style

Datagrid1.borderwidth = 0;

Datagrid1.cellspacing = 1;

Datagrid1.cellpadding = 5;

Datagrid1.itemstyle. cssclass = "item"; // normal Column Style

Datagrid1.headerstyle. cssclass = "Header"; // Header style

Datagrid1.pagerstyle. cssclass = "Header"; // footer Style

Datagrid1.datakeyfield = "stuid"; // primary key field

Datagrid1.allowpaging = true; // allow pagination

Datagrid1.pagesize = 5; // page size

Datagrid1.pagerstyle. mode = pagermode. numericpages; // digital Paging

Editcommandcolumn ECC = new editcommandcolumn (); // update the button Column

ECC. buttontype = buttoncolumntype. Pushbutton; // push button

ECC. edittext = "edit ";

ECC. canceltext = "cancel ";

ECC. updatetext = "Update"; // button text

Datagrid1.columns. Add (ECC); // Add a button Column

Maid + = new maid (maid editcommand );

Maid command + = new maid (maid updatecommand );

Maid command + = new maid (maid cancelcommand); // update, cancel, edit event registration

Datagrid1.pageindexchanged + = new system. Web. UI. webcontrols. datagridpagechangedeventhandler

Setbind (); // bind data

}

Protected void bindgrid ()

{

Templatecolumn TM = new templatecolumn ();

TM. itemtemplate = new columntemplate1 (); // common column

TM. edititemtemplate = new columntemplate2 (); // edit a column

TM. headertext = "name ";

Datagrid1.columns. addat (0, Tm); // Add the first template column to the first column.

Templatecolumn TM2 = new templatecolumn ();

Tm2.itemtemplate = new columntemplate3 ();

Tm2.edititemtemplate = new columntemplate4 ();

Tm2.headertext = "";

Datagrid1.columns. addat (1, TM2); // Add the first template column to the second column.

Datagrid1.itemdatabound + = new system. Web. UI. webcontrols. datagriditemeventhandler (datagrid1_itemdatabound); // register a Data Binding event. Pay attention to the location of Event code registration.

Setbind ();

}

Protected void setbind ()

{

Sqlconnection conn = new sqlconnection (system. configuration. configurationsettings. deleettings ["conn"]);

Sqldataadapter da = new sqldataadapter ("select * from Stu, DEP where Stu. studepid = dep. depid", Conn );

Dataset DS = new dataset ();

Da. Fill (DS, "Table1 ");

This. datagrid1.datasource = Ds. Tables ["Table1"];

This. datagrid1.databind ();



}

Private void maid (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)

{

Sqlconnection conn = new sqlconnection (system. configuration. configurationsettings. deleettings ["conn"]);

Sqldataadapter da = new sqldataadapter ("select * From Dep", Conn );

Dataset DS = new dataset ();

Da. Fill (DS, "Table1 ");

If (E. Item. itemtype = listitemtype. edititem)

{

Dropdownlist DDL = (dropdownlist) E. Item. findcontrol ("Dep ");

DDL. datasource = Ds. Tables ["Table1"];

DDL. datatextfield = "depname ";

DDL. datavaluefield = "depid ";

DDL. databind ();

DDL. Items. findbyvalue (convert. tostring (databinder. eval (E. Item. dataitem, "depid"). Selected = true;

}

}

Private void datagrid1_editcommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)

{

This. datagrid1.edititemindex = E. Item. itemindex;

Bindgrid ();

}

Private void datagrid1_cancelcommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)

{

This. Maid =-1;

Bindgrid ();

}

Private void datagrid1_updatecommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)

{

String uid = E. Item. uniqueid + ":"; // do not omit the colon

Sqlconnection conn = new sqlconnection (system. configuration. configurationsettings. deleettings ["conn"]);

Sqlcommand comm = new sqlcommand ("Update Stu set stuname = @ name, studepid = @ depid where stuid = @ ID", Conn );

Sqlparameter parm1 = new sqlparameter ("@ name", sqldbtype. nvarchar, 50 );

Parm1.value = request. Form [uid + "name"]. tostring ();

Sqlparameter parm2 = new sqlparameter ("@ depid", sqldbtype. INT );

Parm2.value = request. Form [uid + "Dep"]. tostring ();;

Sqlparameter parm3 = new sqlparameter ("@ ID", sqldbtype. INT );

Parm3.value = This. datagrid1.datakeys [E. Item. itemindex];

Comm. Parameters. Add (parm1 );

Comm. Parameters. Add (parm2 );

Comm. Parameters. Add (parm3 );

Conn. open ();

Comm. executenonquery ();

Conn. Close ();

This. Maid =-1;

Bindgrid ();

// The reason why the previous (textbox) E. Item. findcontrol ("name"). text cannot be used to retrieve data is that the DataGrid column is dynamically added and cannot be obtained.

}

Private void datagrid1_pageindexchanged (Object source, system. Web. UI. webcontrols. datagridpagechangedeventargs E)

{

This. datagrid1.currentpageindex = E. newpageindex;

Bindgrid ();

}

# Code generated by region web Form Designer

Override protected void oninit (eventargs E)

{

//

// Codegen: This call is required by the ASP. NET web form designer.

//

Initializecomponent ();

Base. oninit (E );

}

 

/// <Summary>

/// The designer supports the required methods-do not use the code editor to modify

/// Content of this method.

/// </Summary>

Private void initializecomponent ()

{

This. Load + = new system. eventhandler (this. page_load );

}

# Endregion

}

Public class columntemplate1: itemplate

{



Public void instantiatein (control container)

{

Literalcontrol L = new literalcontrol ();

L. databinding + = new eventhandler (this. ondatabinding );

Container. Controls. Add (L );

}

Public void ondatabinding (Object sender, eventargs E)

{

Literalcontrol L = (literalcontrol) sender;

Datagriditem Container = (datagriditem) L. namingcontainer;

L. Text = (datarowview) container. dataitem) ["stuname"]. tostring ();

}

}

Public class columntemplate2: itemplate

{

Public void instantiatein (control container)

{

Textbox T = new Textbox ();

T. width = 88;

T. ID = "name"; // you need to give an ID, which can be obtained at request. form.

T. databinding + = new eventhandler (this. ondatabinding );

Container. Controls. Add (t );

}

Public void ondatabinding (Object sender, eventargs E)

{

Textbox t = (textbox) sender;

Datagriditem Container = (datagriditem) T. namingcontainer;

T. Text = (datarowview) container. dataitem) ["stuname"]. tostring (); // bind the stuname Field

}

}

Public class columntemplate3: itemplate

{

Public void instantiatein (control container)

{

Literalcontrol L = new literalcontrol ();

L. databinding + = new eventhandler (this. ondatabinding );

Container. Controls. Add (L );

}

Public void ondatabinding (Object sender, eventargs E)

{

Literalcontrol L = (literalcontrol) sender;

Datagriditem Container = (datagriditem) L. namingcontainer;

L. Text = (datarowview) container. dataitem) ["depname"]. tostring ();

}

}

Public class columntemplate4: itemplate

{

Public void instantiatein (control container)

{

Dropdownlist DPL = new dropdownlist ();

DPL. ID = "Dep ";

Container. Controls. Add (DPL );

} // No data binding is performed for this drop-down box. This operation is performed in itemdatabound of datagrid1.

}

}

The code is relatively simple, and there are simple annotations in the code.

Supplement 2: (1) this time the DataGrid is generated during design, not during the last running, the latter will cause update disorder; (2) being simple, the template column Class in the Code is not well encapsulated. When using it, you should encapsulate the field and the template column type (listitemtype ).

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.