I've got a dynamic navigation, when they user clicks on a different page, that tab needs to become the active tab. Currently I'm getting NullException error and an Object error.
FYI: That data is coming back fine, it's only when I try to change the tab to active it's not finding the ID.
You cannot add a server control by setting the Text property of a Literal control, even if you specify the runat=server attribute on the elements you are adding. The FindControl method searches for a control by its server side id, but the li element you
are adding is not a server control and because of this the method cannot find it.
You should define your li element as a HtmlGenericControl and add it to a UL, or any other control defined in the markup, with the runat=server attribute. Then you will be able to search for it in your server side code.
C#:
HtmlGenericControl li = new HtmlGenericControl("li");
li.ID="IdOfLi";
li.InnerText="text";
....
myul.Controls.Add(li);
HtmlGenericControl c = (HtmlGenericControl)Page.Master.FindControl("IdOfLi");
Yes, but it doesn't make any difference as you cannot add a server control to a Literal. It is used to display text/HTML on a page. Instead, you should add a HtmlGenericControl to a server side (with runat=server) UL if you want to able to find it with the
FindControl method.
If your referring to the html mark up, I can't show you because it's dynamic, but below is what it looks like in the view source when it's render. Basically based on the what page the user is on, I'm change the display value, which is why I've got an active
and inactive li.
You won't be able to find the li:s with id tab_* in server side code as they are NOT server controls, it's just simple text which happens to have a textual runat=server attribute which means nothing in this context.
You never see the runat=server attribute in the rendered HTML for a server control.
For example, if you write this in the markup for an ASPX page:
<ul runat="server">
<li>llll</li>
</ul>
...it will render without the runat="server" in the browser:
<ul>
<li>llll</li>
</ul>
webguy07
Participant
952 Points
234 Posts
Object Error with HtmlGenericControl
Apr 16, 2012 09:32 PM|LINK
I've got a dynamic navigation, when they user clicks on a different page, that tab needs to become the active tab. Currently I'm getting NullException error and an Object error. FYI: That data is coming back fine, it's only when I try to change the tab to active it's not finding the ID.
******* Navigation Control *******
****** Navigation Code ********
List<Navigation> theNavigation = DataHelper.GetNavigation(); theNavigation = theNavigation.OrderBy(u => u.OrderBy).ToList(); foreach (Navigation item in theNavigation) { ltl_Navigation.Text += "<li ID=" + '"' + item.TabName + '"' + " style=" + '"' + "display:" + item.Display + '"' + " class=" + '"' + item.Class + '"' + " runat=" + '"' + "server" + '"' + "><span><span><a href=" + '"' + item.Location + '"' + ">" + item.Title + "</a></span></span></li>"; }******* Default Page ***** "setting the active tab"
HtmlGenericControl li_active = (HtmlGenericControl)Page.Master.FindControl("tab_homeactive"); HtmlGenericControl li_inactive = (HtmlGenericControl)Page.Master.FindControl("tab_homeinactive"); li_active.Style["display"] = string.Empty; "This is where it's erroring out!!" li_inactive.Style["display"] = "none";mm10
Contributor
6455 Points
1187 Posts
Re: Object Error with HtmlGenericControl
Apr 17, 2012 06:35 AM|LINK
You cannot add a server control by setting the Text property of a Literal control, even if you specify the runat=server attribute on the elements you are adding. The FindControl method searches for a control by its server side id, but the li element you are adding is not a server control and because of this the method cannot find it.
You should define your li element as a HtmlGenericControl and add it to a UL, or any other control defined in the markup, with the runat=server attribute. Then you will be able to search for it in your server side code.
C#:
HtmlGenericControl li = new HtmlGenericControl("li");
li.ID="IdOfLi";
li.InnerText="text";
....
myul.Controls.Add(li);
HtmlGenericControl c = (HtmlGenericControl)Page.Master.FindControl("IdOfLi");
ASPX:
<ul id="myul" runat="server></ul>
webguy07
Participant
952 Points
234 Posts
Re: Object Error with HtmlGenericControl
Apr 17, 2012 07:18 PM|LINK
Thanks. But I do have a runat server in the tag.
Catherine Sh...
All-Star
23382 Points
2490 Posts
Microsoft
Re: Object Error with HtmlGenericControl
Apr 18, 2012 07:09 AM|LINK
Hi webguy07,
Could you show the corresponding code ?
Best wishes,
Feedback to us
Develop and promote your apps in Windows Store
mm10
Contributor
6455 Points
1187 Posts
Re: Object Error with HtmlGenericControl
Apr 18, 2012 08:18 AM|LINK
Yes, but it doesn't make any difference as you cannot add a server control to a Literal. It is used to display text/HTML on a page. Instead, you should add a HtmlGenericControl to a server side (with runat=server) UL if you want to able to find it with the FindControl method.
webguy07
Participant
952 Points
234 Posts
Re: Object Error with HtmlGenericControl
Apr 18, 2012 01:43 PM|LINK
If your referring to the html mark up, I can't show you because it's dynamic, but below is what it looks like in the view source when it's render. Basically based on the what page the user is on, I'm change the display value, which is why I've got an active and inactive li.
<ul id="top-navigation"> <li id="tab_homeinactive" runat="server"><span><span><a href="Default.aspx">Home</a></span></span></li> <li id="tab_homeactive" style="display: none;" class="active" runat="server"><span><span>Home</span></span></li> <li id="tab_eventinactive" runat="server"><span><span><a href="Default.aspx">Events</a></span></span></li> <li id="tab_eventactive" style="display: none;" class="active" runat="server"><span><span>Events</span></span></li> <li id="tab_usersinactive" runat="server"><span><span><a href="Users.aspx">Users</a></span></span></li> <li id="tab_usersactive" style="display: none;" class="active" runat="server"><span><span>Users</span></span></li> <li id="tab_reportinginactive" runat="server"><span><span><a href="Reporting.aspx">Reporting</a></span></span></li> <li id="tab_reportingactive" style="display: none;" class="active" runat="server"><span><span>Reporting</span></span></li> <li id="tab_helpinactive" runat="server"><span><span><a href="Help.aspx">Help</a></span></span></li> <li id="tab_helpactive" style="display: none;" class="active" runat="server"><span><span>Help</span></span></li> </ul>mm10
Contributor
6455 Points
1187 Posts
Re: Object Error with HtmlGenericControl
Apr 18, 2012 02:03 PM|LINK
You won't be able to find the li:s with id tab_* in server side code as they are NOT server controls, it's just simple text which happens to have a textual runat=server attribute which means nothing in this context.
You never see the runat=server attribute in the rendered HTML for a server control.
For example, if you write this in the markup for an ASPX page:
<ul runat="server">
<li>llll</li>
</ul>
...it will render without the runat="server" in the browser:
<ul>
<li>llll</li>
</ul>
webguy07
Participant
952 Points
234 Posts
Re: Object Error with HtmlGenericControl
Apr 18, 2012 02:13 PM|LINK
Ok. Then how do I get the id of the li to change the display value at run time?
-Thanks
mm10
Contributor
6455 Points
1187 Posts
Re: Object Error with HtmlGenericControl
Apr 18, 2012 02:38 PM|LINK
Change your navigation code by creating HtmlGenericControls instead of adding text to a Literal:
List<Navigation> theNavigation = DataHelper.GetNavigation();
theNavigation = theNavigation.OrderBy(u => u.OrderBy).ToList();
foreach (Navigation item in theNavigation)
{
HtmlGenericControl li = new HtmlGenericControl("li");
li.ID = item.TabName;
li.Style.Add("display", item.Display);
li.InnerText="><span><a href.......";
......
yourUl.Controls.Add(li);
}
..and define a UL in your markup to hold the HtmlGenericControls you are creating in code:
<ul runat="server" id="yourUl">
</ul>
webguy07
Participant
952 Points
234 Posts
Re: Object Error with HtmlGenericControl
Apr 18, 2012 03:23 PM|LINK
Ok. I'm not getting an error anymore, but now nothing is showing. Do I need to add the ul top-navigation to a panel or something after the foreach?
-Thanks