asp:menu question - How to get a count of a childItem?

R

rote

I'm looping through a dataset below and adding some rows to my menuitems.
But what i want to achieve on this line

masterItem.Text = parentItem["Description"].ToString() + "[" +
masterItem.ChildItems.Count.ToString();+ "]";

is to get a count of the ChildRow and append it to the Text property below

For example the output should be:


--Nokia[68]
--Motorola[50]

But when i use masterItem.ChildItems.Count.ToString(); it just returns 0

Is this possible?

Code below
----------------------

DataSet ds =
getMasterDetail_Handset.getAllScheduleMaster_getHandSetsByPhoneCode(PhoneCode);


int hsRowCounts = ds.Tables["HandSets"].Rows.Count;if (hsRowCounts > 0)
{

Menu menu = new Menu();


foreach (DataRow parentItem in ds.Tables["MasterDetails"].Rows)
{

//MenuItem categoryItem = new MenuItem((string)parentItem["Master Code"]);

MenuItem masterItem = new MenuItem();
//categoryItem.ChildItems.Count.ToString();

masterItem.NavigateUrl = "#";
//masterItem.ChildItems.Count.ToString().ToString();

masterItem.Text = parentItem["Description"].ToString() + "[" +
parentItem["Dealer"].ToString() + "]";


menu.Items.Add(masterItem);



foreach (DataRow childItem in parentItem.GetChildRows("Children"))
{

MenuItem childrenItem = new
MenuItem((string)childItem["Description"]);childrenItem.NavigateUrl = "#";
masterItem.ChildItems.Add(childrenItem);



}

}
 
P

Patrick.O.Ige

David thanks for the prompt reply.
I believe you've used linq here in the sample snippet you sent to me
foreach (Product product in db.Products.Where(x => x.CategoryID
== c.CategoryID))
I did my relationship in the dataset :(
I will try your advice and see how it goes..

By the way i really enjoyed this below from your site..
http://www.tiredstudent.com/blog/files/AJAXingInDotNet.pdf

thumbs up David


David R. Longnecker said:
Your code is correct, but a bit out of order. You can't count the child
items until you add them. Here's an example I used based on your code
(but against the Microsoft Northwind database). It loops through the
categories (the MasterItems) and adds each product based on category as
child menu items, then adds the count before it adds into the final Menu
object.
Your data logic will be a bit different, but the sequencing of creating
the menu, creating the masterItem, adding the childrenItem(s), then
setting the MasterItem's text should be the same.

NorthwindDataContext db = new NorthwindDataContext();
Menu menu = new Menu();

foreach (Category c in db.Categories.ToList())
{
MenuItem masterItem = new MenuItem();
masterItem.NavigateUrl = "#";
// Loop through the Products table matching CategoryID to
the product's CategoryID.
foreach (Product product in db.Products.Where(x =>
x.CategoryID == c.CategoryID))
{
MenuItem childrenItem = new
MenuItem(product.ProductName);
childrenItem.NavigateUrl = "#";
masterItem.ChildItems.Add(childrenItem);
}

// After the childItems are added, then count them.
masterItem.Text = string.Format("{0} [{1}]",
c.CategoryName, masterItem.ChildItems.Count.ToString());

menu.Items.Add(masterItem);
}

//form1.Controls.Add(menu);

HTH.

-dl

--
David R. Longnecker
http://blog.tiredstudent.com
I'm looping through a dataset below and adding some rows to my
menuitems. But what i want to achieve on this line

masterItem.Text = parentItem["Description"].ToString() + "[" +
masterItem.ChildItems.Count.ToString();+ "]";

is to get a count of the ChildRow and append it to the Text property
below

For example the output should be:

--Nokia[68]
--Motorola[50]
But when i use masterItem.ChildItems.Count.ToString(); it just returns
0

Is this possible?

Code below
----------------------
DataSet ds =
getMasterDetail_Handset.getAllScheduleMaster_getHandSetsByPhoneCode(Ph
oneCode);

int hsRowCounts = ds.Tables["HandSets"].Rows.Count;if (hsRowCounts >
0) {

Menu menu = new Menu();

foreach (DataRow parentItem in ds.Tables["MasterDetails"].Rows) {

//MenuItem categoryItem = new MenuItem((string)parentItem["Master
Code"]);

MenuItem masterItem = new MenuItem();
//categoryItem.ChildItems.Count.ToString();
masterItem.NavigateUrl = "#";
//masterItem.ChildItems.Count.ToString().ToString();
masterItem.Text = parentItem["Description"].ToString() + "[" +
parentItem["Dealer"].ToString() + "]";

menu.Items.Add(masterItem);

foreach (DataRow childItem in parentItem.GetChildRows("Children")) {

MenuItem childrenItem = new
MenuItem((string)childItem["Description"]);childrenItem.NavigateUrl =
"#"; masterItem.ChildItems.Add(childrenItem);

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top