<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="test1.WebForm2" %>
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 Pagetitle>
<script src="Ajax.js" type="text/javascript">script>
<script type="text/javascript">
function func()
{
var table = document.getElementById("test");
var tr = table.insertRow();//添加tr
//tr.className = "xxx";
var td1 = tr.insertCell();//添加td
BuildText(td1,"newtext1");
var td2 = tr.insertCell();//添加td
BuildText(td2,"newtext2");
var url="WebForm2.aspx?type=getdata";
var ajax = new AjaxCore.Ajax("POST",url,receiveInfo,null);
ajax.SendRequest(); }
function receiveInfo(data)
{
if(data.responseText == "false")
{}
else if(data.responseText=="load")
{}
else
{
var str = data.responseText;
var json = eval('('+str+')');
document.getElementById('newtext1').value = json.td1;
document.getElementById('newtext2').value = json.td2;
}
}
function BuildText(panel,id)
{
var input = document.createElement("input");
input.type = "text";
input.id = id;
input.name = id;
input.style.width="95%";
panel.appendChild(input);
}
script>
head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" onclick="func()" value="Add" />
<table id="test" width="50%" border="1">
<tr>
<td>ttttAtd>
<td>ttttBtd>
tr>
table>
div>
form>
body>
html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace test1
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request["type"] != null)
{
if (Request["type"].ToString() == "getdata")
{
string json = "{{\"td1\":\"{0}\",\"td2\":\"{1}\"}}";
Response.Write(string.Format(json,"aaaaa","bbbbb"));
Response.End();
}
}
}
}
}
ajax.js文件
var AjaxCore=new Object();
AjaxCore.Ajax=function(type,url,onload,params,contentType){
this.req=null;
this.url=url;
this.params=params;
if(type){
this.type=type;
}else{
this.type="GET";
}
if(!contentType && type=="POST"){
this.contentType="application/x-www-form-urlencoded; charset=UTF-8";
}else{
this.contentType=contentType;
}
this.onload=onload;
this.responseText=null;
this.GetHttpRequest();
}
AjaxCore.Ajax.prototype.GetHttpRequest=function(){
if(window.ActiveXObject){
this.req=new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
this.req=new XMLHttpRequest();
}
}
AjaxCore.Ajax.prototype.SendRequest=function(){
var loader=this;
this.req.onreadystatechange=function(){
loader.OnReadyState.call(loader);
}
this.req.open(this.type,this.url,true);
if(this.contentType){
this.req.setRequestHeader("Content-Type",this.contentType);
}
this.req.send(this.params);
}
AjaxCore.Ajax.prototype.OnReadyState=function(){
var ready=this.req.readyState;
var data;
if(ready==4){
if(this.req.status==200 || this.req.status==0){
this.responseText=this.req.responseText;
}else{
this.responseText="false";
}
}else{
this.responseText="load";
}
this.onload.call(this,this);
}
本文作者:佚名 来源:本站原创
CIO之家 www.ciozj.com 微信公众号:imciow