Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Adding Controls to a page via external code

Reply
Thread Tools

Adding Controls to a page via external code

 
 
jwwishart@gmail.com
Guest
Posts: n/a
 
      03-28-2007
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(securit yControl)
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

 
Reply With Quote
 
 
 
 
Thomas Hansen
Guest
Posts: n/a
 
      03-28-2007
On Mar 28, 9:25 am, (E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
jwwishart@gmail.com
Guest
Posts: n/a
 
      03-28-2007
This is the ASPX.

<body>
<form id="form1" runat="server">
<asplaceHolder ID="placehold" runat="server"></asplaceHolder>
</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

 
Reply With Quote
 
jwwishart@gmail.com
Guest
Posts: n/a
 
      03-29-2007
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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding WebParts via code behind? Bart Van Hemelen ASP .Net 6 01-19-2010 05:08 AM
Changing the Master Page via the code behind page GusSalvador General Computer Support 0 12-02-2009 03:10 PM
Adding UC via Code Larry R ASP .Net Web Controls 0 07-06-2007 08:49 PM
Template column problem when adding datagrid via code RN ASP .Net 1 02-18-2005 02:03 AM
Adding controls to Page.Controls collection Jeremy Ames ASP .Net 5 03-05-2004 08:33 PM



Advertisments