This is a migrated thread and some comments may be shown as answers.

RadPanelBar items count not rendering

1 Answer 82 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Satyajeet
Top achievements
Rank 1
Satyajeet asked on 16 Jan 2012, 08:51 PM
Hi,

1. I am trying to get the count of the Radpanelbar items. But i am getting Null reference exception sometimes as attached in the screenshot 1.
2. The same issue occurs when i am trying to fetch HtmlListItems. Please see attached scrrenshot 2.

The same code gives the exact Count/fetches HtmlListItems at times  and some times  gives a null reference exception. I am unable to understand this wierd behaviour.
Could you please help.

Thanks,
Satya

1 Answer, 1 is accepted

Sort by
0
Anthony
Telerik team
answered on 17 Jan 2012, 12:33 AM
Hello Satya,

The issue comes when you use a number in the FOR statement that is larger than number of list items.

To avoid this, you can get the count of list items first, then ensure the index number does not go higher than that. Take the following HTML as an example:

<div id="lister">
  <ul>
    <li>One
    <br/>
    <a href="http://bing.com">Bing</a>
    </li>
    <li>Two</li>
    <li>Three</li>
  </ul>
</div>

and use the following code:

HtmlDiv d = Find.ById<HtmlDiv>("lister");
HtmlUnorderedList list = d.Find.ByTagIndex<HtmlUnorderedList>("ul", 0);
 
int count = list.ChildNodes.Count;
Log.WriteLine(count.ToString());
 
for (int i = 0; i < count; i++)
{
    HtmlListItem li = d.Find.ByTagIndex<HtmlListItem>("li", i);
    Log.WriteLine(li.TextContent);
}


Greetings,

Anthony
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
General Discussions
Asked by
Satyajeet
Top achievements
Rank 1
Answers by
Anthony
Telerik team
Share this question
or