ASP.NET自定义控件,我可以从a获取数据吗<;模板>;并将其转换为字符串?
我正在尝试创建一个asp.net自定义控件,其字段如下:ASP.NET自定义控件,我可以从a获取数据吗<;模板>;并将其转换为字符串?,asp.net,custom-controls,Asp.net,Custom Controls,我正在尝试创建一个asp.net自定义控件,其字段如下: <ErrorTemplate> <h1>Error</h1> <p>Blue wizard shot the food</p> </ErrorTemplate> 这是一个asp.net占位符控件-它必须被实例化为能够支持控件的内容 然后,我想使用JQuery和一个变量将其添加到我的页面中,如下所示: string script = "var Errortemplat
<ErrorTemplate>
<h1>Error</h1>
<p>Blue wizard shot the food</p>
</ErrorTemplate>
这是一个asp.net占位符控件-它必须被实例化为能够支持控件的内容
然后,我想使用JQuery和一个变量将其添加到我的页面中,如下所示:
string script = "var Errortemplate = " + errorPHolder.ToString() + ";";
scriptmanager.register(script); // pseudocode
因此,在我的页面上,结果是:
var Errortemplate = '<h1>Error</h1><p>Blue wizard shot the food</p>';
var Errortemplate='ErrorBlue wizard拍摄食物';
然后我可以使用JQuery做someDiv.html(Errortemplate)代码>
最终导致他们在
中放入的内容出现在页面上
因为我使用的是JQuery,所以可以用不同的方法来实现这一点,例如将占位符添加到页面的隐藏div中,并使用JQuery将值复制出来
有什么想法吗?你有没有尝试过展示你的控制权
StringBuilder sb = new StringBuilder();
using (StringWriter tw = new StringWriter(sb))
{
using (HtmlTextWriter hw = new HtmlTextWriter(tw))
{
errorPHolder.RenderControl(hw);
}
}
// its HTML string representation of errorPHolder
string html = sb.ToString();
这非常有效,谢谢!直到现在才知道HtmlTextWriter
)
StringBuilder sb = new StringBuilder();
using (StringWriter tw = new StringWriter(sb))
{
using (HtmlTextWriter hw = new HtmlTextWriter(tw))
{
errorPHolder.RenderControl(hw);
}
}
// its HTML string representation of errorPHolder
string html = sb.ToString();