ASP.net C#获取访问器 [英] ASP.net C# Get Accessor
问题描述
我在ASP.Net C#中是个新手.我正在尝试从单独的类访问Web窗体中的控件.我尝试使用Get Accessor,但它给我一个错误
Hi,
I am quite new in ASP.Net c#. I am trying to access a control in a webform from a separate class. I tried using the Get Accessor, but it gives me an error
Object reference not set to an instance of an object.
这是来自Webform的代码
Here the code from the webform
public string dlText
{
get
{
return dlFrYr.Text;
}
}
这是来自该类的代码
And here is the code from the class
public static bool HasErrors()
{
String frmYR;
int xYr;
_Default xDash = new _Default();
//Dashboard xDash = new Dashboard();
bool xErr = false;
//check if there are PSC or industry selected
if (SubIndID.Count == 0 || PSCid.Count == 0)
xErr = true;
frmYR = xDash.dlText;
}
任何帮助将不胜感激.
谢谢,
Any help is greatly appreciated.
Thanks,
推荐答案
return dlFrYr.Text;
使用F5调试您的应用程序.我的猜测是,如上所示,您的return语句中的dlFrYr变量为null.要访问尚未分配给变量的表单上的控件,您将必须使用 Page.FindControl方法 [ ^ ]这样做. (即var dlFrYr = (TextBox)Page.FindControl("dlFrYr");
)
Use F5 to debug your application. My guess is that the dlFrYr variable in your return statement as shown above is null. To access a control on a form that is not assigned to a variable yet, you''ll have to use the Page.FindControl Method[^] to do so. (ie. var dlFrYr = (TextBox)Page.FindControl("dlFrYr");
)
这篇关于ASP.net C#获取访问器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!