如何向 ASP.NET 表单动态添加控件?

发布于 2024-10-10 07:01:51 字数 76 浏览 0 评论 0原文

我不知道如何使用 C# .net 将控件动态添加到表单中。有人可以帮助我吗?我知道 vb.net 的这一点,但我需要知道 C# 中的语法。

I do not know how to add controls dynamically to the form using C# .net. Can anyone help me? I know this with vb.net but I need to know the syntax in C#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

顾北清歌寒 2024-10-17 07:01:51

在表单中,以下代码可以动态添加按钮:

Button button1 = new Button();
button1.Text = "dynamic button";
button1.Left = 10; button1.Top = 10;  //the button's location
this.Controls.Add(button1);

In the form, the following code can add a button dynamically:

Button button1 = new Button();
button1.Text = "dynamic button";
button1.Left = 10; button1.Top = 10;  //the button's location
this.Controls.Add(button1);
拥醉 2024-10-17 07:01:51

在 Aspx 中,

<%@ Reference Control = "WebUserControl1.ascx" %>

U 可以在 Cs 文件中使用以下内容动态地加载控件...

if (case)
else
{
WebUserControl1 uc = 
      (WebUserControl1) Page.LoadControl("WebUserControl1.ascx"); 
    PlaceHolder1.Controls.Add(uc); 


}

或者尝试这个

 Content.Controls.Add(Page.LoadControl("UserControls/InventoryNav.ascx"));

也可以看看:

http://aspalliance.com/565

http://samuelmueller.com/2008/12/dynamicloader-plugin-dynamically-loading-asp-net-user-controls-with-jquery

http://forums.asp.net/p/1222567/2826338.aspx

In Aspx

<%@ Reference Control = "WebUserControl1.ascx" %>

U can use the following in the Cs file to laod the control dynamically...

if (case)
else
{
WebUserControl1 uc = 
      (WebUserControl1) Page.LoadControl("WebUserControl1.ascx"); 
    PlaceHolder1.Controls.Add(uc); 


}

or try this

 Content.Controls.Add(Page.LoadControl("UserControls/InventoryNav.ascx"));

Can also have a look at:

http://aspalliance.com/565

http://samuelmueller.com/2008/12/dynamicloader-plugin-dynamically-loading-asp-net-user-controls-with-jquery

http://forums.asp.net/p/1222567/2826338.aspx

森末i 2024-10-17 07:01:51

下面是动态添加控件到 ASP.NET 表单的代码。

  1. 初始化标签
  2. 为其分配文本。
  3. 初始化面板
  4. 将标签对象添加到面板。
     Label lbl1 = new Label();
     lbl1.Text = "Your message here";
     Panel panel1= new Panel();
     panel1.Controls.Add(lbl1);

Below is the code to add controls dynamically to ASP.NET form.

  1. Initialize a label
  2. Assign text to it.
  3. Initialize a panel
  4. Add the label object to the panel.
     Label lbl1 = new Label();
     lbl1.Text = "Your message here";
     Panel panel1= new Panel();
     panel1.Controls.Add(lbl1);
祁梦 2024-10-17 07:01:51

请参阅下面的示例,

假设表单名称为 frmMain。

Button btnSave = New Button();
frmMain.Controls.Add(btnSave)

Please see the below sample

lets say forms name is frmMain.

Button btnSave = New Button();
frmMain.Controls.Add(btnSave)
浪推晚风 2024-10-17 07:01:51

下面是可以在某些事件(如页面加载或 onload)甚至某些用户操作(如 onclick)上调用的代码。

protected void add_button(Button btn)
{
   try
   {
        panel1.Controls.Add(btn); // Add the control to the container on a page
   }
   catch (Exception ee)
   {
         lblError.Text = ee.Message.ToString();
   }
}

Below is the code that can be called on some events like page load or onload or even some user action like onclick.

protected void add_button(Button btn)
{
   try
   {
        panel1.Controls.Add(btn); // Add the control to the container on a page
   }
   catch (Exception ee)
   {
         lblError.Text = ee.Message.ToString();
   }
}
鱼忆七猫命九 2024-10-17 07:01:51

将控件添加到面板通常是可以接受的,无论面板是通过标记还是以编程方式添加到页面的。

有关 C# 语法,请参阅以下链接

It's generally acceptable to add the controls to a panel, be it that the panel has been added to the page in the markup or programmatically.

See the following link for the C# syntax

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文