【问题标题】:Getting a runtime exception when I add a UserControl on a page, but there are no compiler errors在页面上添加 UserControl 时出现运行时异常,但没有编译器错误
【发布时间】:2011-10-04 13:59:11
【问题描述】:

引发了“System.Web.HttpUnhandledException”类型的异常。

Step into: Stepping over non-user code 'System.Web.UI.Control.OnLoad'
Step into: Stepping over non-user code 'System.Web.UI.Control.LoadRecursive'
Step into: Stepping over non-user code 'System.Web.UI.Control.LoadRecursive'
Step into: Stepping over non-user code 'System.Web.UI.Control.LoadRecursive'
Step into: Stepping over non-user code 'System.Web.UI.Page.ProcessRequestMain'
Step into: Stepping over non-user code 'System.Web.UI.Page.ProcessRequest'
Step into: Stepping over non-user code 'System.Web.UI.Page.ProcessRequest'
Step into: Stepping over non-user code 'System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute'
Step into: Stepping over non-user code 'System.Web.HttpApplication.ExecuteStep'
A first chance exception of type 'System.NullReferenceException' occurred in App_Code.2x2ldwnl.dll
A first chance exception of type 'System.Exception' occurred in App_Code.2x2ldwnl.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in mscorlib.dll
A first chance exception of type 'System.NullReferenceException' occurred in App_Code.2x2ldwnl.dll
A first chance exception of type 'System.Exception' occurred in App_Code.2x2ldwnl.dll

我的用户控件在构建它自己的东西时也可以正常编译,但在我运行调试演练时会崩溃。 有什么想法可能导致这种情况吗?

用户控制代码:

Call Function
protected void DisplaySendMessageQuestion(string title, int messageType)
{
    UCResetPasswordDisplay.SetVisible(true);
    UCResetPasswordDisplay.MessageType = messageType.ToString();
    UCResetPasswordDisplay.Title = title;
}

用户控件 aspx.cs

public partial class UserControls__ResetPasswordPopup : System.Web.UI.UserControl
{
    public event EventHandler YesResetClick;
    public event EventHandler NoResetClick;


protected void Page_Load(object sender, EventArgs e)
{
    PnlAddConfirmation.CssClass = "PanelResetPass";
    PnlAddConfirmation.CssClass = "PanelYesNoCore";
    txtUsername.Focus();
}

public string MessageType
{
    get { return this.lblMessageType.Text; }
    set { this.lblMessageType.Text = value; }
}

public string UserData
{
    get { return this.txtUsername.Text; }
    set { this.txtUsername.Text = value; }
}

public string Title
{
    get { return this.lblEventTitle.Text; }
    set { this.lblEventTitle.Text = value; }
}

public bool IsVisible
{
    get { return this.PnlAddConfirmation.Visible; }
    set { this.PnlAddConfirmation.Visible = value; }
}

public string ErrorMessage
{
    get { return this.LblEventNotification.Text; }
    set { this.LblEventNotification.Text = value; }
}

public void SetVisible(bool Visible)
{
    PnlAddConfirmation.Visible = Visible;
  //  PnlScreenCover.Visible = Visible;
}

protected void ChangePasswordButton_Click(object sender, EventArgs e)
{
    if (YesResetClick != null)
    {
        YesResetClick(this, EventArgs.Empty);
    }
}
protected void ImgBtnCancel_Click(object sender, EventArgs e)
{
    if (NoResetClick != null)
    {
          NoResetClick(this, EventArgs.Empty);
        }
    }
}

【问题讨论】:

  • 发布代码会有所帮助。请问您是在页面生命周期的哪个阶段添加控件的?
  • 您的用户控件中有什么?似乎 Usercontrol 的内容导致错误
  • 编译器只会捕获编译时错误,不会捕获运行时错误。
  • Bump 添加了代码,我要做的就是将输入从一个文本框返回到调用用户控件的页面,但我什至无法加载用户控件。

标签: c# .net asp.net exception


【解决方案1】:

根据您发布的代码 - 以下其中一项必须为真:

  • PnlAddConfirmation 为空
  • txtUsername 为空

因为 NullReferenceException 被抛出在 OnLoad 方法中 - 因此你的控件的 Page_Load 处理程序。

您可以做的一件事是对 VS 中的所有异常启用中断 - 打开菜单:调试 -> 异常。在显示的对话框中(如下),确保勾选了“Thrown”复选框。

附加调试器后,您应该会被直接发送到发生错误的代码行。确保您的 web.config 也设置了 <compilation mode="debug">,否则您将没有必要的符号来使其正常工作。

【讨论】:

    【解决方案2】:

    这是一个验证器控件,指向验证更改的文本框名称。 即使在调试模式下,正如您所建议的那样,调试器也没有注意到它。 我把整个东西移到了一个干净的项目中,调试器终于注意到了。

    我很生气,我的工作需要升级到 vs2010 并且有一个更不迟钝的 VS 调试器,拼写错误的名称应该被视为错误,这是最常见的人为错误之一。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 2013-12-26
      相关资源
      最近更新 更多