开发者

On postback htmlgeneric control remove its childeren controls in asp.net, why?

I have an htmlgeneric 开发者_如何学Ccontrol and on run time i am adding control in it but when i click on any button then added controls disappear.


Dynamically created controls need to be created on every post back. You also need to give them an ID if you want to maintain and restore their ViewState.

For example, this will show the TextBox the first time the page is loaded, but on any subsiquent page loads, the control will be missing:

protected void Page_Init(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        TextBox newControl = new TextBox()
        {
            ID = "newControl"
        };
        SomeControl.Controls.Add(newControl);
    }
}

However, if you create the control on every postback with the same Id, then the control will be maintained with it's Text:

protected void Page_Init(object sender, EventArgs e)
{
    TextBox newControl = new TextBox()
    {
        ID = "newControl"
    };
    SomeControl.Controls.Add(newControl);
}

Here's a good article about dealing with dynamic controls.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜