我有一个主页,其中有 4 个名为 c_LOB、c_Projectname、c_countryname、c_releasename 的组合框。
现在我正在尝试根据这些组合框的启用属性更改工具提示。以下是使用 C# 和 asp.net 在 Visual Studio 2010 中编写的代码:
namespace GPTRGT
{
public partial class Homepage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserIdentification();
}
public void UserIdentification()
{
c_LOB.Items.Clear();
c_LOB.Items.Add("RUSA");
c_LOB.Items.Add("RETAIL");
c_LOB.Items.Add("CARDS");
if (c_CountryName.Enabled == false)
{
c_CountryName.ToolTip = "Please select your LOB first.";
}
else
{
c_CountryName.ToolTip = "Please select your Country name.";
}
if (c_ProjName.Enabled == false && c_CountryName.Enabled == false)
{
c_ProjName.ToolTip = "Please select you LOB and Country name first.";
}
else if (c_ProjName.Enabled == false)
{
c_ProjName.ToolTip = "Please select your Country name first.";
}
else
{
c_ProjName.ToolTip = "Please select your Project name.";
}
if (c_ProjName.Enabled == false && c_CountryName.Enabled == false && c_ProjName.Enabled == false)
{
c_ReleaseName.ToolTip = "Please select your LOB, Country name & Project name first";
}
else if (c_CountryName.Enabled == false && c_ProjName.Enabled == false)
{
c_ReleaseName.ToolTip = "Please select your Country name & Project name first";
}
else if (c_ProjName.Enabled == false)
{
c_ReleaseName.ToolTip = "Please select your Project name first";
}
else
{
c_ProjName.ToolTip = "Please select your Release name.";
}
}
}
}
此代码工作正常。没有问题。现在我想做的是,我创建了一个名为验证的单独类,并在其中创建了这个 useridentification() 方法。然后我试图从主页类调用这个方法。看看代码。
namespace GPTRGT
{
public partial class Homepage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
validation val = new validation();
val.UserIdentification();
}
}
class validation : Homepage
{
public void UserIdentification()
{
c_LOB.Items.Clear();
c_LOB.Items.Add("RUSA");
c_LOB.Items.Add("RETAIL");
c_LOB.Items.Add("CARDS");
if (c_CountryName.Enabled == false)
{
c_CountryName.ToolTip = "Please select your LOB first.";
}
else
{
c_CountryName.ToolTip = "Please select your Country name.";
}
if (c_ProjName.Enabled == false && c_CountryName.Enabled == false)
{
c_ProjName.ToolTip = "Please select you LOB and Country name first.";
}
else if (c_ProjName.Enabled == false)
{
c_ProjName.ToolTip = "Please select your Country name first.";
}
else
{
c_ProjName.ToolTip = "Please select your Project name.";
}
if (c_ProjName.Enabled == false && c_CountryName.Enabled == false && c_ProjName.Enabled == false)
{
c_ReleaseName.ToolTip = "Please select your LOB, Country name & Project name first";
}
else if (c_CountryName.Enabled == false && c_ProjName.Enabled == false)
{
c_ReleaseName.ToolTip = "Please select your Country name & Project name first";
}
else if (c_ProjName.Enabled == false)
{
c_ReleaseName.ToolTip = "Please select your Project name first";
}
else
{
c_ProjName.ToolTip = "Please select your Release name.";
}
}
}
}
现在,当我运行它时,它在 Internet Explorer 页面中出现以下错误:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 21: public void UserIdentification()
Line 22: {
Line 23: c_LOB.Items.Clear();
Line 24: c_LOB.Items.Add("RUSA");
Line 25: c_LOB.Items.Add("RETAIL");
Source File: C:\Documents and Settings\rs63386\my documents\visual studio 2010\Projects\GPTRGT\GPTRGT\Homepage.aspx.cs Line: 23
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
GPTRGT.validation.UserIdentification() in C:\Documents and Settings\rs63386\my documents\visual studio 2010\Projects\GPTRGT\GPTRGT\Homepage.aspx.cs:23
GPTRGT.Homepage.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\rs63386\my documents\visual studio 2010\Projects\GPTRGT\GPTRGT\Homepage.aspx.cs:15
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272