ziorrinfotech
asked on
Page_Error event does not get when c# code is in separte file
I am have two web form and one of web form has c# code in a separate file and second have in the same file.
When I add the code given below in to the second page then it is working fine. I am getting the error information as Page_error event gets fired.
but when I paste the same code on web form 1 in a separate code file then it does not work.
could any one tell me how can i use Page_error event when i write code in a separate code file
When I add the code given below in to the second page then it is working fine. I am getting the error information as Page_error event gets fired.
but when I paste the same code on web form 1 in a separate code file then it does not work.
could any one tell me how can i use Page_error event when i write code in a separate code file
<script language=C# runat="server">
void Page_Load(object sender, System.EventArgs e)
{
throw(new ArgumentNullException());
}
public void Page_Error(object sender,EventArgs e)
{
Exception objErr = Server.GetLastError().GetBaseException();
string err = "<b>Error Caught in Page_Error event</b><hr><br>" +
"<br><b>Error in: </b>" + Request.Url.ToString() +
"<br><b>Error Message: </b>" + objErr.Message.ToString()+
"<br><b>Stack Trace:</b><br>" +
objErr.StackTrace.ToString();
Response.Write(err.ToString());
Server.ClearError();
}
</script>
ASKER
Hi aibusinesssolutions
Thanks for the help,
I have AutoEventWireup="true" in aspx page then also will it my code not work ??
secondly
I tried the code which you gave me,
protected void Page_Load(object sender, EventArgs e)
{
this.Error += new System.EventHandler(this.P age_Error) ;
throw (new ArgumentException("Directo ry name cannot contain the "));
}
but then also the no change Page_error event does not fired.
help me here pls
Thanks for the help,
I have AutoEventWireup="true" in aspx page then also will it my code not work ??
secondly
I tried the code which you gave me,
protected void Page_Load(object sender, EventArgs e)
{
this.Error += new System.EventHandler(this.P
throw (new ArgumentException("Directo
}
but then also the no change Page_error event does not fired.
help me here pls
AutoEventWireup does not work with C#, only VB.
All AutoEventWireup does is add "Handles" to the end of functions, which C# can not use, so you have to wire everything up yourself.
But anyways, you need to move the init to the Page_Init function.
protected void Page_Init(object sender, EventArgs e)
{
this.Page.Error += new System.EventHandler(this.P age_Error) ;
}
All AutoEventWireup does is add "Handles" to the end of functions, which C# can not use, so you have to wire everything up yourself.
But anyways, you need to move the init to the Page_Init function.
protected void Page_Init(object sender, EventArgs e)
{
this.Page.Error += new System.EventHandler(this.P
}
If you want that to be global error catching, you might be better off adding it to your global.asax file in the Application_Error sub
protected void Application_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetB aseExcepti on();
string err = "Error Caught in Application_Error event\n" +
"Error in: " + Request.Url.ToString() +
"\nError Message:" + objErr.Message.ToString() +
"\nStack Trace:" + objErr.StackTrace.ToString ();
EventLog.WriteEntry("Sampl e_WebApp", err,EventL ogEntryTyp e.Error);
//Server.ClearError();
//additional actions...
}
protected void Application_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetB
string err = "Error Caught in Application_Error event\n" +
"Error in: " + Request.Url.ToString() +
"\nError Message:" + objErr.Message.ToString() +
"\nStack Trace:" + objErr.StackTrace.ToString
EventLog.WriteEntry("Sampl
//Server.ClearError();
//additional actions...
}
ASKER
Hi aibusinesssolutions
protected void Page_Init(object sender, EventArgs e)
{
this.Page.Error += new System.EventHandler(this.P age_Error) ;
}
this also does not work, same old result.
protected void Page_Init(object sender, EventArgs e)
{
this.Page.Error += new System.EventHandler(this.P
}
this also does not work, same old result.
Set your autoeventwireup to false and try it.
Actually, after a little more research, it appears that the Page_Error is supposed to be wired up for you when you have autoeventwireup set to true, so I'm not sure why it isn't firing.
Did you try putting it in your application_error function?
Did you try putting it in your application_error function?
ASKER
yes I tried application_error as welll but that is also not getting fired.
Did you change your Page declaration after you pasted it in to a separate file?
<%@ Page language="c#" Codebehind="filename.cs" Inherits="classname" %>
<%@ Page language="c#" Codebehind="filename.cs" Inherits="classname" %>
ASKER
just tried it but no change.
Even i tried AutoEventWireup=false, then Page_load, init event were not getting fired
Even i tried AutoEventWireup=false, then Page_load, init event were not getting fired
Can you paste your page declaration and the first part of your page.cs file?
ASKER
sure
here is my page.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageEvent.aspx.c s" Inherits="PageEvent" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>
</html>
========================== ========== ========== ========== ========== ========== ========== =
here is .cs code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls. WebParts;
using System.Web.UI.HtmlControls ;
public partial class PageEvent : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
this.Page.Error += new System.EventHandler(this.P age_Error) ;
}
protected void Page_Load(object sender, EventArgs e)
{
throw (new ArgumentException("Directo ry name cannot contain the "));
}
public void Page_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetB aseExcepti on();
string err = "<b>Error Caught in Page_Error event</b><hr><br>" +
"<br><b>Error in: </b>" + Request.Url.ToString() +
"<br><b>Error Message: </b>" + objErr.Message.ToString() +
"<br><b>Stack Trace:</b><br>" +
objErr.StackTrace.ToString ();
Response.Write(err.ToStrin g());
Server.ClearError();
}
protected void Button1_Click(object sender, EventArgs e)
{
throw (new ArgumentException("Directo ry name cannot contain the "));
}
}
here is my page.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageEvent.aspx.c
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>
</html>
==========================
here is .cs code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
using System.Web.UI.HtmlControls
public partial class PageEvent : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
this.Page.Error += new System.EventHandler(this.P
}
protected void Page_Load(object sender, EventArgs e)
{
throw (new ArgumentException("Directo
}
public void Page_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetB
string err = "<b>Error Caught in Page_Error event</b><hr><br>" +
"<br><b>Error in: </b>" + Request.Url.ToString() +
"<br><b>Error Message: </b>" + objErr.Message.ToString() +
"<br><b>Stack Trace:</b><br>" +
objErr.StackTrace.ToString
Response.Write(err.ToStrin
Server.ClearError();
}
protected void Button1_Click(object sender, EventArgs e)
{
throw (new ArgumentException("Directo
}
}
I just copied your code, and took out the page_init function to let it wire it up itself, and it worked.
public partial class PageEvent : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
throw (new ArgumentException("Directo ry name cannot contain the "));
}
public void Page_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetB aseExcepti on();
string err = "<b>Error Caught in Page_Error event</b><hr><br>" +
"<br><b>Error in: </b>" + Request.Url.ToString() +
"<br><b>Error Message: </b>" + objErr.Message.ToString() +
"<br><b>Stack Trace:</b><br>" +
objErr.StackTrace.ToString ();
Response.Write(err.ToStrin g());
Server.ClearError();
}
}
public partial class PageEvent : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
throw (new ArgumentException("Directo
}
public void Page_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetB
string err = "<b>Error Caught in Page_Error event</b><hr><br>" +
"<br><b>Error in: </b>" + Request.Url.ToString() +
"<br><b>Error Message: </b>" + objErr.Message.ToString() +
"<br><b>Stack Trace:</b><br>" +
objErr.StackTrace.ToString
Response.Write(err.ToStrin
Server.ClearError();
}
}
ASKER
I don't know why it is not working on my pc.
I did the same thing which you mention and does not work for me
I did the same thing which you mention and does not work for me
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
this.Error += new System.EventHandler (this.Page_Error );