Link to home
Create AccountLog in
Avatar of dba123
dba123

asked on

Name 'lbl_ExceptionMessage' is not declared

What's the quickest and easiest way to add a code-behind for any aspx if it doesn't have one already in VS 2005 and relate it to the correct aspx page on the fly?  I find myself adding a new class file then having to link it by changing its properties every time which is a pain.

once I'm transfered to error.aspx I get "Name 'lbl_ExceptionMessage' is not declared." so I figure I need a code-behind page for error.aspx...but that is not possible to have 2 code-behind pages is it?  The label is defined in my error.aspx page.

index.aspx.vb
----------------
Imports System.Diagnostics.Process

Partial Class _Default
    Inherits System.Web.UI.Page

    Public Sub Run_Hedi2_Process(ByVal sender As Object, ByVal e As System.EventArgs)
        'Declare and instantiate a new process component.
        Dim myproc As System.Diagnostics.Process
        myproc = New System.Diagnostics.Process

        'Do not receive an event when the process exits.
        myproc.EnableRaisingEvents = False
        'Start Internet Explorer, passing in a web page.
        Try
            Diagnostics.Process.Start("\\ddd\f$\inetpub\wwwroot\ddd\ssis\maintenance\ddd\phase2_consoleapp\ddd\ddd\bin\Debug\ddd.exe")
            Server.Transfer("confirmation.aspx")
        Catch ex As Exception
            Server.Transfer("errors.aspx")
            lbl_ExceptionMessage.Text = ex.Message
        End Try

    End Sub

End Class

Avatar of sachitjain
sachitjain
Flag of India image

ASPX Code
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Exp1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 

<html>
  <head>
    <title>WebForm1</title>
    </head>
  <body MS_POSITIONING="GridLayout">
      
    <form id="Form1" method="post" runat="server">

    </form>
      
  </body>
</html>





Codebehind code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Exp1
{
      /// <summary>
      /// Summary description for WebForm1.
      /// </summary>
      public class WebForm1 : System.Web.UI.Page
      {
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.Load += new System.EventHandler(this.Page_Load);
            }
            #endregion
      }
}
Avatar of dba123
dba123

ASKER

sorry, I am talking about VS 2005 GUI, I'm not talking about how to code the page directives.  There has to be some way to right-click a page and tell it to create a bare code-behind for it without having to do this manually

Anyway, how do I resolve my problem with the redirection and then trying to populate the label on my error.aspx page.
For every server control u define in the ASPX page, u should define an object in class in codebehind file like

protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;

and if u want to write an event, say Click event of Button1
u have to append following statement in InitializeComponent() method within class
this.Button1.Click += new System.EventHandler(this.Button1_Click);

where Button1_Click is the name of method to executed on event raise, like
private void Button1_Click(object sender, System.EventArgs e)
{
      Label1.Text = "Hello World";
}
Avatar of dba123

ASKER

so even if the code-behind is for index.aspx.vb it will also serve my error.aspx?
Avatar of dba123

ASKER

This redirects but no error message appears:

index.asp.vb
--------------
Imports System.Diagnostics.Process

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected WithEvents lbl_ExceptionMessage As System.Web.UI.WebControls.Label

    Public Sub Run_Hedi2_Process(ByVal sender As Object, ByVal e As System.EventArgs)
        'Declare and instantiate a new process component.
        Dim myproc As System.Diagnostics.Process
        myproc = New System.Diagnostics.Process

        'Do not receive an event when the process exits.
        myproc.EnableRaisingEvents = False
        'Start Internet Explorer, passing in a web page.
        Try
            Diagnostics.Process.Start("\\ddd\f$\inetpub\wwwroot\addd\ssis\maintenance\ddd\phase2_consoleapp\ddd\ddd\bin\Debug\ddd.exe")
            Server.Transfer("confirmation.aspx")
        Catch ex As Exception
            Server.Transfer("errors.aspx")
            lbl_ExceptionMessage.Text = ex.Message
        End Try

    End Sub

End Class


errors.aspx
---------------

<%@ Page Language="VB" AutoEventWireup="false"%>
<link href="StyleSheet.css" type="text/css" rel="stylesheet">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>sss Process - Page Errors</title>
</head>
<body>
<table align="center" class="confirmation_outer_table">
    <tr>
        <td class="confirmation_header"><b>The following Errors Occured:</b></td>
    </tr>
    <tr>
        <td class="confirmation_header"><asp:Label id="lbl_ExceptionMessage" runat="server"></asp:Label></td>
    </tr>
    <tr>
        <td class="confirmation_body">
       </td>
    </tr>    
   

</table>
<p class="backlink">Click <b><asp:HyperLink ID="HyperLink1"
              NavigateUrl="http://sss/"
              Text="Here"
              Target="_parent"
              runat="server" /></b>
   back
</p>
</body>
</html>
IF u want to show error message on some page like ErrorPage.aspx
u have to put that error message into Items collection of Context object with some key, like

Context.Items.Add("ErrorMsg","Error occured");
Server.Transfer("ErrorPage.aspx");

On page load event of ErrorPage.aspx.cs file's class

string err = Context.Items["ErrorMsg"].ToString();

Sorry i could not understand ur question last time.
May be this time i m correct.

Regards
Sachit.
Avatar of dba123

ASKER

I'm using VB.NET not C# so no .cs
Avatar of dba123

ASKER

I think y ou're heading in the right direction, I just need to get the syntax right...
Avatar of dba123

ASKER

so in my errors.aspx page, if I have a label defined as so:

<asp:Label id="lbl_ExceptionMessage" runat="server"></asp:Label>

how would I grab the context item and set the label's text to that context item that was passed?
Avatar of dba123

ASKER

that is...vb.net syntax
Avatar of dba123

ASKER

so you're saying I can create a code-behind for errors.aspx then?
Hi dba123

To have code placed in codebehind file separately you will have to check
The check box “place code in separate file”
Place below the name besides language dropdown

I don’t know why you want to have two code behind files
But if this does not work lets me know

Good luck
spdude
Avatar of dba123

ASKER

ok, so how would I do this then, if I don't need another code-behind.  All I want to do is capture the error in my index.aspx.vb processing and throw it to an error page.  I need to know how to do this with VB.NET
On page where error might be generated
Context.Items.Add("ErrorMsg", "Error occured")
Server.Transfer("WebForm2.aspx")

On page load event of ErrorPage.aspx.cs file's class
Dim errMsg As String
errMsg = CType(Context.Items("ErrorMsg"), String)
Label1.Text = errMsg

Regards
Sachit.
Sorry use Server.Transfer("ErrorPage.aspx") instead of Server.Transfer("WebForm2.aspx")

Regards
Sachit.
Avatar of dba123

ASKER

yea, but Sachit, I don't usually put my Page_Load directly in my aspx pages.  It is best practice to put that in your code-behind.  so what I am saying is I understand your solution, that it will work but my issue with it is how then can I put the Page_Load for the error.aspx in a code-behind?
Avatar of dba123

ASKER

I guess maybe that's overkill for an error page and I don't need to worry about a code-behind to throw the Page_Load in...just trying to stick to good practice in all my aspx pages which is to separate code from content.
Avatar of dba123

ASKER

>>>On page load event of ErrorPage.aspx.cs file's class

so you're saying you have a code-behind for your error page right?  Ok, I'm using VB.NET so mine would be .vb.  Anyway, so I need to create another code-behind then...is what you agree with.
Avatar of dba123

ASKER

ErrorPage.aspx.cs is a code-behind file
Avatar of dba123

ASKER

On page where error might be generated
Context.Items.Add("ErrorMsg", "Error occured")
Server.Transfer("WebForm2.aspx")

On page load event of ErrorPage.aspx.cs file's class   <-----------  This is a code-behind file right?  You just said I can't have 2 code-behind files, I already have one for my index.aspx page!
Dim errMsg As String
errMsg = CType(Context.Items("ErrorMsg"), String)
Label1.Text = errMsg
Avatar of dba123

ASKER

here is my project setup to be clear:

index.aspx
index.aspx.vb (server.transfer happens in here to my error.aspx if any of the code in here bombs out)
error.aspx
Avatar of dba123

ASKER

My issue is, I always put Page_Load in code-behinds, so I would think to create yet another .vb for error.aspx but you say that it's not possible.  so I need just clarification from everyone if it's just not good to create another code-behind and I should just byte the bullet and put the Page_Load in error.aspx and be done with it.  If I do that, is this bad practice?
Or u can do onething, u could have 2 usercontrols (ASCX) on same page, one for actual functionality and another for error
On error user control, u could have a label to display error

In case of error u could show the error user control with error dispayed and hide the user control
and in normal case, u could show the other user control and keep the error user control as hidden.

Regards
Sachit.
Avatar of dba123

ASKER

yea, I've already done that...I could just keep doing that.  If so, I need to know how to hide/show the errors then and also hide/show the main content.  On error, then I'd want to hide the previous content and show the error content so it looks as though they were ridirected but not sure how to hide specific parts of my page on the exceptions
Avatar of dba123

ASKER

Would I use some sort of DIV or other way in .NET to hide an entire section of the page and then how would I hide/show the other section...essentially my error html and error labels?
ASKER CERTIFIED SOLUTION
Avatar of sachitjain
sachitjain
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of dba123

ASKER

good idea, thanks!