C# 如何在静态方法中访问下拉列表
我在我的C# 如何在静态方法中访问下拉列表,c#,C#,我在我的home.aspx和home.aspx.cs页面中使用了DropDownList,我想用静态方法访问它。怎么可能呢?我不能用静态方法访问它。请帮帮我。不,这是不可能的 这是许多语言的基本规则。静态方法无法访问任何特定于实例的内容。ASP.NET上的DropDownList的一个实例就是。。实例变量。静态方法是跨所有实例的 为了得到你想要的。。您需要向其中传递一个实例。大概是这样的: public class ObjectA { public string Name { get;
home.aspx
和home.aspx.cs
页面中使用了DropDownList
,我想用静态方法访问它。怎么可能呢?我不能用静态方法访问它。请帮帮我。不,这是不可能的
这是许多语言的基本规则。静态方法无法访问任何特定于实例的内容。ASP.NET上的DropDownList
的一个实例就是。。实例变量。静态方法是跨所有实例的
为了得到你想要的。。您需要向其中传递一个实例。大概是这样的:
public class ObjectA {
public string Name { get; set; }
public static string GetName(ObjectA instance) {
return instance.Name;
}
}
public void Page_Load(object sender, EventArgs e) {
doSomethingWith(dropDownList1);
}
public static void doSomethingWith(DropDownList dropDown) {
// use the dropdown variable here
}
(是的,这是一个可怕的例子。)
因此,使用ASP.NET页面。。您可以这样做:
public class ObjectA {
public string Name { get; set; }
public static string GetName(ObjectA instance) {
return instance.Name;
}
}
public void Page_Load(object sender, EventArgs e) {
doSomethingWith(dropDownList1);
}
public static void doSomethingWith(DropDownList dropDown) {
// use the dropdown variable here
}
不,这是不可能的
这是许多语言的基本规则。静态方法无法访问任何特定于实例的内容。ASP.NET上的DropDownList
的一个实例就是。。实例变量。静态方法是跨所有实例的
为了得到你想要的。。您需要向其中传递一个实例。大概是这样的:
public class ObjectA {
public string Name { get; set; }
public static string GetName(ObjectA instance) {
return instance.Name;
}
}
public void Page_Load(object sender, EventArgs e) {
doSomethingWith(dropDownList1);
}
public static void doSomethingWith(DropDownList dropDown) {
// use the dropdown variable here
}
(是的,这是一个可怕的例子。)
因此,使用ASP.NET页面。。您可以这样做:
public class ObjectA {
public string Name { get; set; }
public static string GetName(ObjectA instance) {
return instance.Name;
}
}
public void Page_Load(object sender, EventArgs e) {
doSomethingWith(dropDownList1);
}
public static void doSomethingWith(DropDownList dropDown) {
// use the dropdown variable here
}
将
DropDownList
作为静态方法的参数传递,然后可以从静态方法调用此实例的方法。将DropDownList
作为静态方法的参数传递,然后可以从静态方法调用此实例的方法