The example in this article describes the method of ASP.net dynamically adding user controls. Share to everyone for your reference. The implementation methods are as follows:
In order for the user control to dynamically add ASP.net page, first write an interface igetucable, this interface has a function, the return object type is UserControl.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
<summary>
///Summary description for igetucable
///</summary>
namespace insus.net
{ Public
Interface Igetucable
{
UserControl GETUC ();
}
}
After you have an interface, you need to create a user control Calculator.ascx:
<%@ control language= "C #" autoeventwireup= "true" codefile= "Calculator.ascx.cs" inherits= "Calculator"%>
Number A: <asp:textbox id= "TextBox1" runat= "server" ></asp:TextBox> <br/>
+
<br/> Number B: <asp:textbox id= "TextBox2" runat= "server" ></asp:textbox><br/> "<asp:button id=
" Buttonequal "runat=" server "text=" = "onclick= buttonequal_click1"/> <br result
:/>
: Label id= "Labelresult" runat= "Server" text= "" ></asp:Label>
Calculator.ascx.cs,cs Implementation Interface:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using Insus.net;
public partial class calculator:system.web.ui.usercontrol,igetucable
{
protected void Page_Load (object sender, EventArgs e)
{
}
protected void Buttonequal_click1 (object sender, EventArgs e)
{
Decimal a = decimal. Parse (this. TextBox1.Text.Trim ());
Decimal b = Decimal. Parse (this. TextBox2.Text.Trim ());
This. Labelresult.text = (A + b). ToString ();
}
Public UserControl GETUC ()
{return this
;
}
}
Finally, the Page_Load event is written in the ASPX that needs to load the user control:
protected void Page_Load (object sender, EventArgs e)
{
igetucable uc1 = (igetucable) LoadControl ("~/ Calculator.ascx ");
THIS.FORM1.CONTROLS.ADD (UC1. GETUC ());
}
I hope this article will help you with the ASP.net program design.