2011-01-06 67 views

回答

1

將控件添加到面板通常是可以接受的,不管面板是以標記還是以編程方式添加到頁面中。

請參閱以下link爲C#語法

6

在形式,下面的代碼可以動態地添加一個按鈕:

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

這就是我的做法。以防萬一,您還可以將按鈕添加到其他控件。 – Marcel 2013-10-17 08:02:48

2

下面是一個可以像頁面加載某些事件被稱爲碼或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(); 
    } 
} 
+0

請修復您的代碼標記 – 2011-01-06 07:11:00

+3

@NishikaDas請在您的帖子末尾停止發送該鏈接的垃圾郵件。 – marcog 2011-01-07 00:40:34

2

請參見下面的示例

可以說形式的名稱爲frmMain。

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

在.aspx的

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

U可以使用在Cs文件下面以LAOD動態控制......

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

2

下面是添加控件動態到ASP.NET形式的代碼。

  1. 初始化標籤
  2. 分配文本。
  3. 初始化面板
  4. 將標籤對象添加到面板。

    Label lbl1 = new Label();
    lbl1.Text =「您的留言在這裏」;
    Panel panel1 = new Panel();
    panel1.Controls.Add(lbl1);

相關問題