在代码里有个地方就是
到处都是
protected .....
protected .....
protected .....
protected .....
在这里加
protected 你的子定义控件类型 UserControl1;
到处都是
protected .....
protected .....
protected .....
protected .....
在这里加
protected 你的子定义控件类型 UserControl1;
解决方案 »
- 根据库里面取出来的记录 动态生成select 而不是dropdownlist
- MVC框架下,如何调用alert报告后台处理状况?
- 网页资料防下载代码
- 大家看看,验证码系统,ASP.NET (VB),为什么在有的机子上可以看到,有的机子上看不到图片哦,立刻给分!!!我的信用,大家看着办
- .NET初学者问题!Label控件绑定数据方法!在线等~
- ASP.NET如何配置授权用户和角色
- 请问大家的网站,一般用什么.NET的论坛
- 向高手请教:HttpApplication象Session一样有生命周期吗?谢谢!!!!
- 关于表单的显示问题,不知道是用DATALIST嵌套好还是用别的方式好,希望大家给点好的建议
- 急,怎么控制添加一个站点,但是让IIS里面不显示出来
- 分页问题,在线求助!
- 如何用datagrid控件来实现分页
uc2.ID = "UserControl2";
this.Panel1.Controls.Add(uc2); 改为: Control uc2=Page.LoadControl("UserControl.ascx");
this.Panel1.Controls.Add(uc2);
<%@ Register TagPrefix="uc1" TagName="UserControl" Src="UserControl.ascx" %>
<uc1:UserControl id="UserControl1" runat="server"></uc1:UserControl>
这样的调用方法都是正常的啊。就是用:
UserControl uc2 = new UserControl();
uc2.ID = "UserControl2";
this.Panel1.Controls.Add(uc2);
调用的时候出错。出错行代码是用户控件中的按钮事件定义这一行:
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);我想是不是还需要对控件做个啥设置才可以正常工作吧。你说的 protected 你的子定义控件类型 UserControl1; 是加到调用页面吧?
还是不行。你可以自己试试看,做个用户控件,然后在上面放个按钮,在给这个按钮加个事件。
然后用 new 的方法将控件加到页面中。
你的方法我知道,我目前用的就是这样的处理方式。
我想知道的是为啥用 new 的那种方法会出错了?
void Page_Load(Object sender,EventArgs e)
{
//动态添加控件
Page.Controls.Add(new HtmlGenericControl("hr"));
Control c1=LoadControl("SampleControl.ascx");
((UserControl)c1).Category="business"; //设置查询语句中的类别
Page.Controls.Add()c1;
...//同上添加控件 设置其他类别
}-------努力学习 不断实践 虚心讨教--------
你们的方法我知道。
我想知道为什么用那种 new 的方法会出错了?
加上断点,你会发现this.btnSubmit为null;你的UserControl中的btnSubmit一定是在usercontrol.ascx中直接声明的,
比如:<asp:Button id="btnSubmit" runat="server"></asp:Button>
对应usercontrol.ascx.cs文件中protected Button btnSubmit;
像你所说:
使用一下方法加入控件到页面,工作正常。
<%@ Register TagPrefix="uc1" TagName="UserControl" Src="UserControl.ascx" %>
<uc1:UserControl id="UserControl1" runat="server"></uc1:UserControl>是因为Http runtime 加载你的UserControl时会解析UserControl.ascx文件并实例化你的btnSubmit,而这个过程是在UserControl构造函数执行以后,UserControl的Page_Load执行之前,相当于它会添加类似下面的一句代码:this.btnSubmit = __ctrl(asp.net runtime 生成的Button实例.这就是你在UserControl.ascx.cs中只有protected Button btnSubmit;的声明,
却能在Page_Load中使用btnSubmit,并且btnSubmit不为null的原因.所也不管是上面的方法,
还是 webdiyer(陕北吴旗娃) 的Control uc2=Page.LoadControl("UserControl.ascx");的方法asp.net都会为你实例化btnSubmit,可以正常工作.所以只要你的UserControl在构造函数中实例化所有子控件(比如this.btnSubmit = new Button();那你就能:
UserControl uc2 = new UserControl();
uc2.ID = "UserControl2";
this.Panel1.Controls.Add(uc2);并且正常工作.这样就像server Control一样了.建议你看看:
http://www.csdn.net/Develop/Read_Article.asp?Id=17687
http://www.csdn.net/Develop/Read_Article.asp?Id=17689
在使用了你的建议后,的确解决了错误提示。
但是出现了一个新问题:自定义控件并没有在页面中显示出来。
我查看了相关属性:
加入控件前,父控件中的子控件数:0
加入控件后,父控件中的子空件数:1 (证明用户控件已经加入)
用户控件的 EnableViewState 属性:True
用户控件的 Visible 属性:True我的用户控件中出了前面提到的一些 asp.net 控件外,也有直接输入的文本信息和表格html代码。这些信息都没有显示。
查看页面代码 Panel2 生成的html代码段为:
<div id="Panel2"></div>
可见中间什么都没有?
请教~~~~~~~^_^。
这样你直接New就不行了
UserControl uc2 = new UserControl();
uc2.ID = "UserControl2";
this.Panel1.Controls.Add(uc2); 你得到的uc2对象,根本不包含“直接输入的文本信息和表格html”,因为他们在usercontrol.ascx文件中, 你如果Control uc2=Page.LoadControl("UserControl.ascx");
这样你再quickwatch uc2,你会发现有你的文本信息、html。如果你想直接New,还要有“直接输入的文本信息和表格html”,那你把他们作为LiteralControl,加入你的UserControl,也可以!
如果你把“直接输入的文本信息和表格html”,作为LiteralControl,加入你的UserControl,那你的.ascx文件也不用了,你的UserControl,和一个server control 没什么区别!