Asp. NET Dynamic Add User Control method _ Practical Tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.