如何将字符串可以把用户控件 [英] How to a string can turn a UserControl
问题描述
时有什么方法/可能性,我可以用一个用户控件替换字符串?
Is there any ways/possibilities that I can replace a string with a UserControl?
除了LoadControl并将其添加到像页或占位另一个控制
apart from LoadControl and adding it to another control like page or placeholder.
原因是用户在页面上添加内容,并把像一个字符串{UC:测试},我想解析这个文本和替换。{UC:测试}与用户控件
The reason is that a user add a content on the page and put a string like {uc:test} and I would like to parse this text and replace {uc:test} with a UserControl.
换言之;
用户输入将是唧唧歪歪{UC:测试}喇嘛喇嘛,当我找回它从数据库我怎么可以注入用户控件并将其替换为{UC:测试}
in other words ; The user input will be "bla bla bla {uc:test} bla bla", when I retrieve it from database how can I inject usercontrol and replace it with {uc:test}
感谢。
推荐答案
尝试LoadControl方法动态加载的用户控件和回发期间将它们添加到您的网页。
Try the LoadControl method to dynamically load user controls and add them to your page during postback.
Control someControl = LoadControl("~/SomeControl.ascx");
您可以...将它添加到你的页面的控件集合:
You could... Add it to your page's control collection:
this.Page.Controls.Add(someControl);
或者......将它添加到另一个控件的控件集合
Or... Add it to another control's control collection
someExistingPlaceHolder.Controls.Add(someControl);
如何获取标签这样使用正则表达式
How about getting the tags like this using regex
List<string> fields = new List<string>();
foreach(Match match in Regex.Matches(str, @"\{([^\}]*)\}")) {
fields.Add(match.Groups[1].Value);
}
这篇关于如何将字符串可以把用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!