Solved

Eventhandler comes out as null

Posted on 2009-03-30
13
548 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 15

Expert Comment

by:oobayly
ID: 24021480
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
ID: 24021558
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
ID: 24021601
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:oobayly
ID: 24021620
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
ID: 24021657

 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
ID: 24022026
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
 
LVL 7

Author Comment

by:Camillia
ID: 24022082
let me try.
0
 
LVL 7

Author Comment

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

Expert Comment

by:oobayly
ID: 24022248
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
ID: 24022297
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
ID: 24022352
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
ID: 24022383
let me see, hope you're right.Spent all day on this.
0
 
LVL 7

Author Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

761 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