Access Controls which are written by Response.WriteFile("Sample.htm") - asp.net

My aim is to stream a .htm file via Response.WriteFile("Sample.htm"); and then access a specific html element (ex. <a runat="server" id="myAnchor" /> ) from the Response which happened in the Page_PreInit Event.
I tried it already with ((HtmlGenericControl)myAnchor) but it doesn't work. It only works, if the anchor tag is inside the .aspx page.
Is there a possibility to reinitialize the .aspx page after the response.write event happened, so that the anchor tag from the sample.htm file gets indexed like it would be an anchor tag from the .aspx page.
Thanks for your help.

No. Once you write anything directly to the response stream it leaves the web server (where your code is running) and goes directly to the browser. Do not pass 'GO'. Do not collect $200.
Anything in that file is never loaded into your Page class' control tree in the first place, but sent directly to the browser. "reinitialize the .aspx page" wouldn't help you. Instead, to re-use content like this, you need to embed it in a control that can be included on the page or put it in a master page.

Related

Stuck in iFrame, how to get out?

I'm using ASPX web pages (WebPage1.aspx) and have an HTML iFrame defined in this page. I dynamically define the iFrame src (via code behind) when my ASPX web page loads ... all good so far.
The web page that gets loaded into my iFrame supports a "ReturnURL" that I've defined when I set the src for the iFrame. Something like:
src = "https://www.someothersite.com/Somewebpage.aspx?ReturnUrl=http://www.mywebsite.com/WebPage2.aspx"
The web page (Somewebpage.aspx) has a "Return" button, the user clicks the Return button and they should get redirected back to the "ReturnURL" I specified (http://www.mywebsite.com/WebPage2.aspx).
What happens is http://www.mywebsite.com/WebPage2.aspx gets displayed in my iFrame on http://www.mywebsite.com/WebPage1.aspx. This IS NOT what I wanted, I wanted to simply be return to http://www.mywebsite.com/WebPage2.aspx.
Is there a solution to this problem within the context of ASPX and code behind?
Cheers, Rob.

asp.net bug? ResouceManager.GetString(string) when string not found, aspx file is executed twice

I tracked down a very weird ... bug...
I found that for some reason, an ASPX page always executed twice.
I tracked it down to this line in a user (asxc) control, I had :
<img src='<%=RS("buildhover")%>' />
RS is just a helper function that resolves to ResouceManager.GetString("buildhover")
I found that "buildhover" was simply missing from the resx file that was being read. When added, the ASPX page is no longer run twice...
This is very strange and since I use resource files extensively, I am really interested to find out why this is...
When you have an image element with a blank url for the string then it makes a request to the current page. When the resource doesn't exist you get a blank string. So the result of ResouceManager.GetString("buildhover") is an empty string.
Look at the html produced. You will have something like <img src="" />
If you are observing load event twice in a post back in ASP.Net page, check the following:
1.If Page_Load handler defined in Codebehind then AutoEventWireup property should be “false”
Check if you have mistakenly multiple event handlers registered for an event
Src or ImageURL attribute of your image control are defined and not empty (you can put %20 for blank)
bgColor or background is empty
Last two problems usually appears in one browser while disappears in other.
http://devshop.wordpress.com/2008/06/02/aspnet-page-loading-twice/

How to react in ASP.NET code-behind class when a hyperlink is clicked inside the page's IFrame?

I have an ASP.Net page containing an IFrame. In the IFrame I load a html document. When the user clicks on a hyperlink in the content of the IFrame, I would need a callback to be called in the code-behind class of the ASP.Net page.
I guess that I need Ajax to do this but I'm not exactly sure about what I need to do. Could you give me some pointers?
By the way I'm fairly new to ASP.Net.
Thanks
A lot of this depends on what it is you want to do specifically.
The problem you've got is that the DOM of the page in the iframe doesn't appear to be in the DOM of the calling page. All the calling page sees is the iframe tag as a closed tag, like an image tag. Some browsers will detect a click inside an iframe nested within a DIV as a click in the div so you have
<DIV id="iframediv">
<Iframe blah...>
</DIV>
and then you might be able to use jQuery or similar to detect a click inside iframediv and do stuff.
The real solution would be to try not to use an iframe as, like I said, even this solution won't necessarily pay off. I can think of at least one scenario where not using an iframe is not an option so I'll leave that be.
Other than that Willem's suggestions also seem to be sound.
Because the html document is not an aspx page it will not be able to trigger any code-behind. If you can change the page in the iframe make it an aspx page and handle the click on a LinkButton like you would do otherwise.
An other option is to change the link in the html page to call a custom aspx page that handles your needs, but that will redirect the html-page to the new aspx page.
Or indeed change the link to call a webservice through javascript (XMLHttpRequest) and let that webservice do what you wanted to do in code-behind.
Finally I ended up writing a Control Extender for the IFrame. The Control Extender gets the links contained in the IFrame via the following Javascript:
var frame = this.get_element();
var links = frame.contentWindow.document.getElementsByTagName("a");
I then simply attach an event handler that reacts to each link's onclick event. The event handler calls back the ASP.Net side via a WCF service.
Not complicated to do once you know the various technologies.

Response.Redirect and IFrames

I have a Main.aspx page and a Sub.aspx page.
The Sub.aspx page resides in my Main.aspx page via IFrame. So the IFrame contains / loads the Sub.aspx
In another Third.aspx completely unrelated I've got some code that needs to redirect back to Main.aspx but then there are some querystrings that need to be passed to Sub.aspx (or put antoher way my Sub.aspx needs to get those values from the Main.aspx).
My Sub.aspx will check for that querystring flag and if set, run x JS scripts.
Maybe pass the QueryString values to Main and, in its Page_Load, include them in the src for the iframe? You can use the iframe like a server control to do this.

How does one call method exists in a page from another page?

I'm working on a page, in which other pages are opened from it in a modal way.
I need to call function exists in opener page to update certain controls in it.
I'm using window.open to open another window but in this case Page.PreviousPage for opened page is null.
I'm using
<%# PreviousPageType VirtualPath="~/PreviousPage.aspx" %>
to refer to Previous Page.
Any suggestions?
FYI: all aspx pages are AJAX-Enabled.
You can't call a method in the code behind of a Page class to update controls in a page that is displayed. The instance of the Page class only exists while the page is rendered, once the page is displayed in the browser, the Page object no longer exists.
The PreviousPage property is used when you make a post from one page to another, but it doesn't contain the Page object that was used to render the page, it contains a recreated Page object that will not be used to render anything at all. You can only use it to read information from the fields based on the information that was posted from it.
If you want to update the opener page you either have to do it on the client side using Javascript (), or reload the page so that the server side code can repopulate it. A combination of them both would be to use AJAX to update the page.
Edit:
You can for example use Javascript to access the opener and change the content of an element:
window.opener.document.getElementById('Info').innerHTML = 'updated';
You can also call a Javascript function in the opener page:
window.opener.doSomething('data');
That which gives you more possibilities, like making an AJAX call to load data from the server.
You can submit the parent page back to the server using javascript. You can use window.opener function in the javascript to access the parent page.

Resources