Adding 2 HTMLGenericControl script with src attribute not working - asp.net

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!

Related

Trying to get a response from xml on an iis7 server

I'm trying to display the version info in a web page on an IIS7 server. I really have no clue what I'm doing, but I would like this to be processed server side, and I'm assuming that means using some variation of asp. I know how to use php to do something similar, but that's not an option for this project. The xml document is coming from a local resource on the same server using the following url:
https://127.0.0.1:8443/webservice/rm-agent/v1/monitor/devices?scope%3Dequipment
and the output of the in chrome looks like this:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<DEVICES count="3" time="13-10-12 16:29:20">
<VIEW name="all" scope="equipment">
<DEVICE mac_address="88:E0:F3:20:08:B9" model="WLC2" system_ip="192.168.1.99/24" sw_version="8.0.3.6.0" location="""" name="WLC2" license="WLAN Access Points:4Adv Voice:1Mesh/Bridging:4High-Availability:1Spectrum Analysis:4" object-id="com.trapeze.appl.shared.mdl.Chassis: 28660" contact="" serial_number="KE3211500127"/>
<DEVICE mac_address="f8:c0:01:ab:54:c0" model="WLA532-US" system_ip="192.168.1.75" name="name-WLA1" object-id="com.trapeze.appl.shared.mdl.DistributedAP: 29143" serial_number="jb0212039600">
<RADIOS_INFO radio_1_type="802.11ng" radio_2_mac_address="f8:c0:01:ab:54:c1" radio_2_type="802.11na" radio_1_mac_address="f8:c0:01:ab:54:c0"/>
</DEVICE>
<DEVICE mac_address="ac:4b:c8:02:68:00" model="WLA532-US" system_ip="192.168.1.82" name="WLA9999" object-id="com.trapeze.appl.shared.mdl.DistributedAP: 167425" serial_number="jb0212294341">
<RADIOS_INFO radio_1_type="802.11ng" radio_2_mac_address="ac:4b:c8:02:68:01" radio_2_type="802.11na" radio_1_mac_address="ac:4b:c8:02:68:00"/>
</DEVICE>
</VIEW>
</DEVICES>
I really just need an html page that shows the sw_version from the first response element, so it would basically just be a page that says:
8.0.3.6.0
Another problem is that I'm forced to use a https url to request the info, but I don't have the ability to install a proper certificate, so the certificate needs to be ignored as well.
this is what I have tried so far:
<%# Page Language="C#" %>
<%# Import Namespace="System.IO" %>
<%# Import Namespace="System.Xml" %>
<%# ServicePointManager.ServerCertificateValidationCallback = delegate( object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors ) { return true; }; %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string url = #"https://127.0.0.1:8443/webservice/rm-agent/v1/monitor/devices?scope%3Dequipment";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(url);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<pre>
<asp:Literal ID="lit1" runat="server" />
</pre>
</div>
</form>
</body>
</html>
I can't get the load to ignore the certificate warning, and I get a parse error on that line.
Thanks #John Saunders for the help getting the request to ignore the certificate warning.
I was unable to get the XML to parse, I think because it was in a weird format from the source, or more likely because I have no clue what I'm doing, but I got it working, so that's all I care about :D
Here is the code that I finally used:
<%# Page Language="C#" %>
<%# Import Namespace="System.IO" %>
<%# Import Namespace="System.Xml" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate( object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors ) { return true; };
string url = "https://127.0.0.1:8443/webservice/rm-agent/v1/monitor/devices?scope%3Dequipment";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(url);
string xmlString = xmlDoc.OuterXml;
string testString = Regex.Match(xmlString, #"sw_version=""([^)]*)"" location").Groups[1].Value;
Response.Write("<center><h2>The Current Version Is:</h2><h1>"+testString+"</h1></center>");
}
</script>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Version</title>
</head>
<body>
</body>
</html>

Overwrite Page Render method .NET Framework 4.0

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?

ASP VBScript Redim Preserve Type mismatch - Scope of variable definition

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.

Getting some HTML code on response of ASPX page

I am making an application.In that application i have one blank aspx page.That application is deployed on IIS. And i am calling that aspx page from other machine.On the load event of aspx page i written code as
protected void Page_Load(object sender, EventArgs e)
{
Response.BufferOutput = false;
writer = Response.Output;
try
{
if (!Page.IsPostBack)
{
processRequest.ProcessReuest(Request, writer);
writer.Close();
}
}
catch(Exception ex)
{
LoggerWeb.Error(ex.Message,ex);
}
finally
{
processRequest = null;
}
}
Where processRequest.ProcessReuest is the method of another class within same project. In that method i am writing some string data on the response.Many a times i am getting correct data on response but sometimes i am getting some HTML data on response as follows:
<!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 method="post" action="obstreamer.aspx?PortNo=16387&Scode=8&SessionId=04052012073228202&Width=4&historyDirection=backword" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="
2012-05-04 19:34:18.994 MVisum Ob[3270:f803] [self.newdata length]=235
2012-05-04 19:34:18.995 MVisum Ob[3270:f803] RECEIVED DATA=/wEPDwULLTE2MTY2ODcyMjlkZIYNklWIHuNxLNApLWs+6QTn2Vt4n8THjCx316p9WOvX" />
<div>
</div>
</form>
</body>
</html>
I am not getting why that data comes on response. Please help me.Thanks in advance.
It sounds like a much simpler experience would be making these other things into UserControls and then including the relevant UserControl in this page. See http://msdn.microsoft.com/en-us/library/26db8ysc(v=vs.85).aspx

Ms charts : Area chart is not loading sometimes when using div.Load ,method to render a chart

I am trying to load mschart in my aspx page(SalesOverview.aspx) using jQuery.load() method,
I am loading another aspx page(ChartHandler.aspx) which accepts the parameters and render the chart .
but when trying to execute,the chart is not getting rendered sometimes.Instead i could see an image holder kind of thing in the page (Similar to what we see when we refer an invalid image url as an image tag src attribute value).
The code i am using is as below
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="SalesOverview.aspx.cs" Inherits="OmnexCRM_UI.SalesOverview" %>
<!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></head>
<body>
<form id="frmPag1" runat="server">
<div id="divGraph"> </div>
</form>
</body>
$(document).ready(function()
{
var startDate ="10/10/2009"
var endDate = "12/11/2009"
var height = "150";
var width = "730";
var strUrl = "ChartHandler.aspx?chartMode=salesOverview&startDate=" + startDate + "&endDate=" + endDate + "&height=" + height + "&width=" + width;
$("#divGraph").html("<div class='divLoadingProdReport'><img src='images/ajax-loader-gray.gif' alt='Loading...'/></div>").fadeIn(10, function() {
jQuery.ajax({
url: strUrl,
type: "POST",
processData: false,
contentType: "text/xml",
data: null,
success: function(data) {
$("#divGraph").fadeOut(200, function() {
$(this).html(data).fadeIn(100);
});
}
}); //ajax
});
});
In ChartHandler.aspx
<!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>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlSalesChart" CssClass="pnlSalesChart" Visible="false" runat="server">
</asp:Panel>
</div>
</form>
In ChartHandler.aspx.cs,In page load,i am reading the querystring values and building an area chart and adding that to the panel.
Couple of things:
You're rendering a whole page to ajax, you should just render your panel
You are doing a POST to your ChartHandler page, this should be a GET
You're requesting text/xml, yet you aren't doing anything with xml
You're requesting a webpage. Just use a generic handler (ashx) for stuff like this
Example code
HTML page
<form id="form1" runat="server">
<div>
<p>Test page</p>
<div id="renderPanel">
</div>
</div>
</form>
<script type="text/javascript">
$(document).ready(function() {
$.get("chart.ashx?propertyA=1&propertyB=2", function(data, textStatus) {
$("#renderPanel").html(data);
});
});
</script>
Create a generic handler (ashx) with the name 'Chart.ashx'
public class ChartHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//process querystring, which is in 'context.Request.QueryString'
context.Response.ContentType = "text/html";
PlaceHolder wrapperPanel = new PlaceHolder();
//add your chart here
wrapperPanel.Controls.Add(
new Image() { ImageUrl = "http://www.geenstijl.nl/archives/images/niekijkenw.png" });
//render control to HTML
StringBuilder stringBuilder = new StringBuilder();
StringWriter stringWriter = new StringWriter(stringBuilder);
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
wrapperPanel.RenderControl(htmlWriter);
context.Response.Write(wrapperPanel);
}
public bool IsReusable {
get {
return false;
}
}
}
Add in web.config under httpHandlers:
<add verb="*" path="~/Chart.ashx" validate="false" type="ChartHandler"/>

Resources