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.
Related
I got a Project where somehow an Method is called from another aspx.page. I want like to know how this work.
For instance, in my Foo.aspx I got this:
<script runat="server">
Sub ShowHint()
some code
End Sub
</script>
in Bar.aspx I got this:
<script runat="server">
ShowHint()
</script>
But how does this can even work? I dont get it.
You can use JavaScript (AJAX) to get the data from a different page.
It is easy to do with the jQuery Load function.
You can also define classes where you can define subs of function which you can call from every webpage.
Is the webpage inside of you project?
An easy example: I have a file test.aspx and I want to load some data from test2.apsx. To load the data I use jQuery.
Here is test.aspx
<%# Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
//$("#presenter").load("test2.aspx" ... = loads the content of test2.aspx into the div with id = presenter
//$("#presenter").load("test2.aspx #content" ... = loads onlay the content of the div with id = content from text2.aspx
//{ message01: "Hello", message02: "world" } = are the paramter I pass to test2.aspx
$("#presenter").load("test2.aspx #content", { message01: "Hello", message02: "world" }, function () {
//here you can place code which will run after the load is completet
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="presenter"></div>
</form>
</body>
</html>
Here is test2.aspx
<%# Page Language="VB" AutoEventWireup="false" CodeFile="test2.aspx.vb" Inherits="test2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="content" runat="server">
</div>
</form>
And the code from test2.asp
Partial Class test2
Inherits System.Web.UI.Page
Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
Dim msg01 As String = Request("message01")
Dim msg02 As String = Request("message02")
Me.content.InnerHtml = msg01 & " " & msg02
End Sub
End Class
Ok I got this. Just have to include my "Helper" aspx-file in to another aspx-File:
<!--#include virtual="somefilename"-->
I am trying to get get model in aspx page. How can i get it. is anything i have to do in config file? PLease help me.
<%# Page Language="C#" AutoEventWireup="false" CodeBehind="Report.aspx.cs"
Inherits="IBATechnologies.IBA.Main.Views.Report" %>
<%# page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
Model // here i cant find Model "the name Model is not in current
context"
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
i'm not sure but you can use DynamicViewPage instead ViewPage.
may be this link will help you.
https://learn.microsoft.com/en-us/aspnet/mvc/overview/views/dynamic-v-strongly-typed-views
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.
I'm trying to reset my form using javascript on client side. The code looks like this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>
<script type="text/javascript" >
function Reset() {
TextBox1.text = "";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Reset()" />
</div>
</form>
</body>
</html>
This of course isn't working, I get the error that Button1 is undefinded. I tried looking control's name within browser (by viewing page source) and using that instead of its ID but that didn't work either.
you need to get the value using getElementById
var mybutton= document.getElementById('Button1');
mybutton.value = ""
I advise you to use jQuery for your javascript code. It's a standard anyway.
After you reference jQuery, you may rewrite your JavaScript as follows:
<script type="text/javascript" >
function resetForm() {
$("#<%=TextBox1.ClientID %>").val("");
}
</script>
If you still do not want to use jQuery, then you need to access your element using its client ID like following:
<script type="text/javascript" >
function resetForm() {
document.getElemenyById("<%=TextBox1.ClientID %>").value = "";
}
</script>
Also, as #Jon pointed out, you need to either rename your OnClientClick value to resetForm() or rename your JavaScript function.
I tried to call my helloworld by just including javascript inside webform but when running it page is blank on both chrome and firefox. In firefox error is
"XML Parsing Error: no element found"
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="jquery01._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>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js">
</script>
<script type="text/javascript">
function helloWorld() {
$("#divSample").append("Hello World!!");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="divSample">
</div>
<script type="text/javascript"> helloWorld();</script>
</form>
</body>
</html>
I needed to add in codebehind:
protected override void Render(HtmlTextWriter writer)
{
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(),
"startup", "<script type=\"text/javascript\">helloWorld();</script>");
base.Render(writer);
}
In that case it works but I don't understand why I just can't use the 1st syntax why it's so complicated for such a simple stuff ?
I also tried the suggestion but it didn't work either:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="jquery01._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>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js">
</script>
<script type="text/javascript">
function helloWorld() {
$("#divSample").append("Hello World!!");
}
</script>
<script type="text/javascript">
$(document).ready(function () {
helloWorld();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="divSample">
</div>
</form>
</body>
</html>
Update: Seems ASP.NET can be unreliable in some circumstances with ajax / jquery ?
http://chiragrdarji.wordpress.com/2010/02/17/xml-parsing-error-no-element-found/
Try wrapping your helloWorld() call in jQuery $(document).ready syntax:
<script type="text/javascript">
$(document).ready(function(){
helloWorld();
});
</script>
Have you tried to run it in a debugger, like FireBug for FIreFox or Chrome's developer tools? (Haven't tried IE's yet... I copyied and pasted your code above and it worked fine for me...
try using a delay="delay" attribute on your script tag.
<script type="text/javascript" delay="delay">helloWorld();</script>
IE does not like if you try to modify the DOM structure when the html is still being rendered. this attribute tells the browser to delay script execution until it is done with the rendering.
Did you try getting rid of the Render override?
protected override void Render(HtmlTextWriter writer)
{
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(),
"startup", "<script type=\"text/javascript\">helloWorld();</script>");
base.Render(writer);
}