views:

189

answers:

4

I have a custom control that does some work for me on async postbacks. Normally, I'd call the control directly from the presentation side using the following code on the ASPX page:

<mytag:CustomControl runat="server">
    html (or other text) goes here
</mytag:CustomControl>

However, in my current application, I need to dymanically create the control from the codebehind, using code similar to the following:

Dim myControl As myClass.CustomControl = New myClass.CustomControl
myControl.ID = "someID"
myControl.?????? = "html (or other text) goes here"
Me.Controls.Add(myControl)

When adding the control to the page dynamically, how do I add info that would normally be between the start and end tags if the control were added the normal, non-dynamic way?

Thanks

Here's the actual control:

Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
    Dim scriptmanagerPage As ScriptManager = ScriptManager.GetCurrent(Page)
    If scriptmanagerPage Is Nothing Then
        'Do nothing
    Else
        'See if we are in a postback
        If scriptmanagerPage.IsInAsyncPostBack Then
            'We are in a postback; register the script
            Dim stringbuilderWorking As New StringBuilder()
            MyBase.Render(New HtmlTextWriter(New StringWriter(stringbuilderWorking)))
            Dim stringScript As String = stringbuilderWorking.ToString()
            ScriptManager.RegisterStartupScript(Me, GetType(ScanWorkXAJAX), UniqueID, stringScript, False)
        Else
            'Not in a postback
            MyBase.Render(writer)
        End If 'In an async postback
    End If 'Scriptmanager present

End Sub
+2  A: 

What do you mean by data? More controls?

You can use

  myControl.Controls.Add(childControlHere);

EDIT After question was clarified:

Add a literal control. i.e.

myControl.Controls.Add(new LiteralControl("<b>hello world</b><script type='text/javascript'>alert('hi');</script>"));
zincorp
I've revised the question to be a bit more clear. I don't want to add controls to my control. I want to add text/html/script data that would normally appear between the start and end tag.
Brad
A: 

Do you mean FIND and append data to controls inside your dynamic control?

You can use the WebControl.FindControl method to find your control embedded in your custom control and then you can add data via its properties. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.findcontrol.aspx

Kevin Goff
A: 

That depends on the properties of the custom control you are using. You may have to bind to a template to display so that events will be fired, it really just depends on your specific control.

Kevin Goff
Fair enough. Custom control class is posted now.
Brad
A: 

Was able to accomplish this using a literal control.

Code above plus:

Dim myLiteral As LiteralControl = New LiteralControl
myLiteral.ID = "myLiteral"
myLiteral.Text = "html (or some other text) goes here"
myControl.Controls.Add(myLiteral)
Brad