动态的呈现页面. 模拟ASP.NET UpdatePanel部分更新配合WebServices.
2009-09-24 17:37
519 查看
在平常工作中,我们经常遇到需要将页面GridView导出到EXCEL, 通常做法我们就是动态构建以个Page及Form,然后写到返回流(Response)中.
using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Reflection;
using System.Web.UI.HtmlControls;
public class ViewManager
{
public static string RenderView(string path)
{
return RenderView(path, null);
}
public static string RenderView(string path, object data)
{
Page pageHolder = new Page();
UserControl viewControl = (UserControl) pageHolder.LoadControl(path);
HtmlForm hf = new HtmlForm();
hf.Attributes.Add("runat", "server");
if (data != null)
{
Type viewControlType = viewControl.GetType();
FieldInfo field = viewControlType.GetField("Data");
if (field != null)
{
field.SetValue(viewControl, data);
}
else
{
throw new Exception("View file: " + path + " does not have a public Data property");
}
}
hf.Controls.Add(viewControl);
pageHolder.Controls.Add(hf);
StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, output, false);
return output.ToString();
}
}
//
protected void Button1_Click(object sender, EventArgs e)
{
List<string> obj = new List<string>();
for (int i = 0; i < 10; i++)
obj.Add(i.ToString());
string str = ViewManager.RenderView("UserControls/User.ascx", obj);
Response.Write(str);
}More:http://blog.joycode.com/scottgu/archive/2006/10.aspx
using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Reflection;
using System.Web.UI.HtmlControls;
public class ViewManager
{
public static string RenderView(string path)
{
return RenderView(path, null);
}
public static string RenderView(string path, object data)
{
Page pageHolder = new Page();
UserControl viewControl = (UserControl) pageHolder.LoadControl(path);
HtmlForm hf = new HtmlForm();
hf.Attributes.Add("runat", "server");
if (data != null)
{
Type viewControlType = viewControl.GetType();
FieldInfo field = viewControlType.GetField("Data");
if (field != null)
{
field.SetValue(viewControl, data);
}
else
{
throw new Exception("View file: " + path + " does not have a public Data property");
}
}
hf.Controls.Add(viewControl);
pageHolder.Controls.Add(hf);
StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, output, false);
return output.ToString();
}
}
//
protected void Button1_Click(object sender, EventArgs e)
{
List<string> obj = new List<string>();
for (int i = 0; i < 10; i++)
obj.Add(i.ToString());
string str = ViewManager.RenderView("UserControls/User.ascx", obj);
Response.Write(str);
}More:http://blog.joycode.com/scottgu/archive/2006/10.aspx
相关文章推荐
- asp.net页面时间实时更新—UpdatePanel的应用
- asp.net页面时间实时更新—UpdatePanel的应用
- asp.net ajax updatepanel控件使用(网页自动部分更新)
- Asp.net UpdatePanel在asyncpostback后页面标题丢失的BUG解决
- ASP.NET UpdatePanel中动态添加的控件需要ID才起作用
- ASP.NET UpdatePanel实时更新时间
- 结合UpdatePanel实例讲解ASP.NET的页面缓存技术
- asp.net ajax 使用updatepanel进行更新后的提示
- [ASP.NET AJAX]谨记给UpdatePanel中动态添加的控件赋ID
- UpdatePanel 和 Repeater实现页面局部数据无刷新 (asp.net C#)
- 【ASP.NET】利用UpdatePanel、ScriptManager、Timer完成定时局部更新
- asp.net updatepanel里gridview更新需要点鼠标两次问题解决
- ASP.NET后台输出js大全,页面顶部、form表单中前面与后面、和UpdatePanel(ScriptManager、AJAX)输出JS
- ASP.NET Ajax 调试技巧:用 FireBug 调试 UpdatePanel 不更新问题
- 学习笔记:Asp.Net MVC更新部分页面
- asp.net ajax实现页面的动态更新
- asp.net ajax 使用updatepanel进行更新后的提示
- asp.netajax使用updatepanel进行更新后的提示
- 新写的一个使用ASP.NET AJAX中的UpdatePanel控件实现GridView的无刷新删除,更新,添加,查询!
- ASP.NET UpdatePanel页面异常提示处理