asp.net中利用ajax擷取動態建立表中文字框的值

來源:互聯網
上載者:User

假設現在主表為公司表(公司ID,公司名稱,公司類型,公司規模),從表為部門表(部門ID,公司ID,經理,聯絡電話),現在一個公司有四個部門,要在同一個頁面上錄入公司資訊以及四個部門的資訊,如何動態建立部門資訊錄入口,以及如何擷取資料存放區到資料庫中,請看下面的代碼。
頁面HTML代碼及js指令碼
代碼 複製代碼 代碼如下:<%@ 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)'>刪除</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>
公司名稱:</td>
<td>
<asp:TextBox ID="txtCompanyName" runat="server"></asp:TextBox></td>
<td>
公司規模:</td>
<td>
<asp:TextBox ID="txtCompanySize" runat="server"></asp:TextBox></td>
<td>
公司類型:</td>
<td>
<asp:DropDownList ID="ddlCompanyType" runat="server">
</asp:DropDownList></td>
</tr>
</table>
<input id="btnAddRow" type="button" value="新增一行" onclick="addRow();" />
<table id="tbl">
<tr>
<td>
部門</td>
<td>
電話</td>
<td>
經理</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)">刪除</a></td>
</tr>
</table>
<input id="btnOK" type="button" value="確定" onclick="getPageData();" />
<br />
</form>
</body>
</html>

後置代碼
代碼 複製代碼 代碼如下: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)
{
//綁定公司類型
ddlCompanyType.Items.Add(new ListItem("國營企業", "1"));
ddlCompanyType.Items.Add(new ListItem("民營企業", "2"));
ddlCompanyType.Items.Add(new ListItem("外資企業", "3"));
ddlCompanyType.SelectedValue = "1";
}
}
[System.Web.Services.WebMethod]
public static string GetData(string companyName, string companySize, string companyType, string[] depts)
{
StringBuilder buider = new StringBuilder();//顯示一下提取到的資料
buider.AppendLine(string.Format("公司名稱:{0}", companyName));
buider.AppendLine(string.Format("公司規模:{0}", companySize));
buider.AppendLine(string.Format("公司性質:{0}", companyType));
CompanyInfo info = new CompanyInfo(companyName, companySize, companyType);//將資料插入到公司實體物件中
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("部門:{0},經理:{1},電話:{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);//將資料插入到部門實體物件List集合中
}
//資料提取出來插入到資料庫就是很簡單的事情了。
 
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()
{
}
}
}

首先是用JS實現動態新增一行、刪除指定行的操作,然後利用AJAX的PageMethod方式,調用後台代碼實現資料提取,然後把資料裝載到公司實體物件與部門實體物件集合中,提交到資料庫(這部分沒有去實現,不用多說了,大家都會)。其中需要注意幾個方面
、必須在 ScriptManager 設定 EnablePageMethods="true",這樣才能使用PageMethod方式
、在JS中調用的服務端函數必須加上[System.Web.Services.WebMethod]
其它的代碼太簡單,就不用一一說明。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.