Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. user control - custom events - convert to inline

user control - custom events - convert to inline

Scheduled Pinned Locked Moved ASP.NET
helpcsharpasp-nettoolsquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leftyfarrell
    wrote on last edited by
    #1

    Our client wanted delivery of the code using inline scripting, but for obvious reasons, we developed the ASP.NET 1.1 app using codebehind. We have an issue now when trying to convert the codebehind code to an inline format. The control fires a custom event (to pass detailed error info up the containing page, should they choose to implement a listener for the event). Using C# the custom control is doing something like this: ============== public delegate void UserControlEventHandler(object sender, ThinData.Common.UserControlEventArgs e); public event UserControlEventHandler UserControlError; ============== the parent page consuming the control was doing this: ============== PersInfo1.UserControlError += new ClientName.RegistrationUserControls.PersonalInformationForm.UserControlEventHandler(Generic_UserControlError); ============== The above works fine using codebehind. My problem is that within the containing page, when it is converted to inline script, the namespace and name: ClientName.RegistrationUserControls.PersonalInformationForm... cannot be resolved properly. The error is "missing assembly reference?" How should I be doing this so I can wire up the event in the parent page properly? I've not worked with inline coding much at all, so any help would be appreciated. Thanks.

    J L 2 Replies Last reply
    0
    • L Leftyfarrell

      Our client wanted delivery of the code using inline scripting, but for obvious reasons, we developed the ASP.NET 1.1 app using codebehind. We have an issue now when trying to convert the codebehind code to an inline format. The control fires a custom event (to pass detailed error info up the containing page, should they choose to implement a listener for the event). Using C# the custom control is doing something like this: ============== public delegate void UserControlEventHandler(object sender, ThinData.Common.UserControlEventArgs e); public event UserControlEventHandler UserControlError; ============== the parent page consuming the control was doing this: ============== PersInfo1.UserControlError += new ClientName.RegistrationUserControls.PersonalInformationForm.UserControlEventHandler(Generic_UserControlError); ============== The above works fine using codebehind. My problem is that within the containing page, when it is converted to inline script, the namespace and name: ClientName.RegistrationUserControls.PersonalInformationForm... cannot be resolved properly. The error is "missing assembly reference?" How should I be doing this so I can wire up the event in the parent page properly? I've not worked with inline coding much at all, so any help would be appreciated. Thanks.

      J Offline
      J Offline
      japanreddy
      wrote on last edited by
      #2

      make sure the namespaces are referenced properly like this : <%@ Import Namespace="System.Data" %> <%@ Register TagPrefix="intex" TagName="TimeDataPlot" rc="myWebControl.ascx" %> <%@ Page Language="C#" Trace="false"%>

      L 1 Reply Last reply
      0
      • J japanreddy

        make sure the namespaces are referenced properly like this : <%@ Import Namespace="System.Data" %> <%@ Register TagPrefix="intex" TagName="TimeDataPlot" rc="myWebControl.ascx" %> <%@ Page Language="C#" Trace="false"%>

        L Offline
        L Offline
        Leftyfarrell
        wrote on last edited by
        #3

        thanks for the reply. Yeah, the directives are defined similar to above. If we remove the event handling code from the parent containing page, everything seems to run properly, so I think our existing directives are appropriate (except maybe for something missing to allow the event wireup). Of course, removing the event handling code from the parent page removes the functionality and purpose of putting the custom event into the control in the first place. any other ideas?

        1 Reply Last reply
        0
        • L Leftyfarrell

          Our client wanted delivery of the code using inline scripting, but for obvious reasons, we developed the ASP.NET 1.1 app using codebehind. We have an issue now when trying to convert the codebehind code to an inline format. The control fires a custom event (to pass detailed error info up the containing page, should they choose to implement a listener for the event). Using C# the custom control is doing something like this: ============== public delegate void UserControlEventHandler(object sender, ThinData.Common.UserControlEventArgs e); public event UserControlEventHandler UserControlError; ============== the parent page consuming the control was doing this: ============== PersInfo1.UserControlError += new ClientName.RegistrationUserControls.PersonalInformationForm.UserControlEventHandler(Generic_UserControlError); ============== The above works fine using codebehind. My problem is that within the containing page, when it is converted to inline script, the namespace and name: ClientName.RegistrationUserControls.PersonalInformationForm... cannot be resolved properly. The error is "missing assembly reference?" How should I be doing this so I can wire up the event in the parent page properly? I've not worked with inline coding much at all, so any help would be appreciated. Thanks.

          L Offline
          L Offline
          Leftyfarrell
          wrote on last edited by
          #4

          To figure this out, I've created a new app, with a single control and a single web page. When converting to inline code, I wind up with this: User Control Code: ============================== private void Page_Load(object sender, System.EventArgs e) { try { throw new NullReferenceException("my error here"); } catch (Exception ex) { HandleControlError(ex, false); } } #region Error Handling Event private void HandleControlError(Exception ex, bool bErrorFromChildWebControl) { if (!bErrorFromChildWebControl)//only log the error once { //log to email and/or event log //GlobalLibrary.HandleControlError( ex ); } //fire error event so page consuming control can trap and process if desired UserControlEventArgs e = new UserControlEventArgs(ex); OnUserControlError(e); } /// /// Raises a Error event so the parent page can inspect/work with errors /// /// UserControlEventArgs object which contains a property to retrieve the exception protected virtual void OnUserControlError(UserControlEventArgs e) { if (UserControlError != null) { //Invokes the delegates. UserControlError(this, e); } } public delegate void UserControlEventHandler(object sender, UserControlEventArgs e); public event UserControlEventHandler UserControlError; /// /// UserControlEventArgs is an EventArgs object used for passing error objects up to the calling page /// public class UserControlEventArgs : EventArgs { private Exception ex = null; public UserControlEventArgs(Exception e) { ex = e; } #region Public Properties public Exception UserControlException { get { return ex; } } #endregion } =================================== The containing Web Page: ==================================== <%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="UserControls/WebUserControl1.ascx" %> private void Page_Load(object sender, System.EventArgs e) { // THIS IS THE PROBLEM LINE WebUserControl11.UserControlError += new UserControls.WebUserControl1.UserControlEventHandler(Generic_UserControlError); } p</x-turndown>

          M 1 Reply Last reply
          0
          • L Leftyfarrell

            To figure this out, I've created a new app, with a single control and a single web page. When converting to inline code, I wind up with this: User Control Code: ============================== private void Page_Load(object sender, System.EventArgs e) { try { throw new NullReferenceException("my error here"); } catch (Exception ex) { HandleControlError(ex, false); } } #region Error Handling Event private void HandleControlError(Exception ex, bool bErrorFromChildWebControl) { if (!bErrorFromChildWebControl)//only log the error once { //log to email and/or event log //GlobalLibrary.HandleControlError( ex ); } //fire error event so page consuming control can trap and process if desired UserControlEventArgs e = new UserControlEventArgs(ex); OnUserControlError(e); } /// /// Raises a Error event so the parent page can inspect/work with errors /// /// UserControlEventArgs object which contains a property to retrieve the exception protected virtual void OnUserControlError(UserControlEventArgs e) { if (UserControlError != null) { //Invokes the delegates. UserControlError(this, e); } } public delegate void UserControlEventHandler(object sender, UserControlEventArgs e); public event UserControlEventHandler UserControlError; /// /// UserControlEventArgs is an EventArgs object used for passing error objects up to the calling page /// public class UserControlEventArgs : EventArgs { private Exception ex = null; public UserControlEventArgs(Exception e) { ex = e; } #region Public Properties public Exception UserControlException { get { return ex; } } #endregion } =================================== The containing Web Page: ==================================== <%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="UserControls/WebUserControl1.ascx" %> private void Page_Load(object sender, System.EventArgs e) { // THIS IS THE PROBLEM LINE WebUserControl11.UserControlError += new UserControls.WebUserControl1.UserControlEventHandler(Generic_UserControlError); } p</x-turndown>

            M Offline
            M Offline
            minhpc_bk
            wrote on last edited by
            #5

            At runtime, the ASP.NET generates a dynamic class which represents for the requested web page, it's name is <page>_aspx and this class is part of the namespace ASP but not GenericWebApp. So if you want to use the WebUserControl1 class in the inline server code, you'll have to use either the class name only and import the namespace GenericWebApp.UserControls or use the fully qualified name GenericWebApp.UserControls.WebUserControl1. Otherwise, it will result in a compile error as the compilers cannot find the specified namespace or type. To import the namespace, you can use the Import directive:

            <%@ Import Namespace="GenericWebApp.UserControls" %>

            I have no idea if your client accept the source code file .cs or not, you can also use the src attribute of the script tag to point to the source file .cs. In addition, if you use the inline code, you don't need to put the code generated by VS on the web page as the Page_Load event handler is automatically wired up to the Load event of the Page.

            L 1 Reply Last reply
            0
            • M minhpc_bk

              At runtime, the ASP.NET generates a dynamic class which represents for the requested web page, it's name is <page>_aspx and this class is part of the namespace ASP but not GenericWebApp. So if you want to use the WebUserControl1 class in the inline server code, you'll have to use either the class name only and import the namespace GenericWebApp.UserControls or use the fully qualified name GenericWebApp.UserControls.WebUserControl1. Otherwise, it will result in a compile error as the compilers cannot find the specified namespace or type. To import the namespace, you can use the Import directive:

              <%@ Import Namespace="GenericWebApp.UserControls" %>

              I have no idea if your client accept the source code file .cs or not, you can also use the src attribute of the script tag to point to the source file .cs. In addition, if you use the inline code, you don't need to put the code generated by VS on the web page as the Page_Load event handler is automatically wired up to the Load event of the Page.

              L Offline
              L Offline
              Leftyfarrell
              wrote on last edited by
              #6

              thanks for the reply. if I use the Import specified above, it reports Type or Namespace could not be found. In trying different things, I tried removing the event wireup from the PageLoad and am now using this: ====== ====== The above seems to work, but then I get a compilation error in my definition for the Generic_UserControlError function: ====== private void Generic_UserControlError(object sender, UserControlEventArgs e) ====== The error is the same Type or Namespace could not be found for the UserControlEventArgs. With the Import you suggested, or without, and using the fully qualified name, the error is the same, it just changes places (at the Import or the function definition) I should mention that the original codebehind application was created an a project called GenericWebApp... hence the namespace name. The inline code above I'm trying to get going is in a virtual directory called TestInlineCode, and after conversion to the inline code, the class names and namespace defintions have been removed (only the code contents within the classes were copied to blocks on each respective page... so technically I guess their are no namespaces defined? Or was the transfer to inline code not completed properly? thanks in advance for any other ideas </x-turndown>

              L 1 Reply Last reply
              0
              • L Leftyfarrell

                thanks for the reply. if I use the Import specified above, it reports Type or Namespace could not be found. In trying different things, I tried removing the event wireup from the PageLoad and am now using this: ====== ====== The above seems to work, but then I get a compilation error in my definition for the Generic_UserControlError function: ====== private void Generic_UserControlError(object sender, UserControlEventArgs e) ====== The error is the same Type or Namespace could not be found for the UserControlEventArgs. With the Import you suggested, or without, and using the fully qualified name, the error is the same, it just changes places (at the Import or the function definition) I should mention that the original codebehind application was created an a project called GenericWebApp... hence the namespace name. The inline code above I'm trying to get going is in a virtual directory called TestInlineCode, and after conversion to the inline code, the class names and namespace defintions have been removed (only the code contents within the classes were copied to blocks on each respective page... so technically I guess their are no namespaces defined? Or was the transfer to inline code not completed properly? thanks in advance for any other ideas </x-turndown>

                L Offline
                L Offline
                Leftyfarrell
                wrote on last edited by
                #7

                UPDATE after reading my own post above, I tried using: ASP.WebUserControl1_ascx.UserControlEventArgs to reference it and it worked. Thanks to your answer I realized the difference in namespaces and classes. Now my new question is, when transferring from codebehind to inline code, I'd prefer to not have to change the code (as above) because the namespaces change or disappear. ie. GenericWebApp.UserControls.WebUserControl1.UserControlEventArgs has now become: ASP.WebUserControl1_ascx.UserControlEventArgs Can I convert the code to inline script and maintain the namespace definitions so that above namespace code changes are not required?

                M 1 Reply Last reply
                0
                • L Leftyfarrell

                  UPDATE after reading my own post above, I tried using: ASP.WebUserControl1_ascx.UserControlEventArgs to reference it and it worked. Thanks to your answer I realized the difference in namespaces and classes. Now my new question is, when transferring from codebehind to inline code, I'd prefer to not have to change the code (as above) because the namespaces change or disappear. ie. GenericWebApp.UserControls.WebUserControl1.UserControlEventArgs has now become: ASP.WebUserControl1_ascx.UserControlEventArgs Can I convert the code to inline script and maintain the namespace definitions so that above namespace code changes are not required?

                  M Offline
                  M Offline
                  minhpc_bk
                  wrote on last edited by
                  #8

                  Basically, the problem only occurs when the code on a web page makes use of a class which is defined in the user control .ascx. In this case, if you still define the UserControlEventArgs and UserControlEventHandler in the WebUserControl1.ascx file, then you'll have to change the namespaces because at runtime they belong to the WebUserControl1_ascx class, part of the namespace ASP. So IMO, you can work around this trouble by defining the event delegates and event arguments in a seperate assembly instead of putting them on the content files .ascx. To access these classes, you simply use Import directive in the content files of the user controls and the web pages, also the namespace code changes are basically not required. This way IMO can be accepted by your client, otherwise you have to place all code including UI code, business layer, and dataaccess layer ... on the web page, it'll look so ugly and not a good practice at all.

                  L 1 Reply Last reply
                  0
                  • M minhpc_bk

                    Basically, the problem only occurs when the code on a web page makes use of a class which is defined in the user control .ascx. In this case, if you still define the UserControlEventArgs and UserControlEventHandler in the WebUserControl1.ascx file, then you'll have to change the namespaces because at runtime they belong to the WebUserControl1_ascx class, part of the namespace ASP. So IMO, you can work around this trouble by defining the event delegates and event arguments in a seperate assembly instead of putting them on the content files .ascx. To access these classes, you simply use Import directive in the content files of the user controls and the web pages, also the namespace code changes are basically not required. This way IMO can be accepted by your client, otherwise you have to place all code including UI code, business layer, and dataaccess layer ... on the web page, it'll look so ugly and not a good practice at all.

                    L Offline
                    L Offline
                    Leftyfarrell
                    wrote on last edited by
                    #9

                    Thanks for the advice. In my actual project I do have the UserControlEventArgs class declared within an assembly and it is used within all controls. I still have the delegate declared within each control, however. I tried to put the delegate into the assembly as well, but ran into problems doing so. I can't remember exactly what the problems were but I think they were namespace or protection level problems of some kind preventing me from doing it. I might take another crack at moving it into the assembly as well... I gave up earlier thinking it was not possible for some reason.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups