使用 C# 在静态方法中获取标签的文本?
Get the text of label in static method using C#?
我有一个静态函数,我想在其中获取 LABEL1 的值
[WebMethod]
public static string SetFileNameU(List<string> someValues)
{
string journey = Convert.ToString(someValues[0]);
Label tbx = _________("Label1", true).FirstOrDefault() as Label;
return "ss";
}
现在我应该在那里做什么?请告诉我
您无法访问 label
文本,因为当您调用静态方法时,它没有视图实例。这就是为什么您既不能直接访问它们,也不能使用 Controls
Collection.
您必须使用另一种方法来 return 您想要的值。也许将所需的值存储在另一个静态变量中。
例如:
public static class MyClass {
public static string AndHisNameIs;
}
public void SomewhereInTheCode() {
....
MyClass.AndHisNameIs = "JOHN CEENA";
....
}
[WebMethod]
public static string SetFileNameU(List<string> someValues)
{
string journey = Convert.ToString(someValues[0]);
return MyClass.AndHisNameIs;
}
我有一个静态函数,我想在其中获取 LABEL1 的值
[WebMethod]
public static string SetFileNameU(List<string> someValues)
{
string journey = Convert.ToString(someValues[0]);
Label tbx = _________("Label1", true).FirstOrDefault() as Label;
return "ss";
}
现在我应该在那里做什么?请告诉我
您无法访问 label
文本,因为当您调用静态方法时,它没有视图实例。这就是为什么您既不能直接访问它们,也不能使用 Controls
Collection.
您必须使用另一种方法来 return 您想要的值。也许将所需的值存储在另一个静态变量中。
例如:
public static class MyClass {
public static string AndHisNameIs;
}
public void SomewhereInTheCode() {
....
MyClass.AndHisNameIs = "JOHN CEENA";
....
}
[WebMethod]
public static string SetFileNameU(List<string> someValues)
{
string journey = Convert.ToString(someValues[0]);
return MyClass.AndHisNameIs;
}