使用ajax post提交,改变repeater1 的绑定, 后台运行绑定完成,但是页面还是原来的数据,小弟要实现这种效果,希望大哥们帮帮忙?
页面代码,点击 readio 后提交到后台
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PostClient._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>无标题页</title>
<script type="text/javascript" language="javascript">
// Ajax
function xmlHttpInitializtions()
{
try
{
xmlhttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
xmlhttpRequest = false;
}
}
if (!xmlhttpRequest && typeof XMLHttpRequest != 'undefined')
{
xmlhttpRequest = new XMLHttpRequest();
}
}
/* 处理航空公司 */
function AirCompany(code)
{
alert(code);
xmlHttpInitializtions();
var url = "Default.aspx?id=123&code="+code;
xmlhttpRequest.open("POST",url,true);
xmlhttpRequest.onreadystatechange = CallBack;
//xmlhttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttpRequest.send();
}
function CallBack()
{
if(xmlhttpRequest.readyState== 4 && xmlhttpRequest.status==200)
{
// responseStr = xmlhttpRequest.responseText;
// if(responseStr=="True")
// {
// alert('运行正常!');
// }
// else
// {
// alert("程序异常!");
// }
}
}
</script>
</head>
<body>
<input id="Radio1" name="ss" type="radio" onclick="AirCompany('ca')" />
<form id="form1" runat="server">
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<td>姓名</td>
<td>年龄</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("Namge")%>
</td>
<td>
<%#Eval("Age")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
后台代码:
Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace PostClient
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Params["id"] == "123" && Request.Params["code"] != null && this.Form.Method == "post")
{
PageBindPost();
}
else
{
PageBindPerson();
}
}
}
/*
* 重新绑定Repeater1
* 这里绑定不了
*/
private void PageBindPost()
{
Person p9 = new Person();
p9.Age = 23;
p9.Namge = "cait";
System.Collections.Generic.List<Person> plist = new System.Collections.Generic.List<Person>();
plist.Add(p9);
this.Repeater1.DataSource = plist;
}
private void PageBindPerson()
{
Person p1 = new Person();
Person p2 = new Person();
Person p3 = new Person();
p1.Namge = "李四";
p1.Age = 43;
p2.Namge = "张三";
p2.Age = 34;
p3.Namge = "王五";
p3.Age = 22;
System.Collections.Generic.List<Person> list = new System.Collections.Generic.List<Person>();
list.Add(p2);
list.Add(p1);
list.Add(p3);
this.Repeater1.DataSource = list;
this.Repeater1.DataBind();
}
}
}