The table dynamically enters multiple rows of data and submits the data ...............

Source: Internet
Author: User

<% @ Page Language = "C #" codebehind = "multi-row input submission. aspx. cs" autoeventwireup = "false" inherits = "bstest2005_8_16.samples. multi-row input submission" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> submit multiple rows </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" style = "Z-INDEX: 101; left: 32px; position: absolute; top: 32px" runat = "server"
Autogeneratecolumns = "false">
<Columns>
<Asp: templatecolumn headertext = "ID">
<Itemtemplate>
<% # "--" + Databinder. eval (container. dataitem, "ID") %>
</Itemtemplate>
<Edititemtemplate>
<Asp: dropdownlist id = "dropdownlist1" runat = "server">
<Asp: listitem value = "AAA"> AAA </ASP: listitem>
<Asp: listitem value = "BBB"> BBB </ASP: listitem>
<Asp: listitem value = "CCC"> CCC </ASP: listitem>
</ASP: dropdownlist>
</Edititemtemplate>
</ASP: templatecolumn>
<Asp: boundcolumn datafield = "name" headertext = "name"> </ASP: boundcolumn>
</Columns>
</ASP: DataGrid>
<Asp: button id = "btnaddnew" style = "Z-INDEX: 102; left: 560px; position: absolute; top: 64px"
Runat = "server" text = "addnew"> </ASP: button>
<Asp: button id = "btnsave" style = "Z-INDEX: 103; left: 560px; position: absolute; top: Running PX" runat = "server"
TEXT = "Submit"> </ASP: button>
</Form>
</Body>
</Html>

-----------------------------------------------------------------------------

Using system;
Using system. Data;
Using system. Web. UI. webcontrols;

Namespace bstest2005_8_16.samples
{
/// <Summary>
/// Multi-row input and submission abstract description.
/// </Summary>
Public class multi-line Input submission: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Button btnaddnew;
Protected system. Web. UI. webcontrols. Button btnsave;
Protected system. Web. UI. webcontrols. DataGrid datagrid1;

/// <Summary>
/// Attribute: DataSet currently being edited
/// </Summary>
Private datatable currentdata
{
Get
{
Return (viewstate ["_ currentdata"] = NULL )? Createtable () :( datatable) viewstate ["_ currentdata"];
}
Set
{
Viewstate ["_ currentdata"] = value;
}
}

private void page_load (Object sender, system. eventargs e)
{< br> // place the user Code here to initialize the page
If (! Ispostback)
{< br> This. datagrid1.datasource = This. currentdata;
This. datagrid1.databind ();
}< BR >}

Private datatable createtable ()
{
Datatable dt = new datatable ();
DT. Columns. Add ("ID ");
DT. Columns. Add ("name ");
DT. acceptchanges ();
Return DT;
}

Private void collectdatafromgrid ()
{
Datatable dt = This. currentdata;
If (Dt. Rows. Count> 0)
{
Dropdownlist ddlst = This. Maid [Maid]. cells [0]. findcontrol ("dropdownlist1") as dropdownlist;
DT. Rows [DT. Rows. Count-1] ["ID"] = ddlst. selectedvalue;
DT. rows [DT. rows. count-1] ["name"] = (textbox) This. datagrid1.items [datagrid1.edititemindex]. cells [1]. controls [0]). text;
}
This. currentdata = DT;
}
Private void btnaddnew_click (Object sender, system. eventargs E)
{
Collectdatafromgrid (); // collect input from the previous row

// Add a new empty row
datatable dt = this. currentdata;
datarow r = DT. newrow ();
DT. rows. add (r);
// locate the new row in the current edit row
This. datagrid1.edititemindex = DT. rows. count-1;
This. datagrid1.datasource = DT;
This. datagrid1.databind ();
}

Private void btnsave_click (Object sender, system. eventargs E)
{
Datatable dt = This. currentdata; // obtain the final multi-row data
// Save it to the database. There are two implementation methods: The dataadapter. Update method and the SQL statement.
}

# 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. btnaddnew. Click + = new system. eventhandler (this. btnaddnew_click );
This. btnsave. Click + = new system. eventhandler (this. btnsave_click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion


}
}

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.