使用占位符在asp.net中动态绑定文本框和字符串

时间:2013-07-22 09:09:34

标签: asp.net

我有以下代码,我使用占位符来保存动态创建的文本框。重写Pre Init和LoadViewState以方便更改,方法NextId将id加1并渲染回来。 Create方法将创建文本框的新实例并将其添加到List。

我的问题是我可以更换"#"在字符串str by textbox中,字符串的其余部分应按原样附加,并且在最终UI用户应该看到文本框而不是"#"和其他字符串值。对不起,如果问题似乎无关紧要,我是新手......任何帮助都将深表感谢,谢谢

public partial class _Default : System.Web.UI.Page
{

public void create()
{
        TextBox txt = new TextBox();
        txt.ID = "TxtFillUp" + NextId.ToString();
        DynamicControl.Controls.Add(txt);
        //DynamicControl.Controls.Add(new LiteralControl("<br>"));
        ControlList.Add(txt.ID);
}

public List<string> ControlList
{
    get
    {
        if (ViewState["Controls"] == null)
        {
            ViewState["Controls"] = new List<string>();
        }
        return (List<string>)ViewState["Controls"];
    }
}


public string str = "# is the capital of #";

StringBuilder sb = new StringBuilder();

public int NextId
{
    get
    {
        return ControlList.Count + 1;
    }
}

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
}

protected override void LoadViewState(object savedState)
{
    base.LoadViewState(savedState);
    foreach (string s in ControlList)
    {
        TextBox txt = new TextBox();
        txt.ID = s;
        DynamicControl.Controls.Add(txt);
        //DynamicControl.Controls.Add(new LiteralControl("<br>"));
    }
}


protected void Page_Load(object sender, EventArgs e)
{
    foreach (char s in str)
    {
        if(s == '#')
        {
           create();//replace "#" with textbox in string str.
        }
        else
        {
            //If chaacterr is not "#"  it should be appended as it is.
        }
    }
    DynamicControl.Controls.Add(//adding the modified string that will be displayed in UI);
}

0 个答案:

没有答案