Free 3 Months
|
| |
| danscan | Asp.Net User |
| Re: Load user controls dynamical, again | 1/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, again | 1/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] |
| shanthanu | Asp.Net User |
| Re: Load user controls dynamical, again | 1/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, again | 1/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, again | 1/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, again | 1/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, again | 1/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, again | 1/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! |
| RadAway | Asp.Net User |
| Re: Load user controls dynamical, again | 1/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?
|
| RadAway | Asp.Net User |
| Re: Load user controls dynamical, again | 1/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 |
|
| |
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:
|
|