[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

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

0
dba123
Asked:
dba123
  • 19
  • 7
1 Solution
 
sachitjainCommented:
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
      }
}
0
 
dba123Author Commented:
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.
0
 
sachitjainCommented:
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";
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dba123Author Commented:
so even if the code-behind is for index.aspx.vb it will also serve my error.aspx?
0
 
dba123Author Commented:
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>
0
 
sachitjainCommented:
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.
0
 
dba123Author Commented:
I'm using VB.NET not C# so no .cs
0
 
dba123Author Commented:
I think y ou're heading in the right direction, I just need to get the syntax right...
0
 
dba123Author Commented:
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?
0
 
dba123Author Commented:
that is...vb.net syntax
0
 
dba123Author Commented:
so you're saying I can create a code-behind for errors.aspx then?
0
 
spdudeCommented:
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
0
 
dba123Author Commented:
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
0
 
sachitjainCommented:
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.
0
 
sachitjainCommented:
Sorry use Server.Transfer("ErrorPage.aspx") instead of Server.Transfer("WebForm2.aspx")

Regards
Sachit.
0
 
dba123Author Commented:
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?
0
 
dba123Author Commented:
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.
0
 
dba123Author Commented:
>>>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.
0
 
dba123Author Commented:
ErrorPage.aspx.cs is a code-behind file
0
 
dba123Author Commented:
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
0
 
dba123Author Commented:
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
0
 
dba123Author Commented:
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?
0
 
sachitjainCommented:
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.
0
 
dba123Author Commented:
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
0
 
dba123Author Commented:
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?
0
 
sachitjainCommented:
IF u r using ASCX files then u could directly use Visible properties of user controls on ur page for making them in/visible
If u r not using ASCX then u could have 2 html tables on ur page with runat=server tag and ids, say tblErr and tblFunc

Then on server, u could make these tables in/visible by setting Visible propery of these HtmlTable objects.

Regards
Sachit.
0
 
dba123Author Commented:
good idea, thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 19
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now