Why does flush in asp.net appear not to be workig? - asp.net

I call flush but the page just hangs for 5second (purposely) then renders completely. Why isnt it showing me the first part then the last?
Firefox 7 and chrome both do this
code file
using System;
namespace ABC
{
public class Test
{
static public void Apple()
{
System.Web.HttpContext.Current.Response.Flush();
System.Threading.Thread.Sleep(5000);
}
}
}
page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
hi
<form id="form1" runat="server">
<div>
starting
<% ABC.Test.Apple(); %>
<% WebApplication1._Default.RecurseMe(Response, #"/var/www/wordpress", 0); %>
</div>
</form>
</body>
</html>

I don't think a web browser will show the page before it's done loading the HTML. Think about it this way.. there are tags that need to be closed before the page can be rendered correctly and is a tag.
I'm not sure why you need to do this, but if you want to hide some data while it loads you should hide the area in a hidden div and then display it using javascript after the time interval or some other method such as an AJAX callback.

Related

Using server tags inside of a local ASP.NET page contained in an iframe

I have a class that modifies the path of a resource (e.g., a stylesheet) based on that file's last modified time. This is in order to prevent caching of stale resources.
The class works on all "normal" pages, but I can't get it to work in a local ASP.NET page that's rendered inside of an iframe. The class is located in ~/App_Code:
fingerprint.aspx.cs
public class Fingerprint {
public static string Tag(string rootRelativePath) {
if (HttpRuntime.Cache[rootRelativePath] == null) {
string absolute = HostingEnvironment.MapPath(rootRelativePath);
DateTime date = File.GetLastWriteTime(absolute);
int index = rootRelativePath.LastIndexOf('/');
string result = rootRelativePath.Insert(index, "/v-" + date.Ticks);
HttpRuntime.Cache.Insert(rootRelativePath, result, new CacheDependency(absolute));
}
return HttpRuntime.Cache[rootRelativePath] as string;
}
}
And in under most circumstances, it works successfully if called as follows:
somepage.aspx
<%# Page Language="C#" AutoEventWireup="true" Inherits="MainSite" CodeFile="somepage.aspx.cs" %>
<html>
<head runat="server">
<title>Title</title>
<link rel="stylesheet" href="<%# Fingerprint.Tag("/path/to/resource.css") %>">
<!-- other markup -->
</head>
<body>
<!-- other markup -->
</body>
</html>
But if I have an ASP.NET page that loads another ASP.NET page inside an iframe, it doesn't seem to get called. Example:
parent.aspx
<%# Page Language="C#" AutoEventWireup="true" Inherits="MainSite" CodeFile="parent.aspx.cs" %>
<asp:Panel ID="Container" runat="server">
<iframe id="testFrame" src="child.aspx"></iframe>
</asp:Panel>
child.aspx
<%# Page Language="C#" AutoEventWireup="true" Inherits="MainSite" CodeFile="child.aspx.cs" %>
<html>
<head runat="server">
<title>Title</title>
<link rel="stylesheet" href="<%# Fingerprint.Tag("/path/to/resource.css") %>">
<!-- other markup -->
</head>
<body>
<!-- other markup -->
</body>
</html>
I don't get any errors, but Fingerprint.Tag doesn't seem to even be getting called. I just get an empty string in the href portion of the <link> tag where the path to my stylesheet would normally be.
Is there a trick to getting server tags to work inside of local ASP.NET pages contained in iframes? I'm using ASP.NET WebForms (no other choice, client requirement).
What about changing <%# Fingerprint.Tag("/path/to/resource.css") %> to <%=Fingerprint.Tag("/path/to/resource.css") %>? Changing # to =.
As far as I know, <%# ... %> is more like a data binding syntax.

Simple ASP.NET with VB.NET - Could not load type

I am a c# guy but I need to get a small vb.net test harness working. For the life of me I can't get this to work, and can't discover why. Here's the aspx:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Source Page</title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
And here is the .vb:
Public Class _Default
Inherits System.Web.UI.Page
End Class
Always the error "Could not load type '_Default'." I even turned the folder into a true Application in IIS but it made no difference. What am I missing?
What am I missing?
Try the below two -
If your code-behind contains a namespace then modify your inherits attribute in Page directive as Inherits="namespace._Default".
If it doesn't, simply remove this Inherits attribute from Page Directive.
Changing the Codebehind attributename to CodeFile worked for me:
vb
CodeFile="Default.aspx.vb"
C#
CodeFile="Default.aspx.cs"

Creating a sub-title with a Master Page and ContentPlaceHolder

I have a site with a few pages, and I'd like the titles of the pages to be:
Foo - 1st Page
Foo - 2nd Page
Foo - 3rd Page
I've created a Master Page with the following code:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Foo.master.cs" Inherits="Foo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Foo - <asp:ContentPlaceHolder ID="SubTitle" runat="server"></asp:ContentPlaceHolder></title>
</head>
<body>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
And then each page looks like this:
<%# Page Language="C#" MasterPageFile="~/Foo.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="SubTitle" runat="server">1st Page</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
Page 2
</asp:Content>
When I load the page in the browser I expect the title to be Foo - 1st Page, but it's just 1st Page.
The html source is :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
1st Page
</title></head>
<body>
Page 2
</body>
</html>
What am I doing wrong?
You do not want to remove runat="server" from head; it'll create other problems.
Use the following method which is a default method in ASP.Net Web Forms Application in Visual Studio 2012.
<!-- Master Page -->
<head runat="server">
<title>Foo - <%: Page.Title %></title>
</head>
<!-- Content Page/xxx.aspx -->
<%# Page Title="Home Page" ...>
Turns out this question had already been asked: ASP.NET: Having common Page title in master page with each page adding page-specific title?
After reading the answers there I found that removing runat="server" from the head element in the Master Page did the trick.

Putting HTML in ASPX - possible?

I have a aspx page that begins like this:
<%# Page Language="C#" MasterPageFile="~/Main_MP_Teacher.master" AutoEventWireup="true" CodeFile="default.aspx.cs"
Inherits="Teacher_default" Title="Teacher Page" %>
I want to include html in this page also but when I put in the first line
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/strict.dtd">
I get a parse error. What am I doing wrong?
The parser error is probably because you're putting something other than <asp:ContentPlaceHolder> in the root of your ASPX page.
If you're using a MasterPageFile then the <!DOCTYPE should be at the start of the MasterPage.
This is unless you have a <asp:ContentPlaceHolder> right at the start of the MasterPage, which you can put the <!DOCTYPE into it.
MORE INFORMATION
The <!DOCTYPE should always be the very first thing in the HTML file, so normally your MasterPage would look something like this...
<%# Master Language="VB" CodeBehind="MyMaster.master.vb" Inherits="dev.MyMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/strict.dtd">
<html>
<head>
<asp:ContentPlaceHolder runat="server" id="myHeader"/>
</head>
<body>
<form id="form1" runat="server">
<asp:ContentPlaceHolder runat="server" id="myBody"/>
</form>
</body>
</html>
If for some reason you wanted to have a page specified doc type, then you could add a new placeholder at the start, with a default value...
<%# Master Language="VB" CodeBehind="MyMaster.master.vb" Inherits="dev.MyMaster" %>
<asp:ContentPlaceHolder runat="server" id="myDocType">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/strict.dtd">
</asp:ContentPlaceHolder>
<html>
...
</html>
And then in the page you want to override put the following (by NOT overriding, the original will be output instead)...
<%# Page Language="vb" MasterPageFile="MyMaster.master" Codebehind="MyPage.aspx.vb"
Inherits="dev.MyPage" Title="My Page" %>
<asp:Content runat="server" ContentPlaceHolderId="myDocType">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
</asp:Content>
...

ASP.NET Adding Javascript to page if page not secure

I'm trying to add some share this javascript in between the head tags of an asp.net page but only if the page is not secure (!Request.IsSecureConnection). How do I get the code in the head tags to check for secure connection and then write the javascript if not secure. I've tried using <% %> blocks and RegisterStartupScriptBlock and it's not working
UPDATE:
Was able to get it to work using this in the Page_Load
if(!Request.IsSecureConnection)
{
HtmlGenericControl Include = new HtmlGenericControl("script");
Include.Attributes.Add("type", "text/javascript");
Include.Attributes.Add("src", "http....");
this.Page.Header.Controls.Add(Include);
}
This works for me:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<% if (!Request.IsSecureConnection)
{ %>
<script type="text/javascript">
onload = function() {
alert('Page is not secure') };
</script>
<% } %>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<%if (!Request.IsSecureConnection)
{%>
<script ..........> </script>
<%}%>
This didn't work?
Update From your comments this didn't work. I'm guessing it has to do with something you are doing in your code behind. Did you try calling RegisterClientScriptBlock from your code behind? If you could post your aspx and code behind we might be able to help more.

Resources