Dynamic rendering of menu Control with rendercontrol

A customer reported a issue with creating a Menu ASP.NET Server control included a custom control on DevTrain.de.  Every Server Control have method RenderControl which creates the HTML and store it in HTMLtextWriter Object. With that you can create eg HTML Mails which databound content.

In that case the goal is a custom control. On runtime there is a NullReferenceException on renderControl. With other controls like Button or Label this works fine. My first idea was that menu control must interact with the HTTPcontext for eg Rights. But after step into the exceptions I must say its a kind of bug of the menu control. You have to fill property's with values which are filled (i guess) automatically if the control is declerated.

Final solution is

Dim SB As New StringBuilder()

Dim SW As New StringWriter(SB)

Dim o As New HtmlTextWriter(SW)

Dim miMenu As MenuItem = Nothing

Dim menuMenu As System.Web.UI.WebControls.Menu = New System.Web.UI.WebControls.Menu

menuMenu.ID = "menuMenu"

MenuMenu.Orientation = Orientation.Horizontal

menuMenu.MaximumDynamicDisplayLevels = 8

menuMenu.Items.Add(New MenuItem("Home", "eins", "", "eins.aspx"))

miMenu = New MenuItem("Home", "home", "", "Default.aspx")

miMenu.ChildItems.Add(New MenuItem("Home2", "home2", "", "Default.aspx"))

menuMenu.Items.Add(miMenu)

MenuMenu.SkipLinkText = "fake.gif" 'Get_SpacerImageUrl

MenuMenu.StaticPopOutImageUrl = "fake.gif" 'get_PopoutImageUrlInternal

MenuMenu.ScrollUpImageUrl = "fake.gif" 'Get_ScrollUpImageUrlInternal

menuMenu.ScrollDownImageUrl = "fake.gif" 'Get_ScrollDownImageUrlInternal

menuMenu.RenderControl(o)

The graphic file fake.gif does not exist. Its necessary to have a fake value and "" is not possible. The comments shows the internal method shown by the exceptions.

Published Wednesday, May 02, 2007 10:45 AM by preishuber
Filed under:

Comments

# re: Dynamic rendering of menu Control with rendercontrol

Tuesday, July 03, 2007 2:04 AM by TheProphet

Just Wondering was there anything you had to do to get this to work?

I copied your code and still can't get it working.

# re: Dynamic rendering of menu Control with rendercontrol

Tuesday, July 03, 2007 6:51 PM by TheProphet

Worked out

MenuMenu.SkipLinkText has to equal String.Empty.

Not "fake.gif"

But thanks for the article.  Helped a lot.

# re: Dynamic rendering of menu Control with rendercontrol

Thursday, October 25, 2007 11:05 AM by kamal

I get error:

Compiler Error Message: CS0019: Operator '+' cannot be applied to operands of type 'string' and 'void'

on this line:

imgLogo.ToolTip = "<table><tr> <td>"+ uc1.RenderControl(o) +"<h1> " + .......

Any Help?

Thanks

# re: Dynamic rendering of menu Control with rendercontrol

Monday, May 12, 2008 11:48 AM by usugarbage

From what I could tell w/ this error it came down to not supplying a url to the menuitem being added and setting 'SkipLinkText = string.Empty';

# re: Dynamic rendering of menu Control with rendercontrol

Tuesday, March 23, 2010 9:13 AM by Roderick

Good evening. We thought, because we had power, we had wisdom. Help me! Help to find sites on the: Razor electric scooter schematic. I found only this - <a href="www.governo.al.gov.br/.../cheap-new-razor-electric-scooter">cheap new razor electric scooter</a>. Razor electric scooter, this shake emerged that i would lose with the chrome recently partly to get office. If you guess a motorcycle gift for diet driveway, come not drum, razor electric scooter. :confused: Thanks in advance. Roderick from Yemen.

Leave a Comment

(required) 
(required) 
(optional)
(required)