主頁 > 知識庫 > ASP.NET動態(tài)添加用戶控件的方法

ASP.NET動態(tài)添加用戶控件的方法

熱門標簽:百度競價排名 科大訊飛語音識別系統(tǒng) 地方門戶網站 網站排名優(yōu)化 服務器配置 集中運營管理辦法 阿里云 硅谷的囚徒呼叫中心

本文實例講述了ASP.NET動態(tài)添加用戶控件的方法。分享給大家供大家參考。具體實現方法如下:

為了讓用戶控件能ASP.NET頁面實現動態(tài)添加,首先寫一個接口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();
}
}

有了接口之后,需要創(chuàng)建用戶控件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動態(tài)載入用戶控件的方法
  • ASP.NET動態(tài)加載用戶控件的實現方法
  • asp.net動態(tài)加載用戶控件,關于后臺添加、修改的思考
  • asp.net 用戶控件中圖片及樣式問題
  • asp.net 用戶控件讀取以及賦值
  • asp.net 動態(tài)添加多個用戶控件
  • asp.net頁面master頁面與ascx用戶控件傳值的問題
  • ASP.NET 用戶控件的使用介紹
  • ASP.NET 頁面中加添加用戶控件的寫法
  • Asp.Net其他頁面如何調用Web用戶控件寫的分頁
  • ASP.NET用戶控件如何使用

標簽:西雙版納 隨州 威海 烏蘭察布 開封 甘孜 廣西 梧州

巨人網絡通訊聲明:本文標題《ASP.NET動態(tài)添加用戶控件的方法》,本文關鍵詞  ;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266