js获取UserControl内容为拼html时提供方便
今天看了老赵的文章可是怎么都调试不通过。
代码如下:
[AjaxPro.AjaxMethod] public string gethtml() { UcViewHelper<UserControl> viewManager = new UcViewHelper<UserControl>(); UserControl control = viewManager.LoadViewControl("~/uc/giftoutmodel.ascx"); string s=viewManager.RenderView(control); return s; } public class UcViewHelper<T> where T : UserControl { private MyPage m_pageHolder; public T LoadViewControl(string path) { m_pageHolder = new MyPage(); return (T)m_pageHolder.LoadControl(path); } public string RenderView(T control) { StringWriter output = new StringWriter(); this.m_pageHolder.Controls.Add(control); HttpContext.Current.Server.Execute(this.m_pageHolder, output, false); return output.ToString(); } } class MyPage : Page { public override void VerifyRenderingInServerForm(Control control) { //if (control is GridView || control is UserControl) //{ // return; //} //base.VerifyRenderingInServerForm(control); } }
测试通过。
如果:
代码如下:
[AjaxPro.AjaxMethod] public string gethtml() { string s = getString(); return s; } public string getString() { UserControl control = LoadControl("~/uc/giftoutmodel.ascx") as UserControl; StringWriter tw = new StringWriter(); HtmlTextWriter writer = new HtmlTextWriter(tw); control.RenderControl(writer); return writer.InnerWriter.ToString(); } public override void VerifyRenderingInServerForm(Control control) { // if (control is GridView || control is UserControl) // { // return; //} //base.VerifyRenderingInServerForm(control); }
究其原因,原来是老赵的代码 继承了Page 然后使用VerifyRenderingInServerForm验证,二我的代码没有继承Page直接使用VerifyRenderingInServerForm 所以会造成
相关推荐
gufudhn 2020-06-06
nercon 2020-06-02
梦的天空 2020-08-25
88274956 2020-11-03
runner 2020-09-01
移动开发与培训 2020-08-16
ReunionIsland 2020-08-16
lyqdanang 2020-08-16
MyNameIsXiaoLai 2020-07-08
星辰的笔记 2020-07-04
csstpeixun 2020-06-28
letheashura 2020-06-26
liaoxuewu 2020-06-26
sunzhihaofuture 2020-06-21
FEvivi 2020-06-16
坚持着执着 2020-06-16
waterv 2020-06-14
xiaoge00 2020-06-14
firejq 2020-06-14
firstboy0 2020-06-14
e度空间 2020-06-12