I want to prepend an aspx page with another page at runtime.
I know the better solution would be to use a usercontrol, But for that I would have to put all the elements of my aspx page in a user control & do that for several pages.
Isnt there a way to load an aspx page inside another page like a user control?
Also I would want to make all the fields readonly of the appended page. Is there a way I can do this?
If usercontrol is the only way, then I have a query.
When I loaded my userControl in my PageLoad event, I cannot cast the control to my usercontrol name, & because of that I cannot set a property of my control.
I want to do this, but this gives error at compile time:
MyControl myUserControl =(MyControl)Page.LoadControl("~/Controls/MessageControl.ascx");
Here MyControl is not available (It is not recognizable by the compiler) I tried Ctrl+Dot on MyControl, the compiler is not adding any reference to it also, It gives the following error
The type or namespace name 'MyControl' could not be found (are you
missing a using directive or an assembly reference?)
When I do this, it works:
var ctrl = LoadControl("~/Controls/MessageControl.ascx");
MyPlaceholder.Controls.Add(ctrl);
But now ctrl is not casted which prevents me to set a property of my user control.
Also does it matter If I instantiate the control in Page_Init instead of Page_load? Because in Page_Init I am unable to make a DB call, probably the assemblies/db config hasnt loaded in Page_Init..
Related
How do I get/set a property within a User Control inside User Control within Master Page in c# ?
Example structure:
Web User Control 'WebUserControlA.ascx'
Web User Control 'WebUserControlB.ascx'
Master Page 'Content.master'
Web Form Page 'Test.aspx'
'Test.aspx', is a content page from the 'Content.master' page, it includes:
<%# MasterType VirtualPath="~/Content.master" %>
The content.master page includes the 'WebUserControlA.ascx' and in 'WebUserControlA.ascx' is 'WebUserControlB.ascx' included.
I wish to be able to set text of a label control in the 'WebUserControlB.ascx' from the 'Test.aspx' code behind file.
UPDATED NOTES:
After receiving a potential answer from 'VDWWD', which allowed me to set the text label, I need to expand my original question:
"I would like to control a get/set a property within the code behind of the WebUserControlB.ascx so that I'm able to display content from the database correctly, for example I want to pass in a country code from the code behind of test.aspx to the WebUserControlB.ascx code behind"
public string CountryCode { get; set; }
I have tried the following however was not able to get hold of the get/set property:
UserControl WebControlB = (UserControl)Page.Master.FindControl("WebUserControlA").FindControl("WebUserControlB");
if (WebControlB != null) {
WebControlB.CountryCode = "[CODE]";
}
When trying to do this i get the following error:
'UserControl' does not contain a definition for 'CountryCode' and no
extension method 'CountryCode' accepting a first argument of type
'UserControl' could be found (are you missing a using directive or an
assembly reference?)
Thanks for helping
I have a page called test.aspx with test.cs.
However, i want to access my control called mbResult
Which is my custom messagebox control, from a sepearate CS page.
I know many people have asked this question and i have found that this is a method to access my controls.
MessageBoxControl mbox1 = this.FindControl("mbResult") as MessageBoxControl;
But I keep getting this error
Error 5 Keyword 'this' is not valid in a static property, static method, or static field initializer
Any ideas on how to access this control all i am trying to do is make it visible.
Thanks
You need to move the code into a non-shared method. You need to be operating in an instance of the page.
Update for clarification in comments
Unfortunately, your application is going to need some restructuring.
If the messageboxcontrol is shown in a new window, then you will need to pass the value from your source page to the new window in the query string.
However, if you want the messagebox control to be displayed on the source page, then you will need to convert it from a page to a UserControl, add a reference to the user control to your source page, and then add an instance of the usercontrol directly to the source page.
Statics don't have instance-based contexts, so using this is not applicable. You'll need a reference to the control for which you want to use .FindControl (possibly by passing it as a parameter).
I am writing a class called Evaluatieform that works with web user controls, their events and eventually adds them to a panel so the web page only needs to instantiate the Evaluatieform class and call a method that returns the panel so it can show it on the website.
Now the problem is that I can't instantiate my user controls I have defined.
First error : The type or namespace name 'DomeinsCriteriums' could not be found(are you missing a using directive or an assembly reference?)
So the next thing I try is to drag the user controls needed into the app_code folder. This however, does not work because the compiler does not allow a web user control in that folder.
I read somewhere else to use
ASP.webusercontrol_ascx usercontrol = new ASP.DomeinsCriteriums_ascx();
However, this does not work aswell.
Can anybody give me a quick solution for this problem?
All you need to do to load a user control is this.
Control ItemX = (Control)Page.LoadControl("/controls/yourusercontrol.ascx");
However if you are getting a namespace error. Simply add the namespace to the top of your page.
using DomeinsCriteriums;
I have a ASP.Net project that is setup in such a way that it can be dropped into any site and "just work." All the paths are relative to the current file, not relative to the "~". The paths are determined by ThePath = this.TemplateSourceDirectory;
This is working for everything expect registering a custom control that is created and added to one of the pages. I can add the control just fine with the Page.LoadControl but I cant cast it as the correct type to access anything.
How can I add a reference to the class from within the code itself?
If you don't know the control's specific type ahead of time, this isn't possible. The class must derive from UserControl, so you can cast it to a UserControl and you'll have access to all the methods and properties on that class. If there's some special information or functionality you need to require all controls to have, and you need to be able to assume those are always present, then you will have to write your own class that derives from UserControl, and require all custom controls to derive from that instead:
//all custom controls must inherit from this
public abstract class SpecialControlBase : UserControl
{
public abstract void DoSomethingSpecial();
}
Then you could cast all controls at load-time to this SpecialControlBase, and have access to the DoSomethingSpecial method.
But as far as the most-specific members of a class loaded at runtime, think about it - if I write my own control called RexsUserControl and drop it into your application, there's no way you could know what methods and fields I've put on my control, so you can't write any code that references those members specifically.
Rex M, If you register the control on the ASPX page, you can cast the control on the code behind.
In the ASPX:
<%# Register TagPrefix="Mine" TagName="Ctrl" Src="~/Test/User/Controls/UserCtrl.ascx" %>
Then, in the ASPX.CS:
User_Controls_UserCtrl myUserCtrl = LoadControl("~/Test/User/Controls/UserCtrl.ascx");
however, if you don't register the control in the ASPX first, you get this error:
CS0246: The type or namespace name 'User_Controls_UserCtrl' could not be found (are you missing a using directive or an assembly reference?)
I'm looking for a way to (preferably) strongly type a master page from a user control which is found in a content page that uses the master page.
Sadly, you can't use this in a user control:
<%# MasterType VirtualPath="~/Masters/Whatever.master" %>
I'm trying to access a property of the master page from the user control and would rather not have to pass the property from the master page to the content page to the user control because multiple content pages use the same user control. One change, one place whatnot.
Try Page.Master.
Whatever whatev = (Whatever)Page.Master;
You'll have to make sure you add the proper using statements to the top of your file, or qualify the Master page type inline.
One potential gotcha is if this control is used by a different page whose master page is NOT the same type. This would only get caught at runtime.
Have you tryed Page.FindControl("name") on the usercontrol?
The best way to do it that I've found is actually to build a custom class that is based off of UserControl, give it a Master property with a get accessor that fishes through the this.Page.Parent until it stops encountering master pages (If you are nesting, this step is unnecessary otherwise) and then return that web control as the type of the master page you want to use. Then, when you add a new user control, change it's base class to the name of your custom class. The .Master property will be accessible and cast properly as the master page you want it to use.
In VB all I needed to do was change this:
Dim lAuthLevel As Integer = Master.MasterContact.AuthenticationLevel
to this:
Dim lAuthLevel As Integer = CType(Me.Page.Master, main).MasterContact.AuthenticationLevel
So all references of Master become Ctype(Me.Page.Master, typeofMaster)
Where is in this case the word "main" - get that from the declaration at the top of the master page. e.g.
So "main" in this case :)