Adding Controls to a page via external code

J

jwwishart

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
 
T

Thomas Hansen

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
 
J

jwwishart

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
 
J

jwwishart

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top