views:

24

answers:

1

Hi All,
I am using Menu control in asp.net. I am creating Menu control using xml databinding.
Here is the xml file;

 <?xml version="1.0" encoding="utf-8" ?>
    <Items Text="">
      <Item Text="" ImgPath="./../images/home.gif"  Url="" Value="Home"   />
      <Item Text="" ImgPath="" Url="" Value="Time Entry" >
        <Item Text="" Value="Our Clients" ImgPath="./../images/oc.gif" Url="~/OurClients.aspx" />
      </Item>
      <Item Text="" ImgPath="" Value="Admin" Url="">
        <Item Text="" Value="About Us" ImgPath="./../images/AboutUs.gif" Url ="~/AboutUs.aspx" />
      </Item>
    </Items>


For creating menu, in .aspx page, I am using;

<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" DisappearAfter="10"
    Width="300px" DataSourceID="XmlDataSource1" StaticEnableDefaultPopOutImage="False" OnMenuItemClick="Menu1_MenuItemClick">
    <StaticMenuItemStyle CssClass="MenuItem" />
    <DynamicHoverStyle CssClass="SubMenuItemHover" />
    <DynamicMenuItemStyle CssClass="SubMenuItem" />
    <StaticHoverStyle CssClass="MenuItemHover" />
    <DataBindings>
        <asp:MenuItemBinding DataMember="Item" NavigateUrlField="Url" TextField="Text" ImageUrlField="ImgPath" ValueField="Value" />
    </DataBindings>
</asp:Menu>


Now, I am trying to get MenuItem from this Menu control. For that, on page load, I am using ;

 MenuItem mn = Menu1.FindItem("Home");


But, it is returning null.
Please help me to get the solution.
Thanks in Advance.

+1  A: 

When you declaratively set a controls DataSourceID (i.e. set it in the aspx), the control is not databound until the page's prerender event. Try call the menu's databind method before you call its FindItem method.

You could also wait until the PreRenderComplete event is fired and run your code there, as long as that isn't too late.

Page Life Cycle

Mike Cellini
Thanks mike. DataBinding did worked. But, I am facing new problem.My Menu control is in master page and I am accessing it from a child page. Now, child page's load event gets fired before master's. so it it throwing "Object referrence not found" error.I am accessing Menu control from child page in Load event.
Vijay Balkawade
Sorry, but I can't think of anything off the top of my head. I would need to see the code-behind of the master/child pages related to the menu. I assume to find the menu you're doing a page.master.findcontrol("menu1")?
Mike Cellini
Hey Mike, sorry for inconvenience. What I am doing in code behind is, I am just assigning xml file name to XMLDatasource and providing the xpath. So, findcontrol() will return the menu, but I am trying to access MenuItem, whose ID is not known at design time.I am assuming that it requires "Text" field value.After page initialization, on some other event DataBind() did worked as suggested by you. But, on page load, it is not working.I will share the code behind page once I get back to office.
Vijay Balkawade