views:

40

answers:

3

Say I have a dynamically generated LinkButton in my ascx.cs code-behind. How could I go about "printing" this control to my page? Obviously I can't do something like just print the Text property as I need the button to retain its hyperlink. I'm guessing I want to use the WebControl.Render method, but I'm not familiar with it at all and haven't been able to find a good example of its use.

+1  A: 

You could provide a label at the right location in the page: <asp:Label id="myLinkButtonPlace" runat="server"></asp:Label>, and in code, you could add the linkbutton to the controlcollection of the label: this.myLinkButtonPlace.Controls.Add(aLinkButton);

Joachim VR
I can't access myLinkButtonPlace from the code. When I try to do myLinkButtonPlace.Controls.Add(button) it doesn't recognize myLinkButtonPlace.
Tyler
Not even if you try to build it? Sometimes the VS develope time compiler runs a little behind on the markup of asp.net.
Joachim VR
Yeah, it's got me confused.
Tyler
+1  A: 

Following on from @Joachim VR, there are many other asp.net controls with which you can add a dynamically created control to.

<asp:Label id="Label1" runat="server" />
<asp:PlaceHolder id="Placeholder1" runat="server" />
<asp:Panel id="Panel1" runat="server" />   

The above would render HTML different. So the Label would render as a <span id="Label1"><a></a></span>
Panel as <div id="Placeholder1"><a></a></div>
The Placeholder would simply render as the <a></a>

Tim B James
+2  A: 

This article http://www.tomot.de/article/3/asp.net/create-a-control-in-the-codebehind-and-retrieve-its-rendered-output should explain the basics of what you are looking for.

citronas
accepted answer but no upvote? was the article not useful?
citronas