G
Gopal Krish
Here is an interesting situation I'm facing while creating custom
controls.
Problem Abstract:
Unable to throw a exception from within the custom user control I
developed as a DLL.
Details:
I wrote a simple custom user control which creates a text box
dynamically.
Then I added try catch in the custom user control and deliberately
inserted a run time error to test the exception handling from custom
user control back to the calling web page.
The custom user control code is as follows and is very simple
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace WebControlLibrary1
{
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
protected override void CreateChildControls()
{
try
{
int[] arr = new int[2]{1,2};
TextBox myTextBox = new TextBox();
myTextBox.Text = arr[4].ToString();
Controls.Add(myTextBox);
}
catch(Exception Ex)
{
throw new Exception(Ex.Message);
}
}
protected override void OnPreRender(EventArgs e)
{
}
}
}
As you can see from the code I deliberately introduced a run time
error (arr[4]) to test the error os propagated to the calling web
page.
Now, when I used this control in my web form and try to run it I
expect that the error will be propagated from the user control to my
web page and display is somewhere.
Here is the complete code for my web form's code behind
using ......
namespace WebApplication1
{
public class WebForm1 : System.Web.UI.Page
{
protected WebControlLibrary1.WebCustomControl1 mySimpleTextControl;
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender, System.EventArgs e)
{
}
}
}
When I run this code, the error is not propagated back to this page
because, as you can see, there is no place for me to put try catch
because the custom user control is added to this page in design time.
It still throws an unhandled exception at the line "throw new
Exception(Ex.Message);".
It does not work (even throw an unhandled exception or work correctly)
if I add the custom user control dynamically thru code.
Can someone throw some insights to this behavior?
Thanks
controls.
Problem Abstract:
Unable to throw a exception from within the custom user control I
developed as a DLL.
Details:
I wrote a simple custom user control which creates a text box
dynamically.
Then I added try catch in the custom user control and deliberately
inserted a run time error to test the exception handling from custom
user control back to the calling web page.
The custom user control code is as follows and is very simple
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace WebControlLibrary1
{
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
protected override void CreateChildControls()
{
try
{
int[] arr = new int[2]{1,2};
TextBox myTextBox = new TextBox();
myTextBox.Text = arr[4].ToString();
Controls.Add(myTextBox);
}
catch(Exception Ex)
{
throw new Exception(Ex.Message);
}
}
protected override void OnPreRender(EventArgs e)
{
}
}
}
As you can see from the code I deliberately introduced a run time
error (arr[4]) to test the error os propagated to the calling web
page.
Now, when I used this control in my web form and try to run it I
expect that the error will be propagated from the user control to my
web page and display is somewhere.
Here is the complete code for my web form's code behind
using ......
namespace WebApplication1
{
public class WebForm1 : System.Web.UI.Page
{
protected WebControlLibrary1.WebCustomControl1 mySimpleTextControl;
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender, System.EventArgs e)
{
}
}
}
When I run this code, the error is not propagated back to this page
because, as you can see, there is no place for me to put try catch
because the custom user control is added to this page in design time.
It still throws an unhandled exception at the line "throw new
Exception(Ex.Message);".
It does not work (even throw an unhandled exception or work correctly)
if I add the custom user control dynamically thru code.
Can someone throw some insights to this behavior?
Thanks