下划线替换成控件

 

最近做系统时需要将一篇文章中的空格"_______"替换成TextBox,.NET中有TemplateControl.ParseControl 方法 将输入字符串分析为 Web 窗体页或用户控件上的 对象。

使用方法:

假设页面有一个Panel,ID为Panel1:

Control c = ParseControl("<asp:button text='Click here!' runat='server' />");         Panel1.Controls.Add(c);

将指定的字符替换为控件:

    string s = "武汉市在_______省?";

     Label1.Text = s;

        s = s.Replace("_______", "<asp:TextBox text='' runat='server' ID='tb1'></asp:TextBox> ");

        Control c = ParseControl(s);

        Panel1.Controls.Add(c);

运行结果:

 

 

获取TextBox的值:

((TextBox)Panel.FindControl("tb1")).Text;

 

百度文库转

原文地址:https://www.cnblogs.com/liziqiang/p/3430973.html