<% @ Page Language = "C #" codebehind = "webform4.aspx. cs" autoeventwireup = "false" inherits = "dgrid. webform4" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> webform4 </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "code_language" content = "C #">
<Meta name = "vs_defaultclientscript" content = "JavaScript">
<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" autogeneratecolumns = "false" showfooter = "true">
<Columns>
<Asp: templatecolumn>
<Itemtemplate>
<% # Databinder. eval (container. dataitem, "name", "{0}") %>
</Itemtemplate>
<Footertemplate>
<Asp: textbox runat = "server" id = "txtname"> </ASP: textbox>
</Footertemplate>
</ASP: templatecolumn>
<Asp: templatecolumn>
<Itemtemplate>
<% # Databinder. eval (container. dataitem, "Age", "{0}") %>
</Itemtemplate>
<Footertemplate>
<Asp: textbox id = "txtage" runat = "server"> </ASP: textbox>
</Footertemplate>
</ASP: templatecolumn>
<Asp: buttoncolumn text = "excluding">
</ASP: buttoncolumn>
</Columns>
</ASP: DataGrid>
</Form>
</Body>
</Html>
Bytes ---------------------------------------------------------------------------------------
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Namespace dgrid
{
/// <Summary>
/// Summary of webform4.
/// </Summary>
Public class webform4: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. DataGrid datagrid1;
Protected dataset dsemployee = new dataset ();
Protected button mybutton = new button ();
Private void page_load (Object sender, system. eventargs E)
{
If (! Page. ispostback)
{
This. Admin ();
}
}
Private void admin ()
{
Sqlconnection conn = new sqlconnection ("Server =.; database = test; uid = sa ");
String strsql = "select * from AAA ";
Sqldataadapter adapter = new sqldataadapter (strsql, Conn );
Adapter. Fill (dsemployee );
This. datagrid1.datasource = dsemployee;
This. datagrid1.databind ();
}
# 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 );
This. Maid + = new maid (maid itemcreated );
This. Maid + = new maid (maid itemcommand );
}
# Endregion
Private void datagrid1_itemcreated (Object sender, datagriditemeventargs E)
{
If (E. Item. itemtype = listitemtype. footer)
{
Mybutton. ID = "btnaddnew ";
Mybutton. Text = "OK ";
Mybutton. commandname = "addnew ";
E. Item. cells [2]. Controls. Add (mybutton );
}
}
Private void datagrid1_itemcommand (Object source, datagridcommandeventargs E)
{
If (E. commandname = "addnew ")
{
String name = (textbox) E. Item. cells [0]. findcontrol ("txtname"). Text. Trim ();
String age = (textbox) E. Item. cells [1]. findcontrol ("txtage"). Text. Trim ();
Sqlconnection conn = new sqlconnection ("Server =.; database = test; uid = sa ");
String strsql = "insert into AAA (name, age) values ('" + name + "', '" + age + "')";
Conn. open ();
Sqlcommand comm = new sqlcommand (strsql, Conn );
Comm. executenonquery ();
Conn. Close ();
This. Admin ();
}
}
}
}