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.

4 Comments

  • 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.

  • Worked out
    MenuMenu.SkipLinkText has to equal String.Empty.
    Not "fake.gif"

    But thanks for the article. Helped a lot.

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

    on this line:

    imgLogo.ToolTip = " "+ uc1.RenderControl(o) +" " + .......

    Any Help?

    Thanks

  • 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';

Comments have been disabled for this content.