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

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

am 08.04.2008 08:59:19 von naijacoder

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_getHandSetsByPh oneCode(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.Navi gateUrl = "#";
masterItem.ChildItems.Add(childrenItem);



}

}

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

am 08.04.2008 17:29:12 von dlongnecker

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_getHandSetsByPh oneCode(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.Navi gateUrl =
> "#"; masterItem.ChildItems.Add(childrenItem);
>
> }
>
> }
>

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

am 08.04.2008 23:39:50 von naijacoder

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" wrote in message
news:a0e9b9da35b708ca677a85deda43@msnews.microsoft.com...
> 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_getHandSetsByPh oneCode(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.Navi gateUrl =
>> "#"; masterItem.ChildItems.Add(childrenItem);
>>
>> }
>>
>> }
>>
>
>

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

am 09.04.2008 15:20:54 von dlongnecker

Thanks for the feedback.

As far as the data source goes, you can use any method to fetch your data
as long as it's enumerable for your foreach statement--even if you build
it by hand out of a DataTable and rows. :) The catch is being sure to count
your MenuItem's ChildItems AFTER you've added the child items. :)

Good luck!

-dl

--
David R. Longnecker
http://blog.tiredstudent.com

> 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" wrote in message
> news:a0e9b9da35b708ca677a85deda43@msnews.microsoft.com...
>
>> 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_getHandSetsByPh oneCode(
>>> 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.Navi gateUrl
>>> = "#"; masterItem.ChildItems.Add(childrenItem);
>>>
>>> }
>>>
>>> }
>>>