本文实例讲述了asp.net动态添加用户控件的方法。分享给大家供大家参考。具体实现方法如下:
为了让用户控件能ASP.NET页面实现动态添加,首先写一个接口IGetUCable,这个接口有一个函数,返回对象类型是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();
}
}
有了接口之后,需要创建用户控件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: <asp:Label ID="LabelResult" runat="server" Text=""></asp:Label>
Calculator.ascx.cs,cs实现接口:
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;
}
}
最后是在需要加载用户控件的aspx的Page_load事件写:
protected void Page_Load(object sender, EventArgs e)
{
IGetUCable uc1 = (IGetUCable)LoadControl("~/Calculator.ascx");
this.form1.Controls.Add(uc1.GetUC());
}
希望本文所述对大家的asp.net程序设计有所帮助。
您可能感兴趣的文章:
- ASP.NET用户控件技术
- asp.net动态载入用户控件的方法
- ASP.NET动态加载用户控件的实现方法
- asp.net动态加载用户控件,关于后台添加、修改的思考
- asp.net 用户控件中图片及样式问题
- asp.net 用户控件读取以及赋值
- asp.net 动态添加多个用户控件
- asp.net页面master页面与ascx用户控件传值的问题
- ASP.NET 用户控件的使用介绍
- ASP.NET 页面中加添加用户控件的写法
- Asp.Net其他页面如何调用Web用户控件写的分页
- ASP.NET用户控件如何使用
声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)