Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Simple menu control shows childitems count =0

This cant be that hard. I have an XML with 1 parent node and 3 childnodes. I bind this to a Menu Control. I want to set the URL of the code behind. I do this in *** OnMenuItemDataBound ***

**** THE COUNT Comes as zero*** why??

Protected Sub test(ByVal sender As Object, ByVal e As MenuEventArgs)
        Dim t As Integer = e.Item.ChildItems.Count
         If e.Item.Value = "jobseekers" Then
            Dim test As String = e.Item.ChildItems(0).Text
            e.Item.ChildItems(0).NavigateUrl = Page.ResolveUrl("~/" + ConfigurationManager.AppSettings("jobseekerfolder") + "/" + e.Item.ChildItems(0).NavigateUrl)
        End If
END

**** I see the menu on the screen with the child nodes. The XML is this:

<JobSeekers>
    <PostResume url="postresume.aspx"></PostResume>
    <SearchJobs url="jobsearch.aspx"></SearchJobs>
    <MyFavorites url="MyFavorites.aspx"></MyFavorites>
  </JobSeekers>
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

What is the parent element, and what are the child elements?

Bob
How are you binding the menu to the XML?

Bob
Avatar of Camillia

ASKER

That XML is it. Parent node is "jobseekers". Child Nodes is under it: postresume, searchjobs and myfavorites.

*********I have this:
<asp:Menu runat="server" ID="Menu1" OnMenuItemDataBound="test" StaticEnableDefaultPopOutImage="false"   Orientation="Horizontal" DataSourceID="XmlDataSource2">
  <DataBindings>
  <asp:MenuItemBinding DataMember="JobSeekers" Value="jobseekers" Text="Job Seekers" > </asp:MenuItemBinding >
 
  </DataBindings>

</asp:Menu>


********and I bind like this:
<asp:XmlDataSource ID="XmlDataSource2" Runat="server" DataFile="~/UserControls/sitemap.xml">
</asp:XmlDataSource>

******** and the XML is what I posted..

***** Maybe I need to do "<asp:MenuItemBinding> for the rest of the nodes??? *****
Usually a SiteMap.xml looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode url="~/Default.aspx" title="Home">
     <siteMapNode url="~/Books/Default.aspx" title="Books">
       <siteMapNode url="~/Books/Novels.aspx" title="Novels" />
       <siteMapNode url="~/Books/History.aspx" title="History" />
       <siteMapNode url="~/Books/Romance.aspx" title="Romance" />
     </siteMapNode>
     <siteMapNode url="~/Electronics/Default.aspx" title="Electronics" />
     <siteMapNode url="~/DVDs/Default.aspx" title="DVDs" />
     <siteMapNode url="~/Computers/Default.aspx" title="Computers" />
  </siteMapNode>
</siteMap>

And, you would bind to a SiteMapDataSource.

Bob
I didnt use a sitemap. I used an XML and binding to an XMLDatasource....i've seen example of this...

http://www.aspdotnetcodes.com/Asp.Net_Menu_Control_XmlDataSource.aspx

*** but what am I missing that count comes out as zero??

I tried this as well:

<asp:Menu runat="server" ID="Menu1" OnMenuItemDataBound="test" StaticEnableDefaultPopOutImage="false"   Orientation="Horizontal" DataSourceID="XmlDataSource2">
  <DataBindings>
  <asp:MenuItemBinding DataMember="JobSeekers" Value="jobseekers" Text="Job Seekers" > </asp:MenuItemBinding >
  <asp:MenuItemBinding DataMember="PostResume" Value="PostResume"  NavigateUrlField="url" Text="Post Resume123" > </asp:MenuItemBinding >
   <asp:MenuItemBinding DataMember="SearchJobs" Value="SearchJobs"  NavigateUrlField="url" Text="Search Jobs123" > </asp:MenuItemBinding >
   <asp:MenuItemBinding DataMember="MyFavorites" Value="MyFavorites"  NavigateUrlField="url" Text="MyFavorites123" > </asp:MenuItemBinding >
 
  </DataBindings>

</asp:Menu>
I don't see TextField and ValueField for the MenuItemBinding entries.  The example shows those:

<asp:MenuItemBinding DataMember="Menu" TextField="text" ValueField="text" NavigateUrlField="url" />

Bob
Geez, that didnt work either. I did this:
****Changed the XML to this:
  <Menu text="" url="">
    <SubMenu text="PostResume" url="postresume.aspx"></SubMenu>
    <SubMenu text="SearchJobs" url="jobsearch.aspx"></SubMenu>
    <SubMenu text="MyFavorites" url="MyFavorites.aspx"></SubMenu>

  </Menu>

*** Chagned Menu control to this:
<asp:Menu runat="server" ID="Menu1" OnMenuItemDataBound="test" StaticEnableDefaultPopOutImage="false"   Orientation="Horizontal" DataSourceID="XmlDataSource2">
  <DataBindings>
  <asp:MenuItemBinding DataMember="Menu"  />
  <asp:MenuItemBinding DataMember="SubMenu" NavigateUrlField="url" TextField="text"
                    ValueField="text" />
 </DataBindings>

</asp:Menu>

***Same XML datasource. Made sure the path is correct But still the count comes as zero which means...Dim test As String = e.Item.ChildItems(0).Text gives me a null value...

Really dont want to change this to Sitemap...if i dont find other examples , i will try the sitemap solution...

how could this be so hard??
You know, with this code:
If e.Item.Value = "Menu" Then
 End If

I set a debug and e.item.value comes out as the nodes of that XML.  Wondering if my code of "e.item.childitems(0).text" is incorrect and I should just look at e.item.value??
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<?xml version="1.0" encoding="utf-8" ?>
<Menu>
      <SubMenu text="PostResume" url="postresume.aspx"></SubMenu>
      <SubMenu text="SearchJobs" url="jobsearch.aspx"></SubMenu>
      <SubMenu text="MyFavorites" url="MyFavorites.aspx"></SubMenu>
</Menu>
Yeah, and then in the code behind i have:
If e.Item.Value = "PostResume" Then
 e.Item.NavigateUrl = Page.ResolveUrl("~/" + ConfigurationManager.AppSettings("jobseekerfolder") + "/" + e.Item.NavigateUrl)
End

and it gets the value correctly. I guess then "childitems(0)" is wrong in this scenario....

*** So, I'll look at EACH NODE like that...i guess no way to look at "<Menu>"s childnodes?
even tried this:

  Dim childItem As MenuItem
        For Each childItem In e.Item.ChildItems
            Dim test As String = e.Item.Text
        Next

but since childitems come as zero, that doesnt work.
Actually, what is that code supposed to accomplish?

Bob
i downloaded a starter-kit from www.asp.net. It has a treeview and I want to change the treeview to menu control because i want to position the control horizontally. Not like a treeview...

 So i removed the treeview and it's XML and added my own XML and menu control...

What I have now works but wondering why that childitems didnt work. Not a big deal ..this gets me going..