在aspx網頁裡動態載入UserControl,並且透過UserControl Event的訂閱,來取得對應的值
来源:互联网 发布:linux如何看用户权限 编辑:程序博客网 时间:2024/06/07 03:07
最近在小舖裡看到了這方面的問題....
小弟找了很多的資料..做一個範例介紹如何動態載入User Control
與如何透過aspx網頁訂閱User Control的Event,來取得相關的資訊
此範例先做一個檔案上傳的User Control,當網頁需要用到上傳功能時..
就可以將此控項制拉進來,,或用動態載入的方式來載入此控制項
所以此範例將會出現兩個User Control(一個是用拉的,一個是動態載入的)
當使用者選取好檔案,按upload時,aspx網頁就可以取得user control傳過來的檔案資訊
這樣一來以後就不用重覆寫上傳檔案的程式了,,只要拉一個user control就可以快速讓網頁有上傳的功能了
c#範例
User Control
WebUserControl.ascx
1<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %> 2<asp:FileUpload ID="FileUpload1" runat="server" /> 3<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="upload" />
view plain | print | ?
WebUserControl.ascx.cs
1using System; 2using System.Data; 3using System.Configuration; 4using System.Collections; 5using System.Web; 6using System.Web.Security; 7using System.Web.UI; 8using System.Web.UI.WebControls; 9using System.Web.UI.WebControls.WebParts; 10using System.Web.UI.HtmlControls; 11 12public partial class WebUserControl : System.Web.UI.UserControl 13{ 14 //客制一個事件參數 15 public class MyEventArgs : EventArgs 16 { 17 string _FileName = string.Empty; 18 int _FileSize; 19 20 public string FileName 21 { 22 set { _FileName = value; } 23 get { return _FileName; } 24 } 25 26 public int FileSize 27 { 28 set { _FileSize =value; } 29 get { return _FileSize; } 30 } 31 32 } 33 34 public delegate void MyClick(object sender, MyEventArgs e); 35 36 public event MyClick cClick; 37 38 private void WebUserControl_Click(object sender, MyEventArgs e) 39 { 40 if (cClick != null) 41 { 42 cClick(sender, e); 43 } 44 } 45 46 protected void Page_Load(object sender, EventArgs e) 47 { 48 49 } 50 protected void btnUpload_Click(object sender, EventArgs e) 51 { 52 MyEventArgs EventArgs = new MyEventArgs(); 53 54 EventArgs.FileName = this.FileUpload1.FileName; 55 EventArgs.FileSize = this.FileUpload1.PostedFile.ContentLength; 56 //上傳到upload資料夾裡 57 this.FileUpload1.SaveAs(Server.MapPath("~/upload/") + this.FileUpload1.FileName); 58 59 WebUserControl_Click(this, EventArgs); 60 } 61 62}
view plain | print | ?
aspx網頁
UserControl.aspx
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserControl.aspx.cs" Inherits="UserControl" %> 2 3<%@ Register src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %> 4 5<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 7<html xmlns="http://www.w3.org/1999/xhtml" > 8<head runat="server"> 9 <title>UserControl</title> 10</head> 11<body> 12 <form id="form1" runat="server"> 13 <div> 14 <uc1:WebUserControl ID="WebUserControl1" OncClick="WebUserControl1_cClick" runat="server" /> 15 </div> 16 </form> 17</body> 18</html>
view plain | print | ?
UserControl.aspx.cs
1using System; 2using System.Data; 3using System.Configuration; 4using System.Collections; 5using System.Web; 6using System.Web.Security; 7using System.Web.UI; 8using System.Web.UI.WebControls; 9using System.Web.UI.WebControls.WebParts; 10using System.Web.UI.HtmlControls; 11 12public partial class UserControl : System.Web.UI.Page 13{ 14 protected void Page_Init(object sender, EventArgs e) 15 { 16 //動態加入UserControl 17 WebUserControl c = (WebUserControl)LoadControl("WebUserControl.ascx"); 18 c.cClick += new WebUserControl.MyClick(c_cClick); 19 this.Page.Form.Controls.Add(c); 20 } 21 22 //動態加入UserControl的事件 23 void c_cClick(object sender, WebUserControl.MyEventArgs e) 24 { 25 Response.Write(e.FileName + "<br/>" + e.FileSize); 26 } 27 28 protected void Page_Load(object sender, EventArgs e) 29 { 30 31 } 32 33 //事先拉好的UserControl事件 34 //在UserControl.aspx裡有一個OncClick="WebUserControl1_cClick",就是對應到此事件 35 protected void WebUserControl1_cClick(object sender, WebUserControl.MyEventArgs e) 36 { 37 Response.Write(e.FileName + "<br/>" + e.FileSize); 38 } 39}
view plain | print | ?
執行結果:
參考網址:
http://www.thescripts.com/forum/thread232638.html
http://blog.blueshop.com.tw/jeff377/archive/2008/01/19/54090.aspx
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20080224205358PAU&fumcde=FUM20041006161839LRJ&rplcnt=4
http://blog.xuite.net/cct0201/derek/15360068
- 在aspx網頁裡動態載入UserControl,並且透過UserControl Event的訂閱,來取得對應的值
- aspx页面与UserControl之间的通讯
- usercontrol!
- Handle UserControl raised event
- DataGrid的自定义分页UserControl
- DataGrid的自定义分页UserControl
- UserControl 方式 的小型 GridViewExtender
- 在UserControl中调用所在页面的ViewState
- 如何在VB6中使用.NET开发的UserControl
- vb.net 如何在同一页面,添加相同的usercontrol
- VS2005的UserControl的使用问题
- 今天练习UserControl遇到的问题
- 一个文件上传与下载的UserControl
- 关于将UserControl应用于Sharepoint的步骤
- 获取GridView/UserControl生成的Html代码
- 利用delegate调用userControl的方法
- 向UserControl传递参数的方法
- SL3中Page、UserControl、ChildWindow的区别
- [Microsoft][ODBC SQL Server Driver][SQL Server]在关键字 'user' 附近有语法错误。
- work with solaris contract subsystem
- 描述性统计与性能结果分析-LoadRunner
- SQL Server 2005的几个新功能
- 和同事相处的30个原则
- 在aspx網頁裡動態載入UserControl,並且透過UserControl Event的訂閱,來取得對應的值
- Siebel Admin: Enabling Find on 8.0.0.2
- Objective-C 基础教程(Amazon超级畅销书,好评如潮)
- Objective-C 基础教程(Amazon超级畅销书,好评如潮)
- Linux下链接文件使用rm无法删除的解决办法
- java各数据类型之间的转换
- WKT (Well Known Text)
- jdbc连接数据库
- [webservices开发]使用XFire开发一个简单的web服务