Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

Eventhandler comes out as null

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
Camillia
Asked:
Camillia
  • 8
  • 5
1 Solution
 
oobaylyCommented:
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
 
CamilliaAuthor Commented:
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
 
CamilliaAuthor Commented:
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
Technology Partners: 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!

 
oobaylyCommented:
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
 
CamilliaAuthor Commented:

 how can I hook it?? I can add the null check but i need this to be hooked...
0
 
oobaylyCommented:
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
 
CamilliaAuthor Commented:
let me try.
0
 
CamilliaAuthor Commented:
what is "onmastertab"?? I have OnTabClick event but what is onmastertab..same thing??
0
 
oobaylyCommented:
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
 
CamilliaAuthor Commented:
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
 
oobaylyCommented:
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
 
CamilliaAuthor Commented:
let me see, hope you're right.Spent all day on this.
0
 
CamilliaAuthor Commented:
YES, FINALLY, it worked. Thanks so much for your help. The entire day on this. Thanks
0

Featured Post

Technology Partners: 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!

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now