ASP.NET Include Disables Code-Behind - asp.net

I've found that when using the
<!-- include file="MyPage.aspx" -->
command in ASP, I'm not able to use the code-behind in MyPage.aspx.
The issue is that when I try to include MyPage.aspx, there is an error because we have two Page Directives. If I remove the Page Directive, I can include MyPage.aspx just fine, but cannot access the code-behind, because the "CodeBehind" parameter in the Page Directive is no longer there.
So, as far as I can tell, we have a Catch-22. Does anyone know of a work-around for this? Or is there just something I'm missing?
Thanks,
-Onion-Knight
I'm not sure if this changes anything, but I am using a Master Page with the page that includes MyPage.aspx.

Why don't you use a user control (*.ascx) instead of including an aspx page?
Have a look at this overview in MSDN which shows how to create and user "user controls".

Related

.aspx works but .ascx does not... why and how to fix?

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

`include` statement for aspx?

I'm trying to include an external aspx page on my aspx (VB) page. If it were php i would have done it with a line of code like <? include "http://www.google.com"; ?> what would be the equivalent of include statement for aspx? Isn't there a VERY SIMPLE way of doing this?
I don't really understand why you need this.. But
If you need to have some another page on your page, you can use iframes for that.
If you create your custom control what would do HTTP request to required site output response as control HTML.
In ASP.Net, you set up the page to include as a custom or user control, and add the control to the page. Includes don't really work well.
It really depends what you are intending to do, if you are trying to get some shared user interface elements, then master pages, or user controls are what you are looking for
If you are trying to include common functionality, you can inherit this by inheriting from a base page which itself inherits from System.Web.UI.Page, and which contains the common code.

ValidateRequest="False" inside a master page

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.

Duplicate Script Managers

I'm working on an ASP.Net webforms app, and I've run into a unique issue. There is a script manager for an update panel on the MasterPage, as well as a script manager in a sub-page calling the master page. This as i'm sure you will note cause an exception to be thrown and crash the webapp.
I've tried programatically excluding the scriptmanager + update panel from the code as follows:
<% If Not (Result.RawURL.Contains("ExcludedPageDirectory") Then %>
<!-- all that code goes here -->
<% End If%>
However I think just the presence of the script manager tag in the source is causing the error. How would I programatically handle this?
A page can contain only one ScriptManager control in its hierarchy. To register services and scripts for nested pages, user controls, or components when the parent page already has a ScriptManager control, use the ScriptManagerProxy control (Source: MSDN)
You don't require a script manager again in the page if you have it in the master page.
Remove it and try
I ended up duplicating the master page and left out the update panel/scriptmanager code. This seems to have been the most elegant solution for the given situation. However I feel that the ScriptManagerProxy answer held the best possible solution had the duplication of the masterpage not been possible.

Linking to ascx file

I am utilizing controls in my asp.net application. I have a register tag the source of which needs to be dynamic. I am using the line below which functions correctly when the full path is specified but when I change it to the variable I get a parser error. Any idea how I can go about doing this?
Thanks
It might be better to use Load Control from the code behind of the aspx page.
If I'm understanding you correctly — you can't use a variable in those directives (Page, Register, etc). They have to be constant expressions.
However, it is possible to dynamically load ASCX controls. You would have to do this in code, though, and it would not involve the Register tag.

Resources