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\ww wroot\ddd\ ssis\maint enance\ddd \phase2_co nsoleapp\d dd\ddd\bin \Debug\ddd .exe")
Server.Transfer("confirmat ion.aspx")
Catch ex As Exception
Server.Transfer("errors.as px")
lbl_ExceptionMessage.Text = ex.Message
End Try
End Sub
End Class
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
'Start Internet Explorer, passing in a web page.
Try
Diagnostics.Process.Start(
Server.Transfer("confirmat
Catch ex As Exception
Server.Transfer("errors.as
lbl_ExceptionMessage.Text = ex.Message
End Try
End Sub
End Class
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.
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.B utton1_Cli ck);
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";
}
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
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.B
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";
}
ASKER
so even if the code-behind is for index.aspx.vb it will also serve my error.aspx?
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\ww wroot\addd \ssis\main tenance\dd d\phase2_c onsoleapp\ ddd\ddd\bi n\Debug\dd d.exe")
Server.Transfer("confirmat ion.aspx")
Catch ex As Exception
Server.Transfer("errors.as px")
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:Lab el 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>
index.asp.vb
--------------
Imports System.Diagnostics.Process
Partial Class _Default
Inherits System.Web.UI.Page
Protected WithEvents lbl_ExceptionMessage As System.Web.UI.WebControls.
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
'Start Internet Explorer, passing in a web page.
Try
Diagnostics.Process.Start(
Server.Transfer("confirmat
Catch ex As Exception
Server.Transfer("errors.as
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_
<tr>
<td class="confirmation_header
</tr>
<tr>
<td class="confirmation_header
</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("ErrorMs g","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.
u have to put that error message into Items collection of Context object with some key, like
Context.Items.Add("ErrorMs
Server.Transfer("ErrorPage
On page load event of ErrorPage.aspx.cs file's class
string err = Context.Items["ErrorMsg"].
Sorry i could not understand ur question last time.
May be this time i m correct.
Regards
Sachit.
ASKER
I'm using VB.NET not C# so no .cs
ASKER
I think y ou're heading in the right direction, I just need to get the syntax right...
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?
<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?
ASKER
that is...vb.net syntax
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
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
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("ErrorMs g", "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("Error Msg"), String)
Label1.Text = errMsg
Regards
Sachit.
Context.Items.Add("ErrorMs
Server.Transfer("WebForm2.
On page load event of ErrorPage.aspx.cs file's class
Dim errMsg As String
errMsg = CType(Context.Items("Error
Label1.Text = errMsg
Regards
Sachit.
Sorry use Server.Transfer("ErrorPage .aspx") instead of Server.Transfer("WebForm2. aspx")
Regards
Sachit.
Regards
Sachit.
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?
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.
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.
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.
ASKER
ErrorPage.aspx.cs is a code-behind file
ASKER
On page where error might be generated
Context.Items.Add("ErrorMs g", "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("Error Msg"), String)
Label1.Text = errMsg
Context.Items.Add("ErrorMs
Server.Transfer("WebForm2.
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("Error
Label1.Text = errMsg
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
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
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.
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.
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
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
good idea, thanks!
<%@ Page language="c#" Codebehind="WebForm1.aspx.
<!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.P
}
#endregion
}
}