I need to do a dynamically loaded Header and Footer on Master page where there are two placeholders to store the users controls which will become the Headers and Footers. The reason why I am doing it to make it more customizable and it is the how it is required from my supervisor as well.
So I have two key-value in web.config such as
<add key="MasterHeader" key="~/Controls/Header.ascx"/>
<add key="MasterFooter" key="~/Controls/Footer.ascx"/>
So I am wondering which Page event should I load these users controls by saying
UserControl uc = (UserControl) Page.LoadControl(ConfigFile.MasterHeader))
PlaceHolder1.Controls.Add(uc)
Where is the best Page event to put the codes above?
Do I need to put something in the page directive to make it work?
I would use OnPreInit or OnPreLoad. You shouldn't need to put anything in the page directive to make this work, the way you're describing it.
Related
Scenario:
I have one main master page say MasterPage1. In that master page I have a splitter. In that splitter there is an iframe. Within that iframe we load another master page say "MasterPage2". In MasterPage2 we load a page on which different User Controls are rendered.
Problem:
Now I want to find a control on MasterPage1 from my User Control loaded on the page in MasterPage2.
Please help....
Problem To your Scenario:
masterpages and content pages are rendered as a single object, thus the page class is able to reference every element found in both the objects(master and content page). When you are rendering an iframe the iframe content is requested by client hence no reference exists. so it is not possible to reference each other on server.
Solution to the problem
From above you must have realized all the problem is the reference , so you will have to hack inti it. the simplest way I can think is to use querystring.
call the iframe page with querystring containing a identifier to the masterpage like mpage=mpage1,mpage=mpage2 etc.
Now in masterpage2 request the querystring to find which masterpage is applied and proceed. This way you will have little relaxaction because masterpage1 content cannot be changed but masterpage2 can be.
Now you will need to work more to what you need. Proceed only if this is the only way to solve the real problem(I think the problem is not masterpage but the solution to the problem that is making you to do these weired things).
Well for that you will have to use javascript and handlers which will render and return the rendered usercontrol. But i seriously say not to use this setup in production and find other alternative by changing your code to use usercontrol instead of iframe.
I want to put a ASTreeView web control in a custom web control, ASTreeView sample code is like:
<ct:ASTreeView ID="astvMyTree"
runat="server"
...
LoadNodesProvider="~/ASTreeViewDemo5.aspx"
.../>
LoadNodesProvider is the page ajax called when loading a node...however if I changed the provider to my .ascx file, it does not work:
LoadNodesProvider="~/ASTreeViewDemo5.ascx"
it did not even go through the Page_Load part of the .ascx file
Though this might be related with astreeview itself, I'm wondering what the problem could be? anything I can do to fix it?
Thanks!
It is because ascx must have a container ie Page. You can't use it same way as Page.
ASPX is a page and ASCX is a usercontrol. You cannot ajax call a control, so you probably want it to be a page with the control on it.
While it isnt entirely clear to me what LoadNodesProvider is supposed to do, if you want to encapsulate some code or run a process via AJAX you have a couple of options. One would be to create a web service (you could use WCF for this) that the AJAX method could call. Another option would be to create an http handler (ASHX extension typically denotes this). Using an ASPX or ASCX for this doesn't make a whole lot of sense to me. Proco and Tomas are correct regarding the ASCX file, these are Usercontrols and are not stand-alone objects.
If you really, really want to use an ASPX page/ASCX control, then I suppose it would be best to create a blank ASPX page that has one placeholder, and then attach your user control (based on query string parameters or something I guess) to the placeholder to render out the content for your AJAX control
My application uses a single aspx page, and dynamically loads in master pages based on the url.
I have a few secured master pages that have forms that need to post html, so I have to set ValidateRequest="false". This of course isn't available in the #Master directive, only the #Page directive. I don't want to disable this security setting site-wide, so is there an alternate way of accomplishing this at the master page level?
I'm referring to the "A potentially dangerous Request.Form value was detected from the client" error message you get when posting a page with html markup.
I don't think I can use web.config either as it's the same physical url that's serving up the entire site.
My only thought is to use javascript to encode the input.
Thanks.
EDIT: Confused Request validation with Event Validation.
Why not set Page's ValidateRequest also, based on URL.
But just out of curiosity, why did you choose that approach? to have one content page and vary the master? Are you applying theming based on the URL?
As it seems you cannot set ValidateRequest property dynamically. It has to be set either in the page directive, or Web.config.
Try using Page.EnableEventValidation = true, if that gives the needed result.
I have a certain page and depending on an administrator role a lot of extra validation controls have to be inserted. However I don't want to take the risk that a validator gets turned on for other users.
What I was thinking is to use some form of "Visual inheritance" but I don't know which path to choose. Perhaps have separate .aspx pages which inherit from the same class (which inherits from Page)?
Side note: this has to be done in webforms, not mvc.
Extra information: The problem I'm trying to solve is the separation of roles and the impact on the page without having to duplicate the page and having to maintain any changes in both. There are clients who get to see the vanilla page and admins can see the same information but with added validation controls. However these validation controls should not be seen by the clients. Also some more information can be seen but that could be handled by using rolebased viewing of certain user control.
Anyone has an idea about this?
Why don't you want to use master pages? Master pages do exactly what you want to do - they can even "inherit" from each other (in a manner of speaking).
I would suggest that you use master pages to do what you want.
ASP.NET master pages allow you to
create a consistent layout for the
pages in your application. A single
master page defines the look and feel
and standard behavior that you want
for all of the pages (or a group of
pages) in your application. You can
then create individual content pages
that contain the content you want to
display. When users request the
content pages, they merge with the
master page to produce output that
combines the layout of the master page
with the content from the content
page.
If the problem you are trying to solve is role based viewing of controls and you are using the builtin asp.net membership/roles providers, you could use the LoginView control to manage the visibilty of the admin stuff:
<asp:LoginView runat="server" ID="LoginView">
<RoleGroups>
<asp:RoleGroup Roles="Admin">
<ContentTemplate>
Special Admin content
<asp:RequiredFieldValidator></asp:RequiredFieldValidator>
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
Often such problems can be solved by splitting the page into user controls (.ascx). It can become a bit messy, but gets the job done.
Inheritance of pages is difficult, although possible. What you must realize, is that one .aspx page cannot inherit from another .aspx page. This is because of how the ASP.NET compiler works: your .aspx XML markup is compiled into a class that derives from your code-behind class. Since this only happens at runtime, you cannot have another .aspx page inherit from it, because the class is simply not there when the code-behind is being compiled.
What you can do is to create another class that inherits from Page and make your .aspx code-behind classes inherit from that. That is possible, but note that this class will not have an .aspx XML part - you will have to instantiate all the controls yourself, as well as assign their properties. In most cases this will be quite messy.
I'm working on a project that has all its user controls registered in its web.config file (which seems very clean and tidy)
So far so good (here comes the problem) however I'm trying to dynamically create and add user controls to a page. These user controls fire events that need handling.
Ordinarily that wouldn't be a problem:
You just register the control in the page, load the control, cast it to the correct type, and assign the event handlers, add it to the page, sit back and let the magic happen, easy peasy.
But I can't reference the control's type when the control is registered in the web.config, which means no cast, which means no event handling!
Weirdly you can reference the type if you add the usercontrol to the page at design time!
There must be a way round this (without having to register the control on the page, or add a control at design time), what on earth am I missing?
It's been a while, but I think I've seen this type of behavior in ASP.NET when a project is a Web Site and not the Web Application. As far as I remember, the Web Site compiles each page into its own assembly and with no common name space and regardless of config requires the <%# Register %> directive. If you don't, you get the exact error of missing an assembly reference.
I would have to test to be sure...
By saying : "you can reference the type if you add the usercontrol to the page at design time"
Do you mean it adds an <%# Register %> Directive at the top of the page ?
Or maybe, it adds a using / Imports (depending on you using c# / vb.net) clause in your source document ?
Because, to be able to cast to your control type, you normally need to import the namespace in the codebind. Maybe this is just what is missing.
The <controls> section in web.config and the <%# Register %> directive are the same thing (with the small exception that entries in web.config apply to the whole application). They allow you to add design-time controls to a web form.
If you want to add controls to a page dynamically, use the LoadControl function to get an instance of your control. Given a control with a class name of "Header", the following will load a control, set a property, and add the control to the form named, "form1":
Dim head As Header = LoadControl("~/Controls/Header.ascx")
head.Text = "Some text..."
Me.form1.Controls.Add(head)