473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Adding Controls to a page via external code

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 </htmltag. (Which is
not good :) )
2. The label is also add after the </htmlclosing 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

Mar 28 '07 #1
3 1646
On Mar 28, 9:25 am, jwwish...@gmail.com 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 </htmltag. (Which is
not good :) )
2. The label is also add after the </htmlclosing 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

Mar 28 '07 #2
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 28 '07 #3
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 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
5
by: Jeremy Ames | last post by:
I have run into a major road block with my page development projects. I am trying to add a literal control to my asp.net page and I got an error stating that the control could not be added because...
3
by: Ankit Aneja | last post by:
I have a strange situation and I have no idea how to solve this. Its a Recruitment Search Page,in the Admin Page, for every button click event the Admin Person has to create a checkbox on the users...
3
by: Mr Newbie | last post by:
I am messing around with Web User Controls at present and (think) I have discovered the following. 1.) The identifier for the control in the code behind must match the ID for the control on the...
9
by: Neo Geshel | last post by:
I have strip-mined, strip-searched, and completely exhausted the Internet (up to the 30th page on Google, with 100 results per page!!), all without finding an answer to my question AS TO WHY IT...
3
by: Mark Denardo | last post by:
I'm trying to dynamically create and add controls to a web page: Label obj1 = new Label(); DropDownList obj2 = new DropDownList(); Controls.Add(obj1); Controls.Add(obj2); But I get the...
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
3
by: Andy B | last post by:
I have a web form that has controls to be modified on it. The class that will modify the controls is not contained inside the page code. How would I best pass the controls to the external class?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.