【问题标题】:how to add buttons in content place holder at runtime in asp.net?如何在运行时在 asp.net 中的内容占位符中添加按钮?
【发布时间】:2012-09-13 16:11:04
【问题描述】:

在我的表单中,我添加了一个内容占位符,并在内容占位符中手动添加了一个按钮。但是现在我想在运行时在同一个内容占位符中添加一个按钮。我尝试了以下代码,但该按钮未显示在浏览器中... 我不知道这里有什么问题......

我的代码...

ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("maincontent");
Button newButton = new System.Web.UI.WebControls.Button();
newButton.ID = "NextButton";
newButton.Text = "Next";
newButton.Visible = true;
content.Controls.Add(newButton);

【问题讨论】:

  • 您在使用 Visual Studio 吗?把手表放在content 上看看: a) 你真的找到了它; b) 它包含 newButton。

标签: c# asp.net asp.net-controls


【解决方案1】:

您丢失了按钮的大小和位置。

【讨论】:

  • 谢谢,这里我通过设置高度和宽度来添加大小。为了给出位置,我们将使用样式。我需要的位置是 style="margin-left: 148px"。我如何分配这个样式我的按钮...
【解决方案2】:

放下

ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("maincontent");

将您的代码更改为

Button newButton = new System.Web.UI.WebControls.Button();
newButton.ID = "NextButton";
newButton.Text = "Next";
newButton.Visible = true;
maincontent.Controls.Add(newButton);

假设“maincontent”是您为占位符或课程指定的 ID。

【讨论】:

    猜你喜欢
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多