我在我的home.aspx中使用了一个DropDownList,在home.aspx.cs页面中,我想在一个静态方法中访问它。这怎么可能呢?我不能在静态方法中访问它。请帮帮我..
发布于 2014-02-08 15:35:54
不不可能。
这是许多语言的基本规则。静态方法不能访问特定于实例的任何内容。ASP.NET上的DropDownList实例就是..实例变量。静态方法适用于所有实例。
为了得到你想要的..您需要向其中传递一个实例。如下所示:
public class ObjectA {
public string Name { get; set; }
public static string GetName(ObjectA instance) {
return instance.Name;
}
}(是的,这是一个可怕的例子。)
因此,使用ASP.NET页面..您可能会这样做:
public void Page_Load(object sender, EventArgs e) {
doSomethingWith(dropDownList1);
}
public static void doSomethingWith(DropDownList dropDown) {
// use the dropdown variable here
}发布于 2014-02-08 15:37:23
将DropDownList作为静态方法的参数传递,然后可以从静态方法调用此实例的方法。
https://stackoverflow.com/questions/21643344
复制相似问题