WEB用戶控件(.ascx)如何自定義事件在外部引用 http://www.csdn.net/develop/Read_Article.asp?Id=19232 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 测试了你的代码,也出现这个问题,想不通ing... ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconhandlingpageletevents.htm 謝dragonsuc(新的开始)指點,能否具體地告訴我應該怎麼做? 楼主,我试了一下你的程序,有几点给你说一下:1.你只是在test.aspx的CS文件中引用了zwErp1.usercontrol,并未在aspx文件中放置该控件,我照你的思路把你的原程序改动了一下,程序可以正常运行了,可看不见运行效果;2.如果你要在aspx文件中放置该控件,在test.aspx文件的开始应加入:<%@ Register TagPrefix="expo" TagName="UserControl" Src="EventTest.ascx" %>在form中加入<expo:UserControl runat="server" id="webUC"/>在CS文件中将private zwErp1.usercontrol.WebUserControl1 webUC; 改为:protected zwErp1.usercontrol.WebUserControl1 webUC; 并将 void OnInit(EventArgs e)中我加的webUC = new zwErp1.usercontrol.WebUserControl1();去掉;我试过程序运行结果正常,单击Button1后,Label1显示为"hello!"下面的程序是你的未放置该控件的原程序,经我改动后可以正常运行了,可看不见运行效果------------------- EventTest.ascx代碼如下: namespace zwErp1.usercontrol { using System; public delegate void MyEventHandler(string msg); public class WebUserControl1 : System.Web.UI.UserControl { protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Button Button1; public event MyEventHandler EH; private void Page_Load(object sender, System.EventArgs e) { // 將使用者程式碼置於此以初始化網頁 } #region Web Form Designer generated code private void onEvent(string msg) { if(EH != null)//只有EH非空时,才能调用事件处理函数 EH(msg); } private void Button1_Click(object sender, System.EventArgs e) { onEvent("hello!"); } } } ---------------------- test.aspx代碼如下: using System; using zwErp1.usercontrol; namespace zwErp1.myoldfile.webForms { public class EventText : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; //如在aspx文件中引用该控件了,应将下面的private换为protectedprivate zwErp1.usercontrol.WebUserControl1 webUC; private void Page_Load(object sender, System.EventArgs e) { } override protected void OnInit(EventArgs e){ InitializeComponent(); //由于你未在aspx文件中引用该控件,必须在此要new一个; //如在aspx文件中引用该控件了,请将这句删除 webUC = new zwErp1.usercontrol.WebUserControl1(); this.webUC.EH+=new zwErp1.usercontrol.MyEventHandler(this.click); base.OnInit(e);}private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); /// <summary>/// 设计器支持所需的方法 - 不要使用代码编辑器修改此方法的内容。/// </summary>//this.webUC.EH+=new zwErp1.usercontrol.MyEventHandler(this.click); } #endregion private void click(string msg) { Label1.Text=msg.ToString(); } } } ---------------------- 1:在CS文件中将private zwErp1.usercontrol.WebUserControl1 webUC; 改为:protected zwErp1.usercontrol.WebUserControl1 webUC; 2:保证这里的名称webUC跟你放置到aspx页面上的用户控件的id也是“webUC”改了这两个肯定就OK了 to chnking(kent) :不行,我原來是這麼做也不行,幫我看看原代碼到你那裡試一下。幫幫忙,搞定了立即加分! 我就是用的你的代码,全部按照你的做就出现你说的那个问题。第一,后来看aspx上面放置的所有控件的声明都是protected的,你要是把哪个控件改成private的,要在代码中访问这个控件必然得到:“异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。”这样的结果,我也是才注意到这个问题,也觉得奇怪为什么一定要protected才行,不过事实就是这样,所以一定要把你声明的用户控件为protected。第二个问题,就是要保证你在aspx上放置的用户控件有个ID,一定要和你在代码中声明的这个用户控件的名称一样。 to chnking(kent):老兄,我以前也改成protected,但也不行,你那边测试成功了吗?是什么原因? 当然成功了,你那改成了protected,但也不行,还有没有注意到我说的第二点?如果还是不行的话,麻烦你把你最新测试的代码全部贴出来,包括aspx的 或者打包发到我的email: [email protected] to chnking(kent):我已將ASPX和ASCX發送給你了,請幫我看看,拜托了! to chnking(kent):搞定了!是按你的第二點去做的,不過我搞不懂為什麼代码中声明的这个用户控件的名称和ID會有問題?有機會請你???。 我的爱好就是把所有的用户控件做成定制控件,如果你把那个垃圾ascx mail to me,我会mail还你一个dll 我保证,那个dll绝对不用dotfuscator混晰。 to LeighSword(Sword) :原碼就在上面,你拿做成一個DLL來看看。我的EMAIL是LIUHAIJIN@163.NET to LeighSword(Sword):對了老兄,忘了告訴你,我實現的功能遠遠不止上面這些,要有DATAGRID(排序、分頁、增加、刪除、修改等),還要有可觀的界面,搞定了我出1000分! 当你直接往aspx页面放置一个用户控件时,并不会自动在cs代码中声明这个用户控件,所以你如果要在cs代码中访问这个用户控件,一定要在cs代码中声明这个用户控件,并且声明的这个控件的名称一定要和放置到aspx页面的那个控件的ID一致,这样你声明的用户控件才是代表你放置在aspx页面上的那个用户控件了。 謝 chnking(kent) ,馬上散分,可惜分不是很多 留言板中楼层号的思路 100分在线等急求助 关于类作为参数传递的问题 asp。net上传文件的权限问题 上传后的图片大小为0字节 晕!!!!!!!初学asp.net到底准备哪些? 水晶报表10的问题:如何定义工具条的按钮命令? 急需《更改密码》原代码?? SOS(一个典型的错误) 水晶报表问题~~~~~~~~~~~~~ ASP.NET(C#)中用調試功能時提示“你沒有對服務器進行調試的權限......” 高分!htmlselect问题
1.你只是在test.aspx的CS文件中引用了zwErp1.usercontrol,并未在aspx文件中放置该控件,
我照你的思路把你的原程序改动了一下,程序可以正常运行了,可看不见运行效果;
2.如果你要在aspx文件中放置该控件,在test.aspx文件的开始应加入:
<%@ Register TagPrefix="expo" TagName="UserControl" Src="EventTest.ascx" %>
在form中加入
<expo:UserControl runat="server" id="webUC"/>
在CS文件中将
private zwErp1.usercontrol.WebUserControl1 webUC;
改为:
protected zwErp1.usercontrol.WebUserControl1 webUC;
并将 void OnInit(EventArgs e)中我加的
webUC = new zwErp1.usercontrol.WebUserControl1();
去掉;
我试过程序运行结果正常,单击Button1后,Label1显示为"hello!"下面的程序是你的未放置该控件的原程序,经我改动后可以正常运行了,可看不见运行效果
-------------------
EventTest.ascx代碼如下: namespace zwErp1.usercontrol
{
using System; public delegate void MyEventHandler(string msg);
public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1; public event MyEventHandler EH;
private void Page_Load(object sender, System.EventArgs e)
{
// 將使用者程式碼置於此以初始化網頁
} #region Web Form Designer generated code
private void onEvent(string msg)
{
if(EH != null)//只有EH非空时,才能调用事件处理函数
EH(msg);
} private void Button1_Click(object sender, System.EventArgs e)
{
onEvent("hello!");
} }
}
----------------------
test.aspx代碼如下: using System;
using zwErp1.usercontrol; namespace zwErp1.myoldfile.webForms
{
public class EventText : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
//如在aspx文件中引用该控件了,应将下面的private换为protected
private zwErp1.usercontrol.WebUserControl1 webUC;
private void Page_Load(object sender, System.EventArgs e)
{
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
//由于你未在aspx文件中引用该控件,必须在此要new一个;
//如在aspx文件中引用该控件了,请将这句删除
webUC = new zwErp1.usercontrol.WebUserControl1();
this.webUC.EH+=new zwErp1.usercontrol.MyEventHandler(this.click);
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改此方法的内容。
/// </summary>
//this.webUC.EH+=new zwErp1.usercontrol.MyEventHandler(this.click); }
#endregion
private void click(string msg)
{
Label1.Text=msg.ToString();
} }
}
----------------------
private zwErp1.usercontrol.WebUserControl1 webUC;
改为:
protected zwErp1.usercontrol.WebUserControl1 webUC; 2:保证这里的名称webUC跟你放置到aspx页面上的用户控件的id也是“webUC”改了这两个肯定就OK了
“异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。”
这样的结果,我也是才注意到这个问题,也觉得奇怪为什么一定要protected才行,不过事实就是这样,所以一定要把你声明的用户控件为protected。第二个问题,就是要保证你在aspx上放置的用户控件有个ID,一定要和你在代码中声明的这个用户控件的名称一样。