欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net中利用ajax獲取動態(tài)創(chuàng)建表中文本框的值

 更新時間:2010年03月09日 22:07:18   作者:  
通常在做主從表的數(shù)據(jù)錄入中,會碰到在一個頁面上同時錄入主表數(shù)據(jù)和從表數(shù)據(jù),主表的數(shù)據(jù)只有一條,從表的數(shù)據(jù)有一條到多條,這樣就要動態(tài)創(chuàng)建從表數(shù)據(jù)錄入入口。
假設(shè)現(xiàn)在主表為公司表(公司ID,公司名稱,公司類型,公司規(guī)模),從表為部門表(部門ID,公司ID,經(jīng)理,聯(lián)系電話),現(xiàn)在一個公司有四個部門,要在同一個頁面上錄入公司信息以及四個部門的信息,如何動態(tài)創(chuàng)建部門信息錄入口,以及如何獲取數(shù)據(jù)存儲到數(shù)據(jù)庫中,請看下面的代碼。
頁面HTML代碼及js腳本
代碼
復(fù)制代碼 代碼如下:

<%@ 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>
公司規(guī)模:</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>
經(jīng)理</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>

后置代碼
代碼
復(fù)制代碼 代碼如下:

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("國營企業(yè)", "1"));
ddlCompanyType.Items.Add(new ListItem("民營企業(yè)", "2"));
ddlCompanyType.Items.Add(new ListItem("外資企業(yè)", "3"));
ddlCompanyType.SelectedValue = "1";
}
}
[System.Web.Services.WebMethod]
public static string GetData(string companyName, string companySize, string companyType, string[] depts)
{
StringBuilder buider = new StringBuilder();//顯示一下提取到的數(shù)據(jù)
buider.AppendLine(string.Format("公司名稱:{0}", companyName));
buider.AppendLine(string.Format("公司規(guī)模:{0}", companySize));
buider.AppendLine(string.Format("公司性質(zhì):{0}", companyType));
CompanyInfo info = new CompanyInfo(companyName, companySize, companyType);//將數(shù)據(jù)插入到公司實(shí)體對象中
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},經(jīng)理:{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);//將數(shù)據(jù)插入到部門實(shí)體對象List集合中
}
//數(shù)據(jù)提取出來插入到數(shù)據(jù)庫就是很簡單的事情了。
 
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實(shí)現(xiàn)動態(tài)新增一行、刪除指定行的操作,然后利用AJAX的PageMethod方式,調(diào)用后臺代碼實(shí)現(xiàn)數(shù)據(jù)提取,然后把數(shù)據(jù)裝載到公司實(shí)體對象與部門實(shí)體對象集合中,提交到數(shù)據(jù)庫(這部分沒有去實(shí)現(xiàn),不用多說了,大家都會)。其中需要注意幾個方面
、必須在 ScriptManager 設(shè)置 EnablePageMethods="true",這樣才能使用PageMethod方式
、在JS中調(diào)用的服務(wù)端函數(shù)必須加上[System.Web.Services.WebMethod]
其它的代碼太簡單,就不用一一說明。

相關(guān)文章

最新評論