CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums

ASP.NET Web Hosting – 3 Months Free!
Free 3 Months



Zone: > NEWSGROUP > Asp.Net Forum > windows_hosting.hosting_open_forum Tags:
Item Type: NewsGroup Date Entered: 1/3/2005 1:11:17 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 22 Views: 43 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
23 Items, 2 Pages 1 2 |< << Go >> >|
RadAway
Asp.Net User
Load user controls dynamical, again1/3/2005 1:11:17 PM

0/0

This is my problem:
I got a webpage that gets all its contents from an XML. The XML is read with an XMLTextReader and the text is written to the page with an XMLTextWriter. In some places on the page I need some more advanced content than just text. At those places it would be perfect if I could load a user control (.ascx) with web controls and stuff to the page. All I want to do in the XML is to write the ascx filename in a tag and with some kind of magic get the contents of that ascx where the XMLTextWriter got its pointer.
Is this possible? If not, do you have any advice for me?
danscan
Asp.Net User
Re: Load user controls dynamical, again1/3/2005 7:56:16 PM

0/0

yes it is possible


UserControl uc = new UserControl();

string strPath; //Path to ascx control

uc = (UserControl)(LoadControl(strPath));


Fill in strPath from your xml data.

Then you just have to add the usercontrol to something. PlaceHolder.Controls.Add(uc);

Good Luck
Dan
The difference between genius and madness is success. And I haven't been too successful.
shanthanu
Asp.Net User
Re: Load user controls dynamical, again1/3/2005 9:53:07 PM

0/0

Hi Rad,

Make sure that you assign a "strong type" to the usercontrol in the .ascx file using the following directive:

--------------------------------------------
In the RadsXMLReadWriter.ascx file
--------------------------------------------
<%@ Control ClassName="RadsXMLReadWriter" %>


And in the host aspx file, we register the control with the following directive

<%@ Register TagPrefix="RadAway" TagName="Rad" Src="RadsXMLReadWriter.ascx" %>

And the code to Load the control would be:

Control RadX=LoadControl("RadsXMLReadWriter.ascx");

Now we gotta use the "Strong Type" to work with the controls Methods and Properties:


((RadsXMLReadWriter).RadX).<<Property_1>>=<<Property_Value1>>;


Hope this helps.





Cheers,

Shanthanu

[email protected]
AndrewSeven
Asp.Net User
Re: Load user controls dynamical, again1/4/2005 12:11:30 AM

0/0

Why do you say that you need to specify the class name?

MyUserControl myUC = (MyUserControl)LoadControl("MyUserControl.ascx");


myUC is an instance of the ascx file.
RadAway
Asp.Net User
Re: Load user controls dynamical, again1/4/2005 10:40:03 AM

0/0

I thank you for your help guys!
I forgot to mention one thing before though, I'm doing this in a control in a dll.
I tried the tip I got from danscan but I got this error:

Controls.cs(191,24): error CS0103: The name 'LoadControl' does not exist in the
class or namespace 'MyControls.BuildGroup'

Where MyControls is my own namespace and BuildGroup is the class I?m writing. How do I fix this?

Another thing is that since I got this in a dll I don't want it to depend on web controls in the Default.aspx, actually I?m not even sure how I address a web control from one of my own controls. Can I create a PlaceHolder dynamical in my code?
Or could I even do something like this:

this.Controls.Add(uc);

Thanks in advance guys!
/Logan
shanthanu
Asp.Net User
Re: Load user controls dynamical, again1/5/2005 4:29:56 PM

0/0

Yep. But the difference between our methods is that you are directly creating an instance of the .ascx control ,while i am creating a generic "Control" object and then casting it to a strong type.

Although i have not tried the method you suggested, i feel that if i assign a class name to the .ascx page, it is more elegant to work with a class representation of the .ascx file rather than work with an instance of a ".ascx" page.



Cheers,

Shanthanu

[email protected]
danscan
Asp.Net User
Re: Load user controls dynamical, again1/5/2005 8:14:47 PM

0/0

Controls.cs(191,24): error CS0103: The name 'LoadControl' does not exist in the
class or namespace 'MyControls.BuildGroup'

You just have to add "using System;" or where ever the LoadControl class is located.

You can can create a PlaceHolder Dynamicly in code but at sometime you have to add the control to sometype of existing control. For instance Page


PlaceHolder ph = new PlaceHolder();
LinkButton lnk = new LinkButton();
lnk.text = "something";
ph.controls.add(lnk);
Page.controls.add(ph);


..ascx is a UserControl while .cs is a class that I assume is a webcontrol.

A webcontrol at some point must be added to hierarchy of Page.

Although I have never added .ascx usercontrols to a custom made webcontrol I assume it
will work.

Now if you actually loading webcontrols (TextBox, LinkButton, Label etc) dynamicly then that is a different story (sort of).

I prefer to use a select case statement with Generc WebControl declared before the select so that the scope for the Generic WebControl is constistant.

Then with in the select statement I just do a case like so

Before the select add WebControl con = new WebControl();

case "LinkButton":
LinkButton lnk = new LinkButton();
lnk.text = something from data;
con = (LinkButton)lnk;
break;

Then at the end of the select statement I just add
con to another control on the page or usercontrol.

Wa-la dynamic webcontrols added to the page.




The difference between genius and madness is success. And I haven't been too successful.
RadAway
Asp.Net User
Re: Load user controls dynamical, again1/6/2005 10:37:06 AM

0/0

Thanks again for the help.
But this time I also got problems. :(

I checked in the SDK documentation where the LoadControl is located. It turned out it's located in: TemplateControl.LoadControl
So I changed my previous code to:

writer.Write(sAdvanced);

UserControl uc = new UserControl();
uc = (UserControl)(TemplateControl.LoadControl(sAdvanced));


But now I got another error message:
Controls.cs(191,24): error CS0120: An object reference is required for the
nonstatic field, method, or property
'System.Web.UI.TemplateControl.LoadControl(string)'

I didn't know what this meant so I checked it out on MSDN, but I just got more confused...

The next problem I encountered was when I tried this:

WebControl con = new WebControl();

LinkButton lnk = new LinkButton();
lnk.Text = "Some text";
con = (LinkButton)lnk;


Now I got this error:
Controls.cs(200,22): error CS0122:
'System.Web.UI.WebControls.WebControl.WebControl()' is inaccessible due
to its protection level

I checked this out on MSDN too, and I think I understand what it means, but how to fix it; no idea.

I?m kind of inexperienced with those things so it would be really great if I could get some help with this too.
shanthanu
Asp.Net User
Re: Load user controls dynamical, again1/6/2005 6:24:25 PM

0/0

Hi again Rad,

About the first error:

Controls.cs(191,24): error CS0120: An object reference is required for the
nonstatic field, method, or property
'System.Web.UI.TemplateControl.LoadControl(string)'


Are you sure that you are passing the correct argument to the LoadControl Method?




Cheers,

Shanthanu

[email protected]
RadAway
Asp.Net User
Re: Load user controls dynamical, again1/6/2005 8:10:10 PM

0/0


writer.Write(sAdvanced);
UserControl uc = new UserControl();
uc = (UserControl)(TemplateControl.LoadControl(sAdvanced));

LoadControl wants a string and I'll get it one; sAdvanced is a string. So I guess that's not a problem.
But thanks anyway...
RadAway
Asp.Net User
Re: Load user controls dynamical, again1/9/2005 9:32:59 PM

0/0

So there is no solution for this problem then?
All I want to do is to add a User Control in my custom made Web Control just given the filename of the User Control... Anyone, please?
AndrewSeven
Asp.Net User
Re: Load user controls dynamical, again1/11/2005 2:02:15 AM

0/0

UserControl is not intended for Library Usage, it is for ascx files.
You can add logic to a custom class so that your ascx files are "smarter", but you will almost always need the ascx.

You should look into server controls and templated controls.

RadAway
Asp.Net User
Re: Load user controls dynamical, again1/11/2005 10:03:04 PM

0/0

Thanks for showing interest in my thread! :)
But I don't quite understand your post here, perhaps you misunderstood the problem or maybe did I misunderstand you.
I'll try to express myself again just in case.

I'm having all those ascx-files full with html and webcontrols, like labels and stuff.
And then I have made this custom control which I'm compiling into a dll-file. In this custom control I want to load one of the ascx-files just given the name of it into the page where the custom control is positioned, but I don't get it to work at all!
I have done this before in codebehind of a page (aspx) with LoadControl, and I want it to work the same way in my custom control within my dll-file.
I mean it have to work somehow! Or is LoadControl useless this time??

Hope that cleared something up if that was needed. And if this doesn't work at all or you just think I'm stupid I would be happy if you could explain to me why it is like that :).
AndrewSeven
Asp.Net User
Re: Load user controls dynamical, again1/12/2005 1:51:55 AM

0/0

LoadControl is a method on the page object so you need a ref to the current page.

Try using .Handler

Page currentPage = (Page)HttpContext.Current.Handler;

currentPage.LoadControl(...)

Note that I've never tried this
RadAway
Asp.Net User
Re: Load user controls dynamical, again1/12/2005 10:38:39 AM

0/0

That was a great idea! It fixed the LoadControl problem and now it seam that I've got the User Control loaded alright.
But another problem occurred; guess it's not that rare in those situations. Well it seam like I got the User Control loaded and now I want to add it to the page. As suggested before in this thread I wanted to add it to a PlaceHolder and then add that to the page. I first tried to just add the PlaceHolder just like this.

PlaceHolder ph = new PlaceHolder();
currentPage.Controls.Add(ph);

The second line generates the following run-time error:
"The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). "
I guess I know what this means but I want to go around it somehow, is there a way?
Maybe I can do something with the PlaceHolder or do I even have to use a one at all to add my already loaded User Control?

Thanks for all help this far!
jbrinkman
Asp.Net User
Re: Load user controls dynamical, again1/12/2005 11:38:03 AM

0/0

You should remove the reference to placeholder and just add the control directly to the page. Placeholder does not provide any value when dynamically injecting content into the page and just slows down page rendering since it results in a bloated control tree.
Joe Brinkman
andrewseven
Asp.Net User
Re: Load user controls dynamical, again1/12/2005 1:57:42 PM

0/0

I agree about the placeholder, but it might be usefull , for the code blocks error, to put a placeholder in the actual markup and add the dynamic controls to the placeholder.
RadAway
Asp.Net User
Re: Load user controls dynamical, again1/12/2005 4:46:39 PM

0/0

I still don't get this to work... :(
I've tried a lot of things and it's kind of hard to find information on the Internet about this.
I just realized I get the error I described before whatever Control I try to Add. It would be nice if anyone could explain to me what it really means. When I searched the web for it I just came across database related problems. Here is my current code:

Page currentPage = (Page)HttpContext.Current.Handler;

UserControl uc = new UserControl();
uc = (UserControl)currentPage.LoadControl("test2.ascx");
currentPage.Controls.Add(uc);

And it generates the following run-time error:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Like I said I get this whatever Control I Add. Like the code in my last post for instance:

PlaceHolder ph = new PlaceHolder();
currentPage.Controls.Add(ph);

Generates the same thing.
What does it mean and why do I get this error?
AndrewSeven
Asp.Net User
Re: Load user controls dynamical, again1/14/2005 12:55:42 AM

0/0

It really does mean what it says. ;)

The Controls collection (of your Page) where you are tyring to add a control has some code blocks <%%> in it.


Try putting a placeholder in the html at the place where you want to add the control, and add youur control to the placeholder


currentPage.FindContol("thePlaceholderId").Controls.Add(uc);

RadAway
Asp.Net User
Re: Load user controls dynamical, again1/14/2005 12:38:36 PM

0/0

Well I?ll be damned! That actually worked! :D
Not exactly the way you suggested but close enough!
I'll write the final code below in a complete example so that others that read this thread find it as useful as possible.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MyControls {
public class LoadWriteUserControl : Control {
public string sUCFilename="test.ascx";
protected override void Render(HtmlTextWriter writer) {
Page currentPage = (Page)HttpContext.Current.Handler;
UserControl uc = new UserControl();
uc = (UserControl)currentPage.LoadControl(sUCFilename);
Control ph = FindControl("placeHolder");
ph.Controls.Add(uc);
}
}
}

As you see it's not much code and I didn't have to use any magic at all!
The code above is a full working Custom Web Control that loads a User Control dynamically to a PlaceHolder with the id "placeHolder" (make sure this exists on the page later or you'll get an error). The User Control have the filename "test.ascx" here but it can be whatever (again be sure it exists or you will get an error). The User Control can contain any markup and Web Controls. Just compile the code into a dll-file.
To use it in an ASP.NET page this following markup does the trick:

<%@ Page Language="C#" Debug="true" %>
<%@ Register TagPrefix="LoganControl" Namespace="MyControls" Assembly="MyControls" %>
<html>
<head>
<title>The Page</title>
</head>
<body>
<form runat="server">
Some content here
<br/>
<LoganControl:LoadWriteUserControl id="cLWUC" runat="server"/>
and some text here perhaps
<br/>
<asp:PlaceHolder id="placeHolder" runat="server"/>
<br/>
well...let's put some here too...
</form>
</body>
</html>

That's actually all that's needed for this. It wasn't really that hard. If you got any suggestions or anything on the code above you can still post this thread.
Thank you all for helping me on this one!

Regards,
Logan Benderius
23 Items, 2 Pages 1 2 |< << Go >> >|


Free Download:

Books:
Hypercube Multiprocessors, 1987: Proceedings of the Second Conference on Hypercube Multiprocessors, Knoxville, Tennessee, September 29-October 1, 1986 Authors: Michael T. Heath, Oak Ridge National Laboratory Mathematical Sciences Section, Society for Industrial and Applied Mathematics, Oak Ridge National Laboratory Mathematical Sciences Section, Society for Industrial and Applied Mathematics, Pages: 761, Published: 1986
The Cambridge Handbook of Multimedia Learning Authors: Richard E. Mayer, Pages: 663, Published: 2005
Dynamic toxics waste load allocation model (DYNTOX) user's manual.: USER'S MANUAL. Authors: Limno-Tech, Inc., Ann Arbor, MI., Pages: 77, Published: 1985
Advances in Web and Network Technologies, and Information Management: APWeb/WAIM 2007 International Workshops, DBMAN 2007, WebETrends 2007, PAIS 2007 and ASWAN 2007, Huang Shan, China, June 16-18, 2007 : Proceedings Authors: Kevin Chen-Chuan Chang, Pages: 707, Published: 2007
Pile Design and Construction Practice Authors: Michael John Tomlinson, Pages: 411, Published: 1994
GLOBECOM '01: IEEE Global Telecommunications Conference : San Antonio, Texas, USA, 25-29 Novemberber, 2001 Authors: Texas IEEE Global Telecommunications Conference San Antonio, IEEE Communications Society, IEEE Communications Society, Pages: 4318, Published: 2001

Web:
Load user controls dynamical, again - ASP.NET Forums Re: Load user controls dynamical, again. 01-03-2005, 2:56 PM. Contact ... Re: Load user controls dynamical, again. 01-03-2005, 4:53 PM. Contact ...
Dynamic load control & viewstate problem - ASP.NET Forums I think you need to add all dynamic control again and again for each ... I just need to load also the previous user control (and hide it ...
CodeProject: Dynamic User Control, Ajaxify Your Controls. Free ... Dynamic User Control, Ajaxify Your Controls. By Jean-Pierre Thomasset UpdatePanel without full page load, Javascript, XML, C# 2.0, C#, Windows, .NET, . ...
Loading UserControl Dynamically in UpdatePanel In this post, I will show you how to load different user control in ..... is fired...but...if you again click from menu and load again sample1 ascx and then ...
Yasin Tarim Blog: Asp.Net Ajax : Creating Dynamic User Control and ... Jun 26, 2008 ... In order to start creating a dynamic user control let’s implement a simple user control ..... to load the script again during a postback ...
Thoughts of a ponderer: Do's and don't's for dynamic control loading. What do we need for dynamic control loading: First of all we need a ASP.Net page . The standard default.aspx does nicely. Second we need a few userControls ...
Brig Lamoreaux: Beware Dynamic User Controls in a Web Farm Beware Dynamic User Controls in a Web Farm. Many large sites utilize a web farm with a load balancer. The load balancer will cycle through each server in ...
Urgent : Viewstate and dynamic controls !ONCE AND FOR ALL PLEASE ... controls. This loading of user controls is done within the code-behind of ... really need an understaing of this once and for all - again, please. Regards ...
ASP.NET.4GuysFromRolla.com: Dynamic Web Controls, Postbacks, and ... Sep 29, 2004 ... To load the current values, I first need to determine the user's type, ... Dynamic controls, as evidenced in this articles and others on ...
User Control and dynamic LinkButton problem - .NET ASP User Control and dynamic LinkButton problem. ... wish to tell the main page to load a different UserControl (depending ...

Videos:
Looming City Bankruptcy?; Online Searching Risks; Public Square: Minimum Wage What is the prognosis for San Diego's troubled financial future? City officials and residents alike are looking for answers. Days after news that San...
An Introduction to SQLite Google TechTalks May 31, 2006 Richard Hipp ABSTRACT SQLite is a small C library that implements a self-contained, embeddable, zero-configuration SQ...
Web Applications and the Ubiquitous Web Google TechTalks February 1, 2006 Dave Raggett Dave Raggett is currently a W3C Fellow from Canon, and W3C Activity Lead for Multimodal Interaction....
XML11: An Abstract Windowing Protocol Google TechTalks June 1, 2006 Arno Puder Arno Puder received his masters and Ph.D. in computer science and is currently working as an Assistant Prof...
How To Break Web Software - A look at security vulnerabilities in web software Google TechTalks April 13, 2006 Mike Andrews Mike Andrews is a senior consultant who specializes in software security and leads the web application...




Search This Site:










intranet authentication with active directory

problem with formsauthentication!

how to use grpups with membership, roles, profile provider

local aspnet account can't write to other server

user authentication on different server

what calls formsauthentication.signout()

what are the 'real world' security advantages of the .net framework and the jvm?

editing roles folder access while in production

createuserwizard

impersonation and spaning process under specific user accounts

¿aspnetdb.mdf readonly?

asp.net configuration interface at runtime

what's the best way to login users?

membership.deleteuser() not removing user from role

can't write a .xml file to the server's hard disk (a security issue?)

getting user details

iis + sql server delegation

multiple authentication

cryptographic service provider (csp) could not be found for this algorithm.

windows authentication on vs.net web project

!! please help - permissions issue writing file.

block using ip

clarification of createuserwizard tasks

two membership providers and two role providers in same application.

loginview and findcontrol heck

put userid in cookie? advice request

asp.net providers

exepath error when using custom membership provider

deny all users through web.config works, but no stylesheets, etc.

asp:placeholder iside loginview control

  Privacy | Contact Us
All Times Are GMT