wiring up html attribute declared event handlers

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default wiring up html attribute declared event handlers

    Hi,


    I have a UserControl derived class:

    <ns:votingbutton runat="server" id="btn1" onclick="votingbuttonclick" />

    My question is, what code do I need in place in the codebehind for this to
    be wired up? All the examples I've seen wire up the event programatically,
    thus making this declarative attribute irrelevant. e.g.

    btn1.click += new EventHandler(this.votingbuttonclick);

    I've been looking at reflection and the control's Attribute collection, but
    this looks awfully messy? I've tried using reflector to reverse engineer how
    the stock webcontrols do it, but I just can't see it. I figure an attribute
    hint is needed on an onclick property perhaps, but I can't find anything
    that'll do the job?

    Any hints?

    - Oisin



    Oisin Grehan Guest

  2. Similar Questions and Discussions

    1. Event handlers: How exactly do they work?
      I'm working on a C# serial connection. I'm talking to a device that's running embedded C. I have an event handler that handles incoming data....
    2. Event handlers called twice, or not at all
      Ok, sorry for the long post in advance, but I need an expert opinion here. I have already crawled through the groups via google and additionally...
    3. event handlers
      Does anyone know how to cause the cursor to advance to the next field on a form after the first field has been filled?
    4. Dynamic separator row causing ButtonColumn event wiring problems
      Hello, I have been attempting to create a Datagrid that inserts a separator row between databound rows using code recommended by DataGrid Girl in...
    5. Losing event handlers
      I have written an ASP.NET app with C# code-behind. While modifying the code and testing it, I find that I will sometimes lose all event handlers...
  3. #2

    Default Re: wiring up html attribute declared event handlers

    Hi Natty,

    You misunderstand or misread my question. If you read my first post
    correctly, you'll see that I'm referring to a UserControl derived control.
    It is a server-side user-defined .ascx control, declared in an aspx page
    like so:

    <ns:mycontrol runat="server" onclick="myhandler" id="myctrl1"/>

    much like the vanilla ASP control:

    <asp:linkbutton runat="server" onclick="clickhandler" text="text"
    id="lnkbtn1"/>

    I want to know how to tie up the EventHandler delegate to a method called
    "myhandler" in a page containing my custom control _without_ having to
    programmatically do it via:

    myctrl1.click += new EventHandler(myhandler);

    When you add a linkbutton, you do not have to do it when you declare it via
    the "onclick" attribute.

    This has _nothing_ to do with client side script or postbacks at all.

    - Oisin


    "Natty Gur" <natty@dao2com.com> wrote in message
    news:%23PMwhegSDHA.1688@TK2MSFTNGP11.phx.gbl...
    > Hi,
    >
    > onclick="votingbuttonclick" will generate a call to client side script
    > with that name and it won't cause any call to the server. if you want
    > event to occur on the server side you need to generate a postback to the
    > server with the correct data in the __EVENTTARGET and __EVENTARGUMENT
    > fields.
    >

    Oisin Grehan Guest

  4. #3

    Default Re: wiring up html attribute declared event handlers

    Hi Natty,


    You still don't get it. I _know_ a delegate is needed. It can't work without
    it. Please read my post again. I'll try to make it really simple for you, as
    perhaps English is not your first language.

    Look at this declaration for an asp:linkbutton

    <asp:linkbutton runat="server" onclick="myhandler" id="btn1" />
    ^^^^^^^^^^^^^^^^^^^

    The sgml attribute "onclick" is specifying a method to handle the click
    event. If you check out the codebehind for this page, you'll see that there
    is NO code to tie the delegate to the eventhandler. It just works. It is
    added at run-time, not compile-time. The only thing the IDE will add is a
    variable reference, e.g. "protected System.Web.UI.WebControls.LinkButton
    btn1;".

    Think about it for a second. If I added:

    btn1.click += new EventHandler(this.myhandler)

    to my codebehind page, why would I need 'onclick="myhandler"' on the
    declaration? Yes, that's right -- I wouldn't need it. Understand now? I was
    asking if anyone knew the [Attribute] or suitable reflection code to use to
    automatically wireup sgml attribute eventhandlers.

    Agh!! :-)

    - Oisin














    "Natty Gur" <natty@dao2com.com> wrote in message
    news:eGoo$aoSDHA.940@TK2MSFTNGP11.phx.gbl...
    > Hi,
    >
    > Sorry about my misunderstanding.
    >
    > Any way you can't do it without using EventHandler. Every event that
    > needs to be handling must use it in order to attach function to the
    > event. Even the linkbutton click event uses it. You can see it in
    > InitializeComponent function. The only different is that VS embed the
    > code for you.
    >
    > private void InitializeComponent()
    > {
    > this.LinkButton1.Click += new
    > System.EventHandler(this.LinkButton1_Click);
    > this.Load += new System.EventHandler(this.Page_Load);
    >
    > }
    >
    > Natty Gur, CTO
    > Dao2Com Ltd.
    > 28th Baruch Hirsch st. Bnei-Brak
    > Israel , 51114
    >
    > Phone Numbers:
    > Office: +972-(0)3-5786668
    > Fax: +972-(0)3-5703475
    > Mobile: +972-(0)58-888377
    >
    > Know the overall picture
    >
    >
    > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!

    Oisin Grehan Guest

  5. #4

    Default Re: wiring up html attribute declared event handlers

    Declarative Syntax for Event Wiring
    ASP.NET pages provide a declarative syntax for wiring server-side event
    handlers on the control's tag:

    <asp:Button id="button1" OnClick="button1_Click" Text="Submit"
    runat="server" />

    This syntax provides an intuitive model for page developers because it
    is similar to the syntax for binding a client-side event handler on an
    HTML tag (onmouse*over="client-side script function name").

    When the page parser parses the .aspx file (as we described in "From
    Text to Controls" in Chapter 2), it transforms the declarative syntax
    into code that binds an event handler to an event via an event delegate
    instance:

    button1.Click += new EventHandler(this.button1_Click);


    Natty Gur, CTO
    Dao2Com Ltd.
    28th Baruch Hirsch st. Bnei-Brak
    Israel , 51114

    Phone Numbers:
    Office: +972-(0)3-5786668
    Fax: +972-(0)3-5703475
    Mobile: +972-(0)58-888377

    Know the overall picture


    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Natty Gur Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139