Adding Controls to a page via external code

Discussion in 'ASP .Net' started by jwwishart@gmail.com, Mar 28, 2007.

  1. Guest

    The following class implements an interface which is in an assembly in
    the GAC
    The assembly that this class is in is loaded dynamically upon session
    start into the Current AppDomain. It is stored in a folder within the
    web application. (Not in the bin directory!)

    My Problems are these...

    1. I can load the User control dynamically but it always adds it to
    the end of the render HTML (After the ending </html> tag. (Which is
    not good :) )
    2. The label is also add after the </html> closing tag
    3. The last button doesn't even get added

    I'm putting some of the controls as you see into a placehold.

    Why are these things so... Does anyone have any idea's ??

    Regards,

    Justin

    ------

    Public Class Plugin
    Inherits MarshalByRefObject
    Implements IPlugin

    Public Function ProcessPage(ByVal page As System.Web.UI.Page) As
    Boolean
    Implements JuratPluginLibraryTEST.IPlugin.ProcessPage

    ' Get all the rows in the Text table of the database
    Dim data As DataTable = ExecuteSQLWithDataset("SELECT *
    FROM Strings").Tables(0)

    ' Add the Security Control
    Dim securityControl As Object
    securityControl = page.LoadControl("~/Plugins/MyPlugin/
    Security.ascx")
    securityControl.ID = "Security1"

    ' Create the new control
    Dim lab As New Label()

    Dim str As New StringBuilder()

    For Each row As DataRow In data.Rows
    str.Append(row.Item(0).ToString()).Append("<br />")
    Next

    lab.Text = str.ToString()
    lab.Font.Name = "Verdana"
    lab.ForeColor = System.Drawing.Color.Green
    lab.Font.Size = FontUnit.Medium

    ' Add Controls

    page.FindControl("placehold").Controls.Add(securityControl)
    page.FindControl("placehold").Controls.Add(lab)

    ' Modify a UserControl - Change text via a property
    Dim securityUserControl As Object
    securityUserControl = page.FindControl("Security1")
    securityUserControl.TextValue = "Ay Carumba"

    ' Add a button
    Dim btn As New Button()
    btn.Text = "Pressing me does nothing"

    page.Controls.Add(btn)
    End Function
    End Class
     
    , Mar 28, 2007
    #1
    1. Advertising

  2. On Mar 28, 9:25 am, wrote:
    > The following class implements an interface which is in an assembly in
    > the GAC
    > The assembly that this class is in is loaded dynamically upon session
    > start into the Current AppDomain. It is stored in a folder within the
    > web application. (Not in the bin directory!)
    >
    > My Problems are these...
    >
    > 1. I can load the User control dynamically but it always adds it to
    > the end of the render HTML (After the ending </html> tag. (Which is
    > not good :) )
    > 2. The label is also add after the </html> closing tag
    > 3. The last button doesn't even get added


    What you want to do is add some sort of Literal control or PlaceHolder
    control and inert your UserControl into the Controls Collection of
    that specific PlaceHolder...
    Not the "Page.Controls.Add(myStuff)" but rather the
    "myPlaceHolder.Controls.Add(myControl)"...!!


    Thomas

    --
    http://ajaxwidgets.com
    ASP.NET 2.0 Ajax Widgets
     
    Thomas Hansen, Mar 28, 2007
    #2
    1. Advertising

  3. Guest

    This is the ASPX.

    <body>
    <form id="form1" runat="server">
    <asp:placeHolder ID="placehold" runat="server"></asp:placeHolder>
    </form>
    </body>

    Please note that i am not adding the UserControl or the label to
    page.Controls. i am only adding the button to that collection and even
    if i add the button to the placehold it still doesnt render.

    So basically
    1. The button doesn't show regardless
    2. The UserControl and the label are rendering after the </html>
    closing tag.

    I seem to be getting the impression that you think that i can directly
    access the placehold ?!?! i can not possibly directly access the
    placehold as i am not in the page. I am in a completely seperate
    assembly!

    Thanks for your reply... Any further suggestions would be appreciated.

    Justin
     
    , Mar 29, 2007
    #3
  4. Guest

    I've managed to get this method working.

    For anyone else who come accross the issue of adding controls to the
    page from a different assembly (by passing a reference to the page
    into a method in the assembly. It seems that controls dont communicate
    very well across assemblies or AppDomains (Not sure which is the
    actual problem or both)

    To solve the problem i used had the assembly pass a Type object of the
    type that had the page processing method in it and the instanciated it
    and called the method! This worked fine and my controls rendered in
    the Form tag!

    Basically (I think) that all i am doing is bringing the object into
    existance inside the web app where before i was passing the Page to an
    assembly!

    It is not quite as clean as i would have liked but it works fine!
     
    , Mar 29, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy Ames

    Adding controls to Page.Controls collection

    Jeremy Ames, Mar 4, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    1,447
    Peter Blum
    Mar 5, 2004
  2. RN
    Replies:
    1
    Views:
    422
  3. Scott Allen
    Replies:
    8
    Views:
    10,949
    Scott Allen
    May 2, 2004
  4. Replies:
    1
    Views:
    146
    Van den Driessche Willy
    Sep 20, 2006
  5. Mellow Crow
    Replies:
    6
    Views:
    420
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page