Re: how to throw (raise) exception from a custom control

From: Victor Garcia Aprea [MVP] (vga_at_NOobiesSPAM.com)
Date: 10/24/04

  • Next message: A Traveler: "Re: Init Control when dropped on form??"
    Date: Sun, 24 Oct 2004 17:20:45 -0300
    
    

    Hi Gopal,

    If I'm following you, what you need is a way to catch an exception throw by
    one of your custom controls, and catch it at the page level, is that right?

    If so, you can attach to the Error event or (what I told you before) just
    override the OnError method of Page class.

    A year ago I wrote a small post on the Error event and some of its
    particularitites, you can find it here[1]

    [1] http://weblogs.asp.net/vga/archive/2003/06/16/8748.aspx

    -- 
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://clariusconsulting.net/vga
    My profile: http://aspnet2.com/mvp.ashx?vga
    "Gopal Krish" <geeksgk@yahoo.com> wrote in message 
    news:8b4092e6.0410231844.27044e1d@posting.google.com...
    > Hi Victor,
    >
    > I couldn't find a OnError method in the Control or the WebControl
    > class. I didn't understand what you meant by page onError method. The
    > error occurs in the aspx page (Page_Load) but the control still goes
    > to the custom control class.
    >
    > Any thoughts?
    >
    >
    > "Victor Garcia Aprea [MVP]" <vga@NOobiesSPAM.com> wrote in message 
    > news:<#WRezFKuEHA.3524@TK2MSFTNGP15.phx.gbl>...
    >> Hi Gopal,
    >>
    >> You can override the page OnError method and there you will have a chance 
    >> to
    >> handle the exception thrown by your control.
    >>
    >> -- 
    >> Victor Garcia Aprea
    >> Microsoft MVP | ASP.NET
    >> Looking for insights on ASP.NET? Read my blog:
    >> http://clariusconsulting.net/vga
    >> My profile: http://aspnet2.com/mvp.ashx?vga
    >>
    >>
    >> "Gopal Krish" <geeksgk@yahoo.com> wrote in message
    >> news:8b4092e6.0410172015.79f2185c@posting.google.com...
    >> > 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 
    

  • Next message: A Traveler: "Re: Init Control when dropped on form??"

    Relevant Pages

    • Re: Databinding to user control
      ... To work around the issue while binding directly to a DataSet do the ... How do you create a custom user control to be binded to a DataSet so the ... DataSet knows when the data used by the control is updated? ... In the title bar is the number of records that have been changed, ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Databinding to user control
      ... control displays one of the columns correctly via the data binding using the ... MsgData set property. ... The DataGridView handles the state of row changes correctly in my ... that I have a custom user control which displays one of the data set ...
      (microsoft.public.dotnet.languages.csharp)
    • RE: Awareness of user control TestContainer
      ... custom server control)? ... Microsoft MSDN Online Support Lead ... Awareness of user control TestContainer ... In my code for a custom user control, is there a way to determine whether ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: how to throw (raise) exception from a custom control
      ... > Unable to throw a exception from within the custom user control I ... > protected override void CreateChildControls() ...
      (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
    • Re: how to throw (raise) exception from a custom control
      ... I couldn't find a OnError method in the Control or the WebControl ... >> Unable to throw a exception from within the custom user control I ...
      (microsoft.public.dotnet.framework.aspnet.buildingcontrols)