最近在小舖裡看到了這方面的問題....
小弟找了很多的資料..做一個範例介紹如何動態載入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
|
| 1 |
using System; |
| 2 |
using System.Data; |
| 3 |
using System.Configuration; |
| 4 |
using System.Collections; |
| 5 |
using System.Web; |
| 6 |
using System.Web.Security; |
| 7 |
using System.Web.UI; |
| 8 |
using System.Web.UI.WebControls; |
| 9 |
using System.Web.UI.WebControls.WebParts; |
| 10 |
using System.Web.UI.HtmlControls; |
| 11 |
|
| 12 |
public 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 |
|
| 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
|
| 1 |
using System; |
| 2 |
using System.Data; |
| 3 |
using System.Configuration; |
| 4 |
using System.Collections; |
| 5 |
using System.Web; |
| 6 |
using System.Web.Security; |
| 7 |
using System.Web.UI; |
| 8 |
using System.Web.UI.WebControls; |
| 9 |
using System.Web.UI.WebControls.WebParts; |
| 10 |
using System.Web.UI.HtmlControls; |
| 11 |
|
| 12 |
public partial class UserControl : System.Web.UI.Page |
| 13 |
{ |
| 14 |
protected void Page_Init(object sender, EventArgs e) |
| 15 |
{ |
| 16 |
|
| 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 |
|
| 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 |
|
| 34 |
|
| 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