I would like to capture page source of a webpage by overriding page render method.
I use the following code:
Dim pageSource As String = ""
Using sw = New StringWriter()
Using htw = New HtmlTextWriter(sw)
MyBase.Render(htw)
pageSource = sw.ToString()
End Using
End Using
The code works fine on .NET Framework 3.5 However, on .NET 4.0,
the pageSource variable includes strange tags as below:
<$A$>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
</$A$><$B$><head><$C$><title>
</title></$C$></head></$B$><$D$>
<body>
</$D$><$E$><form name="form1" method="post" action="Default2.aspx" id="form1">
<div>
</div>
<$F$>
<div>
</div>
</$F$></form></$E$><$G$>
</body>
</html>
</$G$>
Why do you think this happens?
Related
I have a classic ASP page, which includes a server side in the section
<!--#include file="../includes/DataTransferFunctions.asp"-->
Within this function, I have the following at the top of the library file,
Dim wsDataToXferArray()
and then, within one of the functions in the library, it does
redim preserve wsDataToXferArray(3,pSub).
This doesn't work as I get a type mismatch on the redim statement. However, if I have the Dim statement at the top of the main ASP instead of at the top of the include library it works.
I need to be able to declare the variable in a global scope so that is it available to more than one function within the library, but have it so that it is defined within the library code so that it is self-contained. I feel as though I'm missing something obvious.
Thanks.
Here are cut-down versions which show the problem. I have included the main ASP and the relevant library which shows the location of the '2 Dim' statements.
Thanks.
<%# language = vbscript %>
<% Option Explicit %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
Response.Buffer = true
Response.ContentType = "text/html"
Response.AddHeader "Content-Type", "text/html;charset=ISO-8859-1"
Response.CodePage = 1252
Response.CharSet = "ISO-8859-1"
Dim wsResult,wsDatabase
Dim wsSubX
'
'Dim wsDataToXferArray()
subRoutine()
'
'------------------------------------------------------------------------------------------------
sub subRoutine
'------------------------------------------------------------------------------------------------
wsSubX = 0
'
if wsResult = "" then wsResult = fncEnableSetAndCreateDataTransfer(wsDatabase,"LK-PART","abc","A",wsSubX) : wsSubX = wsSubX + 1
End Sub
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />
<title></title>
<%
SubIncludeOtherStyleSheets
SubIncludeJs
%>
<!--Include file containing functions to convert dates and to get sales order status description -->
<!--#include file="../includes/date_convert.asp"-->
<!--#include file="../includes/KHDataTransferFunctions.asp"-->
<!--#include file="../includes/IncludeStyleSheets.asp"-->
<!--#include file="../includes/IncludeJs.asp"-->
</head>
<body>
<form action="KHPartMaintenance.asp" method="post" name="form1" id="form1" >
<table class="tableForm Center Font7pt" width="1000">
<thead>
<tr>
<th colspan="5">Part Maintenance</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="submit" class="submit1" name="submit1" id="btnList" value="Get Part" onclick="javascript:return fncFormOnSubmit('Get');" /></td>
</tr>
</tbody>
</table>
</body>
</html>
This is the KHDataTransferFunctions.asp library.
<%
Dim wsDataToXferArray()
'------------------------------------------------------------------------------------------------
function fncEnableSetAndCreateDataTransfer(pDatabase,pName,pValue,pDataType,pSub)
'------------------------------------------------------------------------------------------------
'
Dim wsPrefix : wsPrefix = left(pName,2)
'
fncEnableSetAndCreateDataTransfer = ""
call subAddFieldToDataTransferArray(wsPrefix,pName,pValue,pDataType,pSub)
end function
'------------------------------------------------------------------------------------------------
sub subAddFieldToDataTransferArray(pPrefix,pName,pValue,pDataType,pSub)
'------------------------------------------------------------------------------------------------
'
' Build the array of the fields for each 'transaction'.
'
redim preserve wsDataToXferArray(3,pSub)
wsDataToXferArray(0,pSub) = pPrefix
wsDataToXferArray(1,pSub) = pName
wsDataToXferArray(2,pSub) = pValue
wsDataToXferArray(3,pSub) = pDataType
'
End Sub
%>
I've resolved the problem. It was caused by the position of the include files within the ASP page.
Here's the revised code (well, part of it ....).
<%# language = vbscript %>
<% Option Explicit %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
Response.Buffer = true
Response.ContentType = "text/html"
Response.AddHeader "Content-Type", "text/html;charset=ISO-8859-1"
Response.CodePage = 1252
Response.CharSet = "ISO-8859-1"
%>
<!--Include file containing functions to convert dates and to get sales order status description -->
<!--#include file="../includes/date_convert.asp"-->
<!--#include file="../includes/KHDataTransferFunctions.asp"-->
<!--#include file="../includes/IncludeStyleSheets.asp"-->
<!--#include file="../includes/IncludeJs.asp"-->
<%
Dim wsResult,wsDatabase
Dim wsSubX
'
'Dim wsDataToXferArray()
subRoutine()
'
'------------------------------------------------------------------------------------------------
sub subRoutine
'------------------------------------------------------------------------------------------------
wsSubX = 0
'
if wsResult = "" then wsResult = fncEnableSetAndCreateDataTransfer(wsDatabase,"LK-PART","abc","A",wsSubX) : wsSubX = wsSubX + 1
End Sub
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />
<title></title>
<%
SubIncludeOtherStyleSheets
SubIncludeJs
%>
Problem was caused by the location of the server side include files. These need to be before the main ASP code gets executed, so that any global variables are declared early enough.
See the block with the modified code in the original post for the solution.
If I have the following HTML:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<link href="base.css" rel="stylesheet"/>
</head>
<body>
<div class="caption">
Test
</div>
</body>
</html>
And I have this HTML as a STRING Called HtmlText, and I set the .htmlText property of an Adobe Air HTML Control to it something like this:
HTMLControl.htmlText=HtmlText;
Into what folder do I put 'base.css' so that it is applied to the content of the Adobe AIR HTML control?
I don't know if you can use external CSS in this context.
But there is another way: you could load/inject the CSS file using JavaScript:
var css_file:File = File.applicationDirectory.resolvePath("assets/thecss.css");
var css_string:String;
if(css_file.exists)
{
var file_stream1:FileStream = new FileStream();
file_stream1.open(css_file, FileMode.READ);
css_string = file_stream1.readUTFBytes(file_stream1.bytesAvailable);
}
var css_style:Object = html.htmlLoader.window.document.createElement("STYLE");
css_style.type = "text/css";
css_style.innerText = css_string;
html.htmlLoader.window.document.getElementsByTagName("head")[0].appendChild(css_style);
Adaptation of this.
I have several content pages hanging off of one master page. I need to add a refresh meta tag to one of the content pages but I can't see where I can do this.
Any help would be much appreciated.
Have not tried this with refresh, but in general you can add a meta tag like this:
var keywords = new HtmlMeta { Name = "keywords", Content = "one,two,three" };
Header.Controls.Add(keywords);
update: it is possible this way. Check Rick Strahl
http://www.west-wind.com/weblog/posts/2006/Aug/04/No-more-Meta-Refresh-Tags
This page explains the new feature: ASP.Net 4 adds 2 new Meta tag related properties to the Page. They can be used to set meta tags for keywords and description.
You can set them in the code behind:
Page.MetaKeywords = "keyword1, keyword2, keyword3";
Page.MetaDescription = "Example of new meta tag support in ASP.Net 4";
You can also set in the #Page directive:
<%# Page Language="C#" AutoEventWireup="true"
MetaKeywords="keyword1, keyword2, keyword3"
MetaDescription="Example of new meta tag support in ASP.Net 4"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
The ouput of either of these methods renders html similar to the following:
<!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>
ASP.NET 4 Meta Tag Support
</title>
<meta name="description" content="Example of new meta tag support in ASP.Net 4" />
<meta name="keywords" content="keyword1, keyword2, keyword3" />
</head>
<body>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = "Title of page";
HtmlMeta tag = new HtmlMeta();
tag.Name = "description";
tag.Content = "description of page";
Header.Controls.Add(tag);
HtmlMeta tagKeyword = new HtmlMeta();
tagKeyword.Name = "keywords";
tagKeyword.Content = "keywords of page";
Header.Controls.Add(tagKeyword );
}
(source url)
You can add a content place holder on the master page in the head section of the html. You can then add stuff to this content section in your specific content page and it will be outputted to the page header.
add below code in designer page
<meta id="metaDescription" runat="server" name="Description" />
Now add below code to your .cs page
Page.MetaKeywords = "keyword1, keyword2, keyword3";
Page.MetaDescription = "Example of new meta tag";
One way I found to do this (that I didn't see listed here) was to have a Literal and fill it with whatever kinds of meta tags you want. In my case, I needed to use it without a Master page, to have Facebook recognize a thumbnail image, title, and description:
<head runat="server">
<asp:Literal runat="server" ID="litMeta" />
...
</head>
CodeBehind:
var img = "<meta property=\"og:image\" content=\"thumbnail.jpg\" />";
var title = "<meta property=\"og:title\" content=\"Title\" />";
var desc = "<meta property=\"og:description\" content=\"Description\" />";
litMeta.Text = img + title + desc;
I have asp.net 2.0 c# application.
I have 2 scripts I want to add to a user control's control collection. Instead of adding them one after another, it only opens one script tag and throws the 2 src strings together as strings
string tagLinks = "/Resources/Javascript/js/taglinks.js";
HtmlGenericControl scriptTagLinks = new HtmlGenericControl("script");
scriptTagLinks.Attributes["type"] = "text/javascript";
scriptTagLinks.Attributes["src"] = tagLinks;
this.Controls.Add(scriptTagLinks);
script = new HtmlGenericControl("script");
script.Attributes.Add("type", "text/javascript");
script.Attributes.Add("src", gsJSHost);
this.Controls.Add(script);
This is what happens:
<script type="text/javascript">/Resources/Javascript/js/taglinks.jsvar pageTracker =
_gat._getTracker('UA-1213766-27'); pageTracker._initData();pageTracker._trackPageview();</script>
Here is what I've found out about your answer:
I added (basically) your code to my user control loaded event (which will inject the script tags that you're looking for). I don't have the global script that is contained in your gsJSHost global variable so I substituted with the variable name.
This is my control code:
Public Partial Class MyControl
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim tagLinks As String = "/Resources/Javascript/js/taglinks.js"
Dim scriptTagLinks As HtmlGenericControl = New HtmlGenericControl("script")
scriptTagLinks.Attributes.Add("type", "text/javascript")
scriptTagLinks.Attributes.Add("src", tagLinks)
Me.Controls.Add(scriptTagLinks)
Dim script As New HtmlGenericControl("script")
script.Attributes.Add("type", "text/javascript")
script.Attributes.Add("src", "gsJSHost")
Me.Controls.Add(script)
End Sub
End Class
This is my resulting page source:
<!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>
</title></head>
<body>
<form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUIMTI2NzczMzlkZE8ExRatvhvKqmX5DtpWJX8MhAcE" />
</div>
<div>
<script type="text/javascript" src="/Resources/Javascript/js/taglinks.js"></script><script type="text/javascript" src="gsJSHost"></script>
</div>
</form>
</body>
</html>
Please download the source code at my Google code demo project:
http://code.google.com/p/stackoverflow-answers-by-scott/
Direct link to zipped download:
http://stackoverflow-answers-by-scott.googlecode.com/files/1716701.zip
I hope this helps,
Thanks!
I am new to code and starting with ASP. How do I create a simple message box so I can alert the users on the web page?
<% response.write("<script language=""javascript"">alert('Hello!');</script>") %>
Here is one way of doing it:
<%
Dim message
message = "This is my message"
Response.Write("<script language=VBScript>MsgBox """ + message + """</script>")
%>
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
alert("Hello!");
}
</script>
</body>
</html>
Copy Paste this in an HTML file and run in any browser , this should show an alert using javascript.
If you want to do it from code behind, try this:
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('Message');", true);