Refresh an iframe using ASP/VB? - iframe

I have an iframe, looks like this:
< iframe id="PDFLetter" src="http://127.0.0.1/letterwriterasp/pdfs/test.pdf" width="60%" height="500" runat="server" scrolling="auto" >< /iframe>
Problem is, that pdf file is regenerated. So I need to refresh the iframe to reflect the changes to the user. I tried this:
PDFLetter.Attributes("src") = ""
PDFLetter.Attributes("src") = "http://127.0.0.1/letterwriterasp/pdfs/test.pdf"
But to no avail. It doesn't refresh the pdf in the iframe.
Not sure what can be done here. Any ideas?
Thanks,
Jason

Are you doing this on a postback and entering the code in the PageLoad event? If so it should work (for complete control over html you can laways just write to a Literal control although this is much heavier).

Try adding a random number at the end of the pdf name using ?randomnumber so that it does not use a cached version of the pdf file.

Related

Displaying html page in ASPX page

I need to display dynamic created html in aspx(server side html);
I tried to use Iframe but it will not displaying anything ; it will not work because of security reasons ;
Is there is any controls that will display html page? Dynamic html have its on css and javascripts so I can’t use html text box controls.
If anyone have solution please help
Thanks
Since your dynamic page has its own CSS & Javascript, I'm assuming it's not written to coexist with its host page. I'm also assuming that when you tried to use the iFrame you just tried to write straight to it from the containing page.
I would suggest moving your code that generates the HTML to a separate ASPX page and referencing that page as the source of your iFrame or rewriting your CSS & Javascript so it will coexist and using a DIV.
Also, it's sort of hard to come up with a workable solution without you showing some of the code you have currently.
Have a look at the
<asp:Literal>
control. There's an example here: Set ASP Literal text with Javascript
-- EDITED 03/05/2012 --
Simple example of an asp.net literal control in action:
.aspx code
<asp:Literal ID="MyLiteral" runat="server" />
.vb code behind
Dim k As String
k = "<table style=""border: 1px solid red;""><tr><td>Cell 1</td></tr><tr><td>Cell 2</td></tr></table>"
MyLiteral.Text = k
If I compile this in VS2008 I get a two row table with a red border in IE.
I found the answer!! Use UFRAME!! It's simple and easy!! uframe.codeplex.com

Avoid browser cache in asp.net

I try to disable the html url browser cache using programmatically.
I am developing as a site using asp.net, I need to disable the html url browser for the security reason.
I tried many ways to disable the cache but none seems to work. Any ideas?
<iframe id="iframe" src="http://www.phy.mtu.edu/basiccomputing/sample.html" runat ="server" width="200" height="300"></iframe>
As far as it's a html file you want to work with, a easy solution is to append a random number and the end of your url like this
<iframe id="iframe" src="http://www.phy.mtu.edu/basiccomputing/sample.html?12345" runat ="server" width="200" height="300"></iframe>
I don't know wich view engine you are using, so i cant provide a sample you simple copy and paste.
You have to replace ?12345with a random number like new Random().Next().ToString()
If you call a asp.net page you can control it with
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.Cache.SetNoStore();
more informations about that:
ASP.NET Cache Examples
hope this helps
You could attach a random parameter to the URL so the browser thinks it's a different page every time.
http://www.phy.mtu.edu/basiccomputing/sample.html?rand=[INSERT RANDOM HERE]
try
HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)). This site has something that can be quite helpful.

hyperlink.NavigateUrl getting changed on master page (possibly by ResolveUrl() )

I have a hyperlink that in certain cases I want to change to show a jquery popup, but I'm having a strange problem when doing it on a master page. The following works in a regular page:
hyp1.NavigateUrl = "#notificationPopup";
Which renders as:
<a id="ctl00_hyp1" href="#notificationPopup">Example</a>
This is exactly what I want. The problem is with the exact same code on a hyperlink on the master page it renders as:
<a id="ctl00_hyp1" href="../MasterPages/#notificationPopup">Example</a>
It looks like it might be running the navigateUrl through ResolveClientUrl() or something when I'm setting it on the master page. I've tried swapping the <asp:hyperlink for a <a href runat=server, but the same thing happens.
Any ideas?
There is a note on MSDN Control.ResolveClientUrl method description.
The URL returned by this method is
relative to the folder containing the
source file in which the control is
instantiated. Controls that inherit
this property, such as UserControl and
MasterPage, will return a fully
qualified URL relative to the control.
So the behavior of master page in your exampe is fully predictable (although this is not a very comfortable to work with). So what are the alternatives?
The best one is to set the <a> as a client control (remove runat="server"); should work like a charm even in a master page:
Example
In the case if this control should be server side only: you could just build an URL from your code behind by using UriBuilder class:
UriBuilder newPath = new UriBuilder(Request.Url);
// this will add a #notificationPopup fragment to the current URL
newPath.Fragment = "notificationPopup";
hyp1.HRef = newPath.Uri.ToString();
Create a hidden field on your form and set the value to where you want to navigate / the url of the hyperlink instead of the hyperlinks navigate url. Then call the onclick method of the hyperlink in javascript and set the hyperlink there before the browser does the actual navigation.
<html><head><title></title></head>
<script type="text/javascript">
function navHyperlink(field)
{
field.href = document.getElementById('ctl00_hdnHypNav').value;
return true;
}
</script>
<input type="hidden" id="hdnHypNav" value="test2.html" runat="server"/>
<a href="" onclick="navHyperlink(this);" >click here</a>
</html>
Code behind would be:
hdnHypNav.value = "#notificationPopup";
You could also just try setting the url after the postback with below code, i.e. replace your code behind line with this one but I am not sure if it will work...
ScriptManager.RegisterStartupScript(this,this.GetType(),"SetHyp","$('ctl00_hyp1').href = '#notificationPopup';",True)
I found another way to solve the problem.
hyp1.Attributes.Add("href", "#notificationPopup");
Seeing as the whole reason I replaced my static hyperlink with a runat="server" one was to benefit from automatic resource-based localization, none of these answers served my needs.
My fix was to enclose the hyperlink in a literal:
<asp:Literal ID="lit1" runat="server" meta:resourcekey="lit1">
Example
</asp:Literal>
The downside is if you need to programmatically manipulate the link, it's a bit more annoying:
lit1.Text = String.Format("Example", HttpUtility.HtmlAttributeEncode(url));

Why does ASP.Net rewrite relative paths for runat=server anchor controls?

I have my UserControls in a ~/Controls folder in my solution:
/Controls/TheControl.ascx
If specify the following:
<a id="theId" runat="server" href="./?pg=1">link text</a>
ASP.Net seems to want to rewrite the path to point to the absolute location. For example, If the control is on site.com/products/fish/cans.aspx the link href will be rewritten to read
<a id="munged_theId" href="../../Controls/?pg=1>link text</a>
Why does Asp.Net rewrite these control paths, and is there an elegant way to fix it?
I just want the anchor control to spit out exactly what I tell it to!!! Is that so hard?
EDIT:
I've basically done what Kelsey suggested. I knew I could do it this way, but I don't like adding markup in my code when I want something relatively simple. At least it solves the problem:
Aspx page:
<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
Code-behind:
var anchor = new HtmlGenericControl("a") { InnerText = "Previous" + " " + PageSize) };
anchor.Attributes["href"] = "?pg=" + (CurrentPage - 1);
anchor.Attributes["class"] = "prev button";
ph.Controls.Clear();
ph.Controls.Add(anchor);
As you can see by the amount of code needed for what is essentially supposed to be be a simple and light-weight anchor, it's not the most optimal solution. I know I could use a Literal but I figured this was cleaner as I'm adding more than one anchor.
I would be interesting in knowing WHY ASP.Net takes over and tries to fix my URL, though.
Why do you have runat="server" and no ID defined? Do you need to access it server side? If you remove the runat="server" everything will work as expected.
For more information regardinging how ASP.NET handles paths check out this MSDN article.
Edit: You can get around the problem then by using a Literal control and then outputing the raw <a href... to it.
Eg:
<asp:Literal ID="myLiteral" runat="server" />
myLiteral.Text = "link text";
Then you can set the visible property on the Literal however you want.
I know this is a bit of an old topic, but I was running into this problem as well and in the end went with a similar solution, but was able to save a few lines of code by doing this in the ascx:
<anchor id="myAnchor" runat="server" href="xxx">link text</anchor>
Then in the code behind, I referenced it using an HtmlGenericControl and can then do this:
myAnchor.TagName = "a";
// other properties set as needed
Anyway, I thought I'd post in case anyone else stumbles in here with the same issue.
Best bet is to make everything app root relative using the magic ~/ lead-in to the url. That tends to keep stuff straight.
There isn't a great answer to your question. ASP.NET is going to treat a relative path in a UserControl as relative to the path of the user control.
What you can do is in the code behind for your user control, set the HRef property of your anchor tag based on the Request.Path property. Then you can create URLs relative to the page.
Alternative is to use a literal like Kelsey was suggestion, or I would just try and map everything app relative with ~/ like Wyatt suggested.
Even a literal doesn't work using ICallBackEventHandler and RenderControl at least... I ended up hacking the tag back client-side :/ e.g in JQuery:
$('#munged_theId').attr('href', './?pg=1');

RegisterClientScriptBlock without form tag

After trying to understand why client code is not rendered in a page (injected by user control) I found this link, it turns out you must have a form tag for it to work (Page.RegisterClientScriptBlock did declare this but ClientScriptManager.RegisterClientScriptBlock which I use does not say anything regarding this).
I am using Visual studio 2005.
Does anyone know if this has been solved?
Edit:
To clarify, I want my control to add javascript code to the head section of the page without having to use the
<form runat="server"
I have tried adding it using:
HtmlGenericControl x = new HtmlGenericControl("script");
x.InnerText = "alert('123');";
Page.Header.Controls.Add(x);
But this did not work for me.
As far as I know this functions the same in current versions, you can test it very simply though.
Update
per discussion in the comments, the only "workaround" that I could think of would be for your to manually insert the script into the "head" section of the page on your own, using a runat="server" declaration on the Head element.
Got it!
My mistake was not doing it in the OnPreRender method (I used the Render method).
Now all that is needed is - like Mitchel Sellers wrote, set the header to runat server and than add to it's controls:
HtmlGenericControl x = new HtmlGenericControl("script");
x.InnerText = GetScriptSection();
Page.Header.Controls.Add(x);
Thanks for pointing me to the right direction!
The MSDN Page for registerclientscriptblock here says:
The client-side script is emitted just
after the opening tag of the Page
object's <form runat= server> element.
The script block is emitted as the
object that renders the output is
defined, so you must include both tags
of the <script> element.
If you do not want to include a form, than you will basically need to build your own implementation of it.
Minor clarification for anyone seeing this:
The form tag must have the runat="server" attribute set, e.g.
<form id="theform" runat="server">
Just placing a regular HTML form tag in the page will not help.

Resources