ASP.NET:获取标记为的.ascx [英] ASP.NET: Getting markup for .ascx

查看:78
本文介绍了ASP.NET:获取标记为的.ascx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一个一个的.ascx用户控件。我如何获得它的HTML标记为一个字符串?

Let's say I have an a .ascx user control. How do I get its HTML markup into a string?

推荐答案

在您使用LoadControl()方法得到它变成一个页面,你可以可以通过调用它的渲染()方法获取HTML。它需要的HtmlTextWriter写,但它是相当简单的构造:

Once you use the LoadControl() method to get it into a page, you can can get the HTML by calling the Render() method on it. It requires an HtmlTextWriter to write to, but it's fairly straightforward to construct:

var userControl = (userControlType)Page.LoadControl( ....ascx);
StringBuilder sb = new StringBuilder();
StringWriter SW = new StringWriter(SB);
HtmlTextWriter htw = new HtmlTextWriter(new StringWriter(sb));
userControl.RenderControl(htw);

string html = sb.ToString();

如果你的页面上下文中不是,还有的等方式来做到这一点。

If you're not inside the page context, there are other ways to do it.

这篇关于ASP.NET:获取标记为的.ascx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆