`include` statement for aspx? - asp.net

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.

Related

What is the best way of setting the src of an iframe from a codebehind file in ASP.NET?

I have an iframe that has a dynamic URL based on the results of web API call. What is the best way of setting its src in the aspx page? Here is an example of the iframe:
<iframe id="dynamicframe" runat="server"></iframe>
Should I set it directly in the codebehind like so:
dynamicframe.Attributes["src"] = "http://dynamicurl.com";
Or should I create a property in the codebehind and reference it in the iframe:
<iframe id="dynamicframe" src="<%= dynamicFrameUrl %>"></iframe>
Or some other method altogether?
This is a general question that can stands the same for any html tag.
The alternative third option is to use a literal control and fully render the iframe on code behind as:
txtLiteral.Text = "string.Format(
"<iframe id=\"dynamicfrmae\" src=\"{0}\"></iframe>", PageUrlForSrc );
The different for all methods :
Direct write on page <%= %>
Not work with update panel
Its run the moment the page send to the browser (and not before on the page steps)
Not accessible as control
This is the method that I avoid most. I use it only when I like to left some calculations for later and avoid page cycle, or when I have responce.flush() just before it.
Write it using to literal
Compatible with UpdatePanel
Not accessible as control
Write it as attribute on code behind
Make the control pass the steps of the html cycle
Is accessible else where on the page as variable
The id of this control may change but you can avoid conflicts
All methods have their purpose, and I used then according what they fit best.
Well if you have the URL available to you in the code-behind file I would say option one is the most straight forward. However, I'm a little confused about the question, are you looking for a best-practice?

.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

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.

Web User Controls, Javascript and Script Managers

Is there a way to only add a script manager to your Web User Control if there is not one already on the page (on the main page or if you are using the Web User Control multiple times)?
Same question about adding javascript files to the page, many times I have controls that add the javascript into the head of the page for the control.
Regarding ScriptManager:
I would use master pages, and include the script manager on your master page. Alternatively, if you have something like Header.ascx which you know is included on every page, you could put it there also.
Regarding javascript files:
Use the ClientScriptManager.RegisterStartupScript method to include javascript on your page. It will not produce duplicates if they share the same key name parameter.
http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

ASP.NET Include Disables Code-Behind

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".

Resources