Use Ajax in Asp.net to obtain the value of the text box in the dynamically created table

Source: Internet
Author: User

Assume that the current main table is a company table (company ID, company name, company type, company size), and the table is a department table (Department ID, company ID, Manager, contact number ), currently, a company has four departments. On the same page, you need to enter company information and information about the four departments, dynamically create department information input ports, and obtain and store data in databases, see the following Code .
Page HTML code and JS script
Code Copy code The Code is as follows: <% @ page Language = "C #" autoeventwireup = "true" codebehind = "default. aspx. cs" inherits = "webapp. _ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
<Script language = "JavaScript" type = "text/JavaScript">
Function addrow ()
{
VaR TBL = Document. getelementbyid ("TBL ");
VaR rows = TBL. Rows. length;
VaR TR = TBL. insertrow (rows );
VaR TD;
For (VAR I = 0; I <4; I ++)
{
TD = tr. insertcell (I );
If (I = 3)
TD. innerhtml = "<a id = 'a" + rows + "'href = '# 'onclick = 'delrow (this)'> Delete </a> ";
Else
TD. innerhtml = "<input id = 'txt" + rows + I + "'Type = 'text'/> ";
}
}
Function delrow (OBJ)
{
VaR TBL = Document. getelementbyid ("TBL ");
TBL. deleterow (obj. parentnode. parentnode. rowindex );
}
Function getpagedata ()
{
VaR companyName = Document. getelementbyid ("txtcompanyname ");
VaR companysize = Document. getelementbyid ("txtcompanysize ");
VaR companytype = Document. getelementbyid ("ddlcompanytype ");
VaR TBL = Document. getelementbyid ("TBL ");
VaR txt;
VaR datas = new array (TBL. Rows. Length-1 );
For (VAR I = 1; I <TBL. Rows. length; I ++)
{
TXT = TBL. Rows [I]. getelementsbytagname ("input ");
Datas [I-1] = TXT [0]. Value + "," + TXT [1]. Value + "," + TXT [2]. value;
}
Pagemethods. getdata (companyName. Value, companysize. Value, companytype. Options [companytype. selectedindex]. Value, datas, showresult );
}
Function showresult (MSG)
{
Alert (MSG );
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: scriptmanager id = "scriptmanager1" runat = "server" enablepagemethods = "true"> </ASP: scriptmanager>
<Table>
<Tr>
<TD>
Company Name: </TD>
<TD>
<Asp: textbox id = "txtcompanyname" runat = "server"> </ASP: textbox> </TD>
<TD>
Company scale: </TD>
<TD>
<Asp: textbox id = "txtcompanysize" runat = "server"> </ASP: textbox> </TD>
<TD>
Company type: </TD>
<TD>
<Asp: dropdownlist id = "ddlcompanytype" runat = "server">
</ASP: dropdownlist> </TD>
</Tr>
</Table>
<Input id = "btnaddrow" type = "button" value = "Add a new row" onclick = "addrow ();"/>
<Table id = "TBL">
<Tr>
<TD>
Department </TD>
<TD>
Phone number </TD>
<TD>
Manager </TD>
<TD>
</TD>
</Tr>
<Tr>
<TD>
<Input id = "txt10" type = "text"/> </TD>
<TD>
<Input id = "txt11" type = "text"/> </TD>
<TD>
<Input id = "txt12" type = "text"/> </TD>
<TD>
<A id = "A1" href = '# 'onclick = "delrow (this)"> Delete </a> </TD>
</Tr>
</Table>
<Input id = "btnok" type = "button" value = "OK" onclick = "getpagedata ();"/>
<Br/>
</Form>
</Body>
</Html>

Post Code
Code Copy code The Code is as follows: using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Collections. Generic;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. text;
Using system. Web. UI. htmlcontrols;
Namespace webapp
{
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
// Bind company type
Ddlcompanytype. Items. Add (New listitem ("state-owned enterprise", "1 "));
Ddlcompanytype. Items. Add (New listitem ("private enterprise", "2 "));
Ddlcompanytype. Items. Add (New listitem ("Foreign Enterprise", "3 "));
Ddlcompanytype. selectedvalue = "1 ";
}
}
[System. Web. Services. webmethod]
Public static string getdata (string companyName, string companysize, string companytype, string [] depts)
{
Stringbuilder buider = new stringbuilder (); // display the extracted data
Buider. appendline (string. Format ("Company Name: {0}", companyName ));
Buider. appendline (string. Format ("company size: {0}", companysize ));
Buider. appendline (string. Format ("company nature: {0}", companytype ));
Companyinfo info = new companyinfo (companyName, companysize, companytype); // insert data to a company object
List <departmentinfo> Infos = new list <departmentinfo> ();
Departmentinfo info1 = NULL;
String [] temp;
For (INT I = 0; I <Depts. length; I ++)
{
Temp = depts [I]. Split (New char [] {','});
Buider. appendline (string. format ("Department: {0}, Manager: {1}, tel: {2}", temp [0], temp [1], temp [2]);
Info1 = new departmentinfo ();
Info1.deptname = temp [0];
Info1.mamanger = temp [1];
Info1.phone = temp [2];
Infos. Add (info1); // insert data to the Department Object List set.
}
// It is easy to extract data and insert it into the database.

Return buider. tostring ();
}
}
Public class companyinfo
{
Private string _ companyName;
Private string _ companysize;
Private string _ companytype;
Public String companytype
{
Get {return _ companytype ;}
Set {_ companytype = value ;}
}
Public String companyName
{
Get {return _ companyName ;}
Set {_ companyName = value ;}
}
Public String companysize
{
Get {return _ companysize ;}
Set {_ companysize = value ;}
}
Public companyinfo ()
{}
Public companyinfo (string companyName, string companysize, string companytype)
{
This. _ companyName = companyName;
This. _ companysize = companysize;
This. _ companytype = companytype;
}
}
Public class departmentinfo
{
Private string _ deptname;
Private string _ mamanger;
Private string _ phone;
Public String phone
{
Get {return _ phone ;}
Set {_ phone = value ;}
}
Public String mamanger
{
Get {return _ mamanger ;}
Set {_ mamanger = value ;}
}
Public String deptname
{
Get {return _ deptname ;}
Set {_ deptname = value ;}
}
Public departmentinfo ()
{
}
}
}

first, JavaScript is used to dynamically add and delete a row, and then Ajax pagemethod is used to call background code for data extraction, then, load the data to the entity object set of the company and the entity object set of the Department, and submit the data to the database (this part is not implemented, so you don't need to talk about it, everyone will ). Note the following:
you must set enablepagemethods = "true" in scriptmanager to use the pagemethod method.
you must add [system. web. services. webmethod]
other codes are too simple to be described one by one.

Related Article

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.