当前位置:编程学习 > C#/ASP.NET >>

asp.net 控件的问题

using System; using System.IO; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Chapter102_Default4 : System.Web.UI.Page { DropDownList ddl1 = Page.FindControl("DropDownList1") as DropDownList; protected void Page_Load(object sender, EventArgs e) { DropDownList ddl1 = Page.FindControl("DropDownList1") as DropDownList; } } 同样一句话,为什么上一句就报错? 错误是“非静态的字段、方法或属性要求对象引用” 求解,谢谢。
答案:首先Page.FindControl("DropDownList1") as DropDownList;相当于
this.Page.FindControl("DropDownList1") as DropDownList;

因为上面那句不在方法里面,相当于初始化成员,此时无法使用this(严格第说来,初始化成员比构造函数还早)
其他:你一个在类里面引用,一个是在方法体里面引用,在类字段中Page对象不能你那样访问,放在方法中,才有它的属性和方法体现的 了解一下.NET的编译原理就知道了。 非静态的字段、方法或属性要求对象引用?这话的意思是非静态的字段、方法或属性,要求该类必须实例化。

你直接赋值 不是矛盾了?
你手动能打就知道打不出 

上一个:请好心人回答俺的asp.net事件问题??
下一个:asp.net中用控件IMAGE插入的图片无法显示

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,