Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
770 views
in Technique[技术] by (71.8m points)

c# - Accessing controls added programmatically on postback

On postback: How can I access ASP.NET controls in my code-behind file, which are added programmatically?

I am adding a CheckBox control to a Placeholder control:

PlaceHolder.Controls.Add(new CheckBox { ID = "findme" });

Controls added in the ASPX file are showing up fine in Request.Form.AllKeys except the ones I add programatically. What am I doing wrong?

Enabling the use of the ViewState on the controls does not help. If only it was that simple :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should recreate your dynamic control on postback:

protected override void OnInit(EventArgs e)
{

    string dynamicControlId = "MyControl";

    TextBox textBox = new TextBox {ID = dynamicControlId};
    placeHolder.Controls.Add(textBox);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...