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. Client side event handlers in Custom Control

Client side event handlers in Custom Control

Scheduled Pinned Locked Moved ASP.NET
tutorialquestion
4 Posts 2 Posters 2 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.
  • I Offline
    I Offline
    Irshad Ahmad Sulaimani
    wrote on last edited by
    #1

    I have created a custom control CustomTextBox. I want to attach client side event handlers like onfocus, onblur etcs to it. How To raise this event and capture it.? Thanks in Advance.:rose: Irshad Ahmad

    M 1 Reply Last reply
    0
    • I Irshad Ahmad Sulaimani

      I have created a custom control CustomTextBox. I want to attach client side event handlers like onfocus, onblur etcs to it. How To raise this event and capture it.? Thanks in Advance.:rose: Irshad Ahmad

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Assuming your control inherits from TextBox, you can attach client-side event handlers through the Attributes collection. Something like this (assumes a control on the page with its id set to myTextBox):

      void Page_Load(object o, EventArgs e)
      {
      myTextBox.Attributes["onfocus"] = "MyClientSideOnFocusFunction();"
      myTextBox.Attributes["onblur"] = "MyClientSideOnBlurFunction();"
      //... or something like ....
      myTextBox.Attributes["onfocus"] = "window.status = 'Text box help...'; return true;";
      myTextBox.Attributes["onfocus"] = "window.status = ''; return true;";
      }

      I 1 Reply Last reply
      0
      • M Mike Ellison

        Assuming your control inherits from TextBox, you can attach client-side event handlers through the Attributes collection. Something like this (assumes a control on the page with its id set to myTextBox):

        void Page_Load(object o, EventArgs e)
        {
        myTextBox.Attributes["onfocus"] = "MyClientSideOnFocusFunction();"
        myTextBox.Attributes["onblur"] = "MyClientSideOnBlurFunction();"
        //... or something like ....
        myTextBox.Attributes["onfocus"] = "window.status = 'Text box help...'; return true;";
        myTextBox.Attributes["onfocus"] = "window.status = ''; return true;";
        }

        I Offline
        I Offline
        Irshad Ahmad Sulaimani
        wrote on last edited by
        #3

        Hi Thank you for your reply. But my problem is that i don't want to write this code in the page where it is consumed. Actually, I want to add this functionality in the custom control's coding itself. on getting focus, i want my Custom text box to fire an event without postback on server. Hope I'm able to explain my problem. Looking for more help. Thanks n Regards, Irshad Irshad A Sulaimani

        M 1 Reply Last reply
        0
        • I Irshad Ahmad Sulaimani

          Hi Thank you for your reply. But my problem is that i don't want to write this code in the page where it is consumed. Actually, I want to add this functionality in the custom control's coding itself. on getting focus, i want my Custom text box to fire an event without postback on server. Hope I'm able to explain my problem. Looking for more help. Thanks n Regards, Irshad Irshad A Sulaimani

          M Offline
          M Offline
          Mike Ellison
          wrote on last edited by
          #4

          Hi there. You can do this in the control itself. If you are rendering the control yourself (i.e. overriding Render and outputting all the html yourself) then just make sure you output the approprate attributes with the text box tag (e.g. onfocus='...'). If you are subclassing TextBox and letting it handle the rendering, you can add an this.Attributes["onfocus"]='...' within the control code just like you would in the Page_Load of the page. You might for example override the Control.OnLoad()[^] method to add your custom javascript attributes. As an additional point to this: if you want to output a javascript function to the page (something that might be called from your onfocus attribute for example), you may want to review the Page.RegisterClientScriptBlock()[^] method. This in conjuction with Page.IsClientScriptBlockRegistered()[^] will allow you to emit a javascript function once to the page (even if multiple instances of your control are present).

          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