简体   繁体   中英

ASP.NET Event Handler problem on Postback

Hi I have a bit of a doozie.

I have a control with a click event attached. As far as I can see the event should be raised during postback. Things I have checked:

  • The __Eventtarget is correct.
  • The __EventArgs is correct.
  • The control can be found by using page.findControl([string in __Eventtarget]) after init, after load and during prerender.
  • I can cast the found control to IPostBackEventHandler and raise the event manually and it works fine.

What am I missing so the framework can handle this event?

Suggestions on a postcard or below please

I found the problem: it was a third party control registering itself requiring a raise event. This information was found by using custom class inherited from Page, overrode RegisterRequiresRaiseEvent and placing a break point on entry of the overrode sub.

 Public Class Page
    Inherits System.Web.UI.Page

    Public Overrides Sub RegisterRequiresRaiseEvent(ByVal control As System.Web.UI.IPostBackEventHandler)
        If TypeOf control Is Infragistics.WebUI.UltraWebGrid.UltraWebGrid Then Exit Sub
        MyBase.RegisterRequiresRaiseEvent(control)
    End Sub

End Class

Without the code here's a few things that may be causing problems:

  • Control is being added to the hierarchy late in the request cycle (and after the event has fired). This is problem if you're dynamically creating controls (including those in a data bound control like a Repeater, ListView, GridView, etc)
  • The event handler is being added in the code behind not the front end, eg: myButton.Click += new EventHandler(myButton_Click) , and that is not being done on every request. It could be behind a logical path not executed

You have to make sure that the event is wired up in the request cycle. You can do this using the following methods:

There are other possible ways to wire up the events, eg you could do it manually in your code somewhere. But I've found these 2 options to be the safest so that you will be sure that your events will be fired

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM