由于Ajax方法返回必须是一个字符串,所以可将GridView绑定后生成的HTML代码作为返回值,这里我偷了一个懒,直接使用Coolite的Label来存GridView的HTML代码,让Coolite的Label自己更新,这样我不用自己要将HTML代码返回客户端,再写JS来呈现。
写一个用户控件,拖放GridView,按自己需要进行绑定。用户控件提供一个返回GenerInnerHTML的公共方法,用来返回GridView绑定之后生成的HTML代码。
完整代码:
GenerGridViewInnerHtml.ascx
view plaincopy to clipboardprint?
01.<%@ Control Language="C#" AutoEventWireup="true" CodeFile="GenerGridViewInnerHtml.ascx.cs"
02. Inherits="CooliteAjaxMethodsSamples_GenerGridViewInnerHtml" %>
03.<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
04. ForeColor="#333333" GridLines="None" Width="691px">
05. <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
06. <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
07. <Columns>
08. <asp:BoundField DataField="Id" HeaderText="编号" />
09. <asp:BoundField DataField="Name" HeaderText="Name" />
10. <asp:BoundField DataField="Addr" HeaderText="Addr" />
11. <asp:TemplateField HeaderText="操作">
12. <ItemTemplate>
13. <a href="javascript:delSingleRow('<%# Eval(" mce_href="javascript:delSingleRow('<%# Eval("Id") %>');">删除</a>
14. </ItemTemplate>
15. </asp:TemplateField>
16. </Columns>
17. <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
18. <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
19. <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
20. <AlternatingRowStyle BackColor="White" />
21.</asp:GridView>
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="GenerGridViewInnerHtml.ascx.cs"
Inherits="CooliteAjaxMethodsSamples_GenerGridViewInnerHtml" %>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" Width="691px">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="编号" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Addr" HeaderText="Addr" />
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<a href="javascript:delSingleRow('<%# Eval(" mce_href="javascript:delSingleRow('<%# Eval("Id") %>');">删除</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
GenerGridViewInnerHtml.ascx.cs
view plaincopy to clipboardprint?
01.using System;
02.using System.Collections;
03.using System.Configuration;
04.using System.Data;
05.using System.Linq;
06.using System.Web;
07.using System.Web.Security;
08.using System.Web.UI;
09.using System.Web.UI.HtmlControls;
10.using System.Web.UI.WebControls;
11.using System.Web.UI.WebControls.WebParts;
12.using System.Xml.Linq;
13.using System.IO;
14.
15.public partial class CooliteAjaxMethodsSamples_GenerGridViewInnerHtml : System.Web.UI.UserControl
16.{
17. public string GenerInnerHTML(Object dataSource)
18. {
19. GridView1.DataSource = dataSource;
20. GridView1.DataBind();
21.
22. StringWriter sw = new StringWriter();
23. HtmlTextWriter writer = new HtmlTextWriter(sw);
24. GridView1.RenderControl(writer);
25. return sw.ToString();
26. }
27.}
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;
using System.IO;
public partial class CooliteAjaxMethodsSamples_GenerGridViewInnerHtml : System.Web.UI.UserControl
{
public string GenerInnerHTML(Object dataSource)
{
GridView1.DataSource = dataSource;
GridView1.DataBind();
StringWriter sw = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(sw);
GridView1.RenderControl(writer);
return sw.ToString();
}
}
ReurbishGridView.aspx
view plaincopy to clipboardprint?
01.<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReurbishGridView.aspx.cs" EnableEventValidation="false" Inherits="CooliteAjaxMethodsSamples_ReurbishGridView" %>
02.
03.<%@ Register Assembly="Coolite.Ext.Web" Namespace="Coolite.Ext.Web" TagPrefix="ext" %>
04.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05.
06.<html xmlns="http://www.w3.org/1999/xhtml">
07.<head id="Head1" runat="server">
08. <title>无标题页</title>
09. <mce:script language="javascript" type="text/javascript"><!--
10. function addRowToGrid()
11. {
12. var name = document.getElementById("TB_Name").value;
13. var addr = document.getElementById("TB_Addr").value;
14.
15. Coolite.AjaxMethods.AddRowToGrid(name,addr,{
16. eventMask:{
17. showMask: true,
18. minDelay: 500,
19. msg: '正在加载,请稍后...'
20. }
21. });
22. }
23.
24. function delSingleRow(id)
25. {
26. Coolite.AjaxMethods.DelSingleRow(id,{
27. eventMask:{
28. showMask: true,
29. minDelay: 500,
30. msg: '正在加载,请稍后...'
31. }
32. });
33. }
34.
35.// --></mce:script>
36.</head>
37.<body>
38. <form id="form1" runat="server">
39. <div>
40. <ext:ScriptManager ID="s1" runat="server"></ext:ScriptManager>
41. Name:<asp:TextBox ID="TB_Name" runat="server"></asp:TextBox><br />
42. Addr:<asp:TextBox ID="TB_Addr" runat="server"></asp:TextBox><br />
43. <button id="BT_Add" οnclick="addRowToGrid();">增加</button>
44.
45. <ext:Label runat="server" ID="GridContainer"></ext:Label>
46. <ext:Hidden runat="server" ID="m_viewStateLb"></ext:Hidden>
47. </div>
48. </form>
49.</body>
50.</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReurbishGridView.aspx.cs" EnableEventValidation="false" Inherits="CooliteAjaxMethodsSamples_ReurbishGridView" %>
<%@ Register Assembly="Coolite.Ext.Web" Namespace="Coolite.Ext.Web" TagPrefix="ext" %>
<!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 id="Head1" runat="server">
<title>无标题页</title>
<mce:script language="javascript" type="text/javascript"><!--
function addRowToGrid()
{
var name = document.getElementById("TB_Name").value;
var addr = document.getElementById("TB_Addr").value;
Coolite.AjaxMethods.AddRowToGrid(name,addr,{
eventMask:{
showMask: true,
minDelay: 500,
msg: '正在加载,请稍后...'
}
});
}
function delSingleRow(id)
{
Coolite.AjaxMethods.DelSingleRow(id,{
eventMask:{
showMask: true,
minDelay: 500,
msg: '正在加载,请稍后...'
}
});
}
// --></mce:script>
</head>
<body>
<form id="form1" runat="server">
<div>
<ext:ScriptManager ID="s1" runat="server"></ext:ScriptManager>
Name:<asp:TextBox ID="TB_Name" runat="server"></asp:TextBox><br />
Addr:<asp:TextBox ID="TB_Addr" runat="server"></asp:TextBox><br />
<button id="BT_Add" οnclick="addRowToGrid();">增加</button>
<ext:Label runat="server" ID="GridContainer"></ext:Label>
<ext:Hidden runat="server" ID="m_viewStateLb"></ext:Hidden>
</div>
</form>
</body>
</html>
ReurbishGridView.aspx.cs
view plaincopy to clipboardprint?
01.using System;
02.using System.Collections;
03.using System.Configuration;
04.using System.Data;
05.using System.Linq;
06.using System.Web;
07.using System.Web.Security;
08.using System.Web.UI;
09.using System.Web.UI.HtmlControls;
10.using System.Web.UI.WebControls;
11.using System.Web.UI.WebControls.WebParts;
12.using System.Xml.Linq;
13.using Coolite.Ext.Web;
14.using System.Collections.Generic;
15.
16.public partial class CooliteAjaxMethodsSamples_ReurbishGridView : System.Web.UI.Page
17.{
18. protected void Page_Load(object sender, EventArgs e)
19. {
20. if (!IsPostBack && !Ext.IsAjaxRequest)
21. {
22. Ini();
23. }
24. }
25.
26. private void Ini()
27. {
28. SetViewState(c_gridSourceKey, GetTestObject);
29. GridContainer.Html = GetGridViewInnerHTML();
30. }
31.
32. private const string c_gridSourceKey = "grid";
33.
34. [AjaxMethod]
35. public void AddRowToGrid(string name, string addr)
36. {
37. List<People> list = GetViewState(c_gridSourceKey) as List<People>;
38. int id = list.Count == 0 ? 0 : ((People)list[list.Count - 1]).Id + 1;
39. People p = new People(id,name, addr);
40. list.Add(p);
41. SetViewState(c_gridSourceKey, list);
42.
43. GridContainer.Html = GetGridViewInnerHTML();
44. }
45.
46. [AjaxMethod]
47. public void DelSingleRow(string id)
48. {
49. List<People> oldList = GetViewState(c_gridSourceKey) as List<People>;
50. List<People> newList = new List<People>();
51. foreach (People p in oldList)
52. {
53. if (p.Id != Convert.ToInt32(id))
54. newList.Add(p);
55. }
56.
57. SetViewState(c_gridSourceKey, newList);
58. GridContainer.Html = GetGridViewInnerHTML();
59. }
60.
61. private string GetGridViewInnerHTML()
62. {
63. List<People> data = GetViewState(c_gridSourceKey) as List<People>;
64. CooliteAjaxMethodsSamples_GenerGridViewInnerHtml uctl = this.LoadControl("GenerGridViewInnerHTML.ascx") as CooliteAjaxMethodsSamples_GenerGridViewInnerHtml;
65. return uctl.GenerInnerHTML(data);
66. }
67.
68. public override void VerifyRenderingInServerForm(Control control)
69. {
70. //如果control是GridView,跳过验证,这里最好的验证是验证control的Id
71. if (control is System.Web.UI.WebControls.GridView || control is System.Web.UI.WebControls.LinkButton)
72. {
73. return;
74. }
75. base.VerifyRenderingInServerForm(control);
76. }
77.
78. private List<People> GetTestObject
79. {
80. get
81. {
82. List<People> list = new List<People>();
83. list.Add(new People(1,"张三", "四川成都市"));
84. list.Add(new People(2,"邢林莉", "河南郑洲市"));
85. list.Add(new People(3,"李小林", "泸洲上新区"));
86.
87. return list;
88. }
89. }
90.
91. #region ------处理自定义的ViewState(模拟ViewState,保存GridView的数据源)------
92.
93. private Dictionary<string, object> m_innerViewState;
94.
95. private void TryBuildViewState()
96. {
97. if (m_innerViewState == null)
98. {
99. LoadState();
100. }
101. }
102.
103. private void UpdateState()
104. {
105. m_viewStateLb.Text = H2O.Basic.ObjectStringFormatter.ToBase64String(this.m_innerViewState);
106. }
107.
108. private void LoadState()
109. {
110. if (string.IsNullOrEmpty(m_viewStateLb.Text))
111. {
112. this.m_innerViewState = new Dictionary<string, object>();
113. return;
114. }
115. this.m_innerViewState = (Dictionary<string, object>)H2O.Basic.ObjectStringFormatter.FromBase64String(m_viewStateLb.Text);
116. }
117.
118. public void SetViewState(string key, object obj)
119. {
120. TryBuildViewState();
121. m_innerViewState[key] = obj;
122. UpdateState();
123. }
124.
125. public object GetViewState(string key)
126. {
127. object r;
128. TryBuildViewState();
129. if (m_innerViewState.TryGetValue(key, out r))
130. return r;
131. return null;
132. }
133.
134. #endregion
135.
136. [Serializable]
137. public class People
138. {
139. private int m_id;
140. private string m_name, m_addr;
141.
142. public People(int id, string name, string addr)
143. {
144. m_id = id;
145. m_name = name;
146. m_addr = addr;
147. }
148.
149. public int Id
150. {
151. get { return m_id; }
152. set { m_id = value; }
153. }
154.
155. public string Name
156. {
157. get { return m_name; }
158. set { m_name = value; }
159. }
160.
161. public string Addr
162. {
163. get { return m_addr; }
164. set { m_addr = value; }
165. }
166. }
167.}
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;
using Coolite.Ext.Web;
using System.Collections.Generic;
public partial class CooliteAjaxMethodsSamples_ReurbishGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && !Ext.IsAjaxRequest)
{
Ini();
}
}
private void Ini()
{
SetViewState(c_gridSourceKey, GetTestObject);
GridContainer.Html = GetGridViewInnerHTML();
}
private const string c_gridSourceKey = "grid";
[AjaxMethod]
public void AddRowToGrid(string name, string addr)
{
List<People> list = GetViewState(c_gridSourceKey) as List<People>;
int id = list.Count == 0 ? 0 : ((People)list[list.Count - 1]).Id + 1;
People p = new People(id,name, addr);
list.Add(p);
SetViewState(c_gridSourceKey, list);
GridContainer.Html = GetGridViewInnerHTML();
}
[AjaxMethod]
public void DelSingleRow(string id)
{
List<People> oldList = GetViewState(c_gridSourceKey) as List<People>;
List<People> newList = new List<People>();
foreach (People p in oldList)
{
if (p.Id != Convert.ToInt32(id))
newList.Add(p);
}
SetViewState(c_gridSourceKey, newList);
GridContainer.Html = GetGridViewInnerHTML();
}
private string GetGridViewInnerHTML()
{
List<People> data = GetViewState(c_gridSourceKey) as List<People>;
CooliteAjaxMethodsSamples_GenerGridViewInnerHtml uctl = this.LoadControl("GenerGridViewInnerHTML.ascx") as CooliteAjaxMethodsSamples_GenerGridViewInnerHtml;
return uctl.GenerInnerHTML(data);
}
public override void VerifyRenderingInServerForm(Control control)
{
//如果control是GridView,跳过验证,这里最好的验证是验证control的Id
if (control is System.Web.UI.WebControls.GridView || control is System.Web.UI.WebControls.LinkButton)
{
return;
}
base.VerifyRenderingInServerForm(control);
}
private List<People> GetTestObject
{
get
{
List<People> list = new List<People>();
list.Add(new People(1,"张三", "四川成都市"));
list.Add(new People(2,"邢林莉", "河南郑洲市"));
list.Add(new People(3,"李小林", "泸洲上新区"));
return list;
}
}
#region ------处理自定义的ViewState(模拟ViewState,保存GridView的数据源)------
private Dictionary<string, object> m_innerViewState;
private void TryBuildViewState()
{
if (m_innerViewState == null)
{
LoadState();
}
}
private void UpdateState()
{
m_viewStateLb.Text = H2O.Basic.ObjectStringFormatter.ToBase64String(this.m_innerViewState);
}
private void LoadState()
{
if (string.IsNullOrEmpty(m_viewStateLb.Text))
{
this.m_innerViewState = new Dictionary<string, object>();
return;
}
this.m_innerViewState = (Dictionary<string, object>)H2O.Basic.ObjectStringFormatter.FromBase64String(m_viewStateLb.Text);
}
public void SetViewState(string key, object obj)
{
TryBuildViewState();
m_innerViewState[key] = obj;
UpdateState();
}
public object GetViewState(string key)
{
object r;
TryBuildViewState();
if (m_innerViewState.TryGetValue(key, out r))
return r;
return null;
}
#endregion
[Serializable]
public class People
{
private int m_id;
private string m_name, m_addr;
public People(int id, string name, string addr)
{
m_id = id;
m_name = name;
m_addr = addr;
}
public int Id
{
get { return m_id; }
set { m_id = value; }
}
public string Name
{
get { return m_name; }
set { m_name = value; }
}
public string Addr
{
get { return m_addr; }
set { m_addr = value; }
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/llxchen/archive/2009/12/21/5047038.aspx