这是我的代码
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
}
单击按钮
时,我无法看到我的标签文字任何帮助表示
由于
答案 0 :(得分:1)
您必须在页面上存在的任何控件内添加您的面板。
答案 1 :(得分:1)
如果您没有其他地方可以将其添加到网页或顶级表单元素中,则必须将Panel添加到某个控件中。
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
this.Form.Controls.Add(panel1); // YOU ARE MISSING THIS
}
答案 2 :(得分:1)
您需要将Panel
添加到页面中:
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
//Do this
SomeControlOnYourPage.Controls.Add(panel1);
}