Solved

Eventhandler comes out as null

Posted on 2009-03-30
13
544 Views
Last Modified: 2012-05-06
Not sure what I'm doing wrong here.
I have a Telerik RadStrip control in my AdminMaster page. I added OnTabClick to the RadStrip. I added a Delegate and an eventHandler but not sure why the eventhandler comes out as null:


1. <telerik:RadTabStrip ID="MainMenu" runat="server" AutoPostBack="true" OnTabClick="Tab_Click"   Skin="Outlook">

                    <Tabs>

                          ...
 

2. Outside the AdminMaster class

public delegate void MasterPageTabClickHandler(object sender, RadTabStripEventArgs e);
 

3. Inside AdminMaster class:

public event MasterPageTabClickHandler MasterTab; //*** MasterTab comes out as null
 

4. protected void Tab_Click(object sender, RadTabStripEventArgs e)

    {...}

Open in new window

0
Comment
Question by:Camillia
  • 8
  • 5
13 Comments
 
LVL 15

Expert Comment

by:oobayly
Comment Utility
The event has to be hooked for it to be usable:
ie. the person person using the AdminMaster class would have to do this:
adminMaster.MasterTab += new MasterPageTabClickHandler(adminMaster_MasterTab);

and then the handler:
protected void adminMaster_MasterTab(object sender, RadTabStripEventArgs e){
}

You should always test if the event is null when writing your own events before firing it, as it may be null.

0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
i have all that code in my content page.

Master.Master.MasterTab += new MasterPageTabClickHandler(Master_MasterTab);

*and I have:

 void Master_MasterTab(object sender, RadTabStripEventArgs e)
   {
    string t = string.Empty;
        t = "hello";
    }
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
yeah, comes out as null. I can do a check for null but wont do me anything good. I need to raise this event.
0
 
LVL 15

Expert Comment

by:oobayly
Comment Utility
It then sounds like your trying to fire the event before it's been hooked. It's a bad idea to assume that the event is always hooked, hence checking if it's null.
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility

 how can I hook it?? I can add the null check but i need this to be hooked...
0
 
LVL 15

Expert Comment

by:oobayly
Comment Utility
You've already got the code for hooking the event, but this sequence appears to be happening:
  1. Instance of AdminMaster created
  2. MasterTab event raised
  3. Exception thrown as MasterTab == null
  4. Hook MasterTab event, now MasterTab != null
You obviously never get to step 4.

You could try hooking the event in the asp tag instead of in code behind:

<asp:AdminMaster id="Master" runat="server" onmastertab="Master_MasterTab" />

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 7

Author Comment

by:Camillia
Comment Utility
let me try.
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
what is "onmastertab"?? I have OnTabClick event but what is onmastertab..same thing??
0
 
LVL 15

Expert Comment

by:oobayly
Comment Utility
Sorry, my bad, we're talking about a masterpage, not a webcontrol aren't we.

Can you tell me at what stage you are calling the line
Master.Master.MasterTab += new MasterPageTabClickHandler(Master_MasterTab);
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
that's in my content page.

 void Master_MasterTab(object sender, RadTabStripEventArgs e)
    {
        string t = string.Empty;
        t = "hello";
    }
    protected void Page_Load(object sender, EventArgs e)
    {



       Master.Master.MasterTab += new MasterPageTabClickHandler(Master_MasterTab);
0
 
LVL 15

Accepted Solution

by:
oobayly earned 500 total points
Comment Utility
Aha, in page load. Try hooking the event in Page_Init, this is fired before Page_Load, so you may have more luck.
More information about when ASP.Net page event are fired ASP.Net Page Life Cycle


  protected void Page_Init(object sender, EventArgs e) {

     Master.Master.MasterTab += new MasterPageTabClickHandler(Master_MasterTab);

  }

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
let me see, hope you're right.Spent all day on this.
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
YES, FINALLY, it worked. Thanks so much for your help. The entire day on this. Thanks
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now