I have a javascript src that i need to add to some of the pages in a site.
for example
<script type="text/javascript" src="http:abcxyz.com/zzz"></script>
I want to add this conditionally on a .ascx page - if the Request.ServerVariables["SCRIPT_NAME"] ends with certain criteria.
The ascx language is vb, and there is no code behind.
Thanks
If you make it so the tag is runat=server, you should be able to conditionally add the code as script:
<head runat="server">
<% If Request.ServerVariables("SCRIPT_NAME") = "value" Then %>
<script type="text/javascript" src="whatever.js"></script>
<% Else %>
<script type="text/javascript" src="whatever_else.js"></script>
<% End If %>
</head>
No code behind, but still code in front right?
This will probably do it...
<%= If(Request.ServerVariables("SCRIPT_NAME").EndsWith("<criteria>"), "<script type='text/javascript' src='http:abcxyz.com/zzz'></script>", "")%>
Related
I have a content page in ASP.Net web app.
In that I am trying to use Jquery UI Autocomplete. But it does not work. What is wrong in this page? Here is my code:
<asp:Content ID="Content3" ContentPlaceHolderID="contentMainBody" runat="server" >
<link href="Atlas_Css/menu.css" rel="stylesheet" type="text/css" />
<link href="Atlas_Css/jquery.simplyscroll.css" rel="stylesheet" type="text/css" media="all" />
<link href="Atlas_Css/Jquery%20UI.css" rel="stylesheet" type="text/css" />
<script src="JS_AtlasFly/jquery.js" type="text/javascript"></script>
<script src="Scripts/Validations.js" type="text/javascript"></script>
<script src="JS_AtlasFly/jquery.simplyscroll.js" type="text/javascript"></script>
<script src="JS_AtlasFly/jquery.simplyscroll.min.js" type="text/javascript"></script>
<script src="JS_AtlasFly/js-image-slider.js" type="text/javascript"></script>
<script src="JS_AtlasFly/accordian.pack.js" type="text/javascript"></script>
<script src="JS_AtlasFly/jquery-1.9.1.js" type="text/javascript"></script>
<script src="JS_AtlasFly/jquery-ui.js" type="text/javascript"></script>
<script type="text/javascript">
window.onload = function ()
{
var availableTags = [ <%= PassFname %> ];
$("#txtFname" ).autocomplete({
source: availableTags
});
}
</script>
<div id="testDiv" runat="server">
<asp:Label ID="lblName" runat="server" Text="F Name"></asp:Label> <asp:TextBox ID="txtFname" runat="server"></asp:TextBox>
</div>
NOTE: the server side variable, PassFname, I am populating in code behind .
In a normal asp.Net page , without using master page (within the same project), Autocomplete works fine.
What is wrong in this page? is it because of master page?
What is the solution for this?
Since the same code behind code works in another page, I am not including it here. I have checked it here also, the string variable is populated on page load. Only autocomplete is not binding.
Any help will be appreciated.
I found the solution to this. I used the clientId of the TextBox directly in
Javascript.
like this:
$("#ctl00_contentMainBody_txtFname").autocomplete({
source: availableTags
});
that solved the binding of autocomplete with the textbox's id.
BTW, using <%=txtFname.ClientID%> doesnot solve the issue. Instead the page stops rendering.
So I used the client Id from page source, and it works.
Thanks to all
I should be sleeping but this is really bugging me. I can't get a simple javascript alert box to display in my asp.net project. Hopefully someone can see what I'm doing wrong. My test page is this:
<%# Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="JSTest.aspx.cs" Inherits="Proj.JSTest" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Header" runat="server">
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript" language="javascript" >
$(document).ready(function() {
alert("Working");
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Main" runat="server">
</asp:Content>
The masterpage is pretty standard as well. Here's the header part of it (which I figure is the key bit)
<head runat="server">
<title></title>
<%--<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> --%>
<link href="App_Themes/Default/Default.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="Header" runat="server">
</asp:ContentPlaceHolder>
</head>
I know I must be missing something obvious - probably just need sleep :D. But if anyone can see why this isn't working, that would be great!
Thanks!
Try putting it in another script tag, but without the src attribute.
<script type="text/javascript" language="javascript" >
$(document).ready(function() {
alert("Working");
});
</script>
Or put it in a separate file and include the file using the src attribute.
<script type="text/javascript" language="javascript" src="myJSFile.js"></script>
You are encosing document ready inside the script tag which references jquery. Put that in another script tag like below.
<script src="Scripts/jquery-1.4.1.js" type="text/javascript" language="javascript" ></script>
<script type="text/javascript">
$(document).ready(function() {
alert("Working");
});
</script>
Should be like...
<script src="Scripts/jquery-1.4.1.js" type="text/javascript" language="javascript" ></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
alert("Working");
});
</script>
When you got a bug in javascript code, Firebug and Web inspector will be your best friend.
You can see what's wrong in the page. It might that jQuery library not loaded, the produced HTML contains error, missing tags, etc.
Using Firebug or Web Inspector, you can know what is wrong in the page.
include that script within
<script type="text/javascript>
$(document).ready(function() {
alert("Working");
});
</script>
I'm trying to use the Response.Write() method to dynamically insert content in the < head > section of an aspx page. I need to inject a string value from a property on a code-behind object which is a link to my CSS file. However, it is not being processed properly at run time. The object is public on the class and is hydrated in the Page_Load() event. Down in the body of the page I can successfully inject other properties from the Corpoartion object with no problem at all.
Why does this not work in the < head > section?
This is the part that does not expand correctly:
<link href="<%= Corporation.PageStyleSheet %>" rel="stylesheet" type="text/css" />
Here is the entire < head > section:
<head runat="server">
<title></title>
<link href="<%= Corporation.PageStyleSheet %>" rel="stylesheet" type="text/css" />
<script language="JavaScript" type="text/JavaScript" src="cntv_menu.js"></script>
<script language="JavaScript" type="text/JavaScript" src="cntv_category.js"></script>
</head>
What is the reason that this will not expand properly?
You can't use <%= %> inside a runat="server" tag, which your <head> tag is.
You can either change it to <%# %> and DataBind to it in the code-behind, or you can make the link tag runat="server", give it an id and assign the attribute from the code behind.
See this answer, which goes into the details.
Use this:
this.myButton.Attributes.Add(attribute, value);
It worked for me :)
the best way to resolve this problem is using OnPreRender
Example:
First, define your tag:
<link href="~/css/your_default.css" type="text/css" runat="server" id="myCSS" />
And on the OnPreRender:
protected override void OnPreRender(EventArgs e){
base.OnPreRender(e);
myCSS.Attributes["href"] = "~/css/your_new.css";
}
If you write out the full line you should be ok:
<%
Response.write("<link href=\"" + Corporation.PageStyleSheet + "\" rel=\"stylesheet\" />");
%>
P.S. My syntax may not be completely right, sorry in advance.
I have this in an ASP.Net Master Page:
<script language="javascript" type="text/javascript">
<asp:ContentPlaceHolder ID="scriptContentHolder" runat="server"></asp:ContentPlaceHolder>
</script>
But when I try to view the content page in design mode it tells me there is an error in the associated Master page because "scriptContentHolder" does not exist.
<asp:Content ID="scriptContent" ContentPlaceHolderID="scriptContentHolder" runat="server">
g_page = "mnuSurveys";
</asp:Content>
If I change the Master page to this:
<asp:ContentPlaceHolder ID="scriptContentHolder" runat="server"></asp:ContentPlaceHolder>
and the content page to this:
<asp:Content ID="scriptContent" ContentPlaceHolderID="scriptContentHolder" runat="server">
<script language="javascript" type="text/javascript">
g_page = "mnuSurveys";
</script>
</asp:Content>
Then all is cool. Why is this? The page compiles and executes just fine... but as above the designer squawks when placing ContentPlaceHolder controls within tags.
I had the same problem and solved it like that:
<%= "<script type=\"text/javascript\">" %>
jQuery(document).ready(function() {
// On document ready, execute this methods...
<asp:ContentPlaceHolder ID="jQueryOnDocReady" runat="server" />
});
<%= "</script>"%>
According to this MS Connect posting as of May '09, the
VS designer doesn't support controls
within script blocks. Alternately, you
can call
Page.ClientScriptManager.RegistgerClientScriptBlock
from content page
[sic]
So you'll have to use the second/work around method you posted.
this may be a bit off track. But I was having the same issue because i had some generic code i wanted in my Master page, and other more specific only on certain pages, here is my solution:
-In my .Master:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-X']);
_gaq.push(['_trackPageview']);
</script>
<asp:ContentPlaceHolder ID="PerPageScript" runat="server">
</asp:ContentPlaceHolder>
-In my .aspx:
<asp:Content ID="Content1" ContentPlaceHolderID="PerPageScript" runat="server">
<script type="text/javascript">
...
</script>
</asp:Content>
I know I can access the head section of a page which uses a masterpage programmatically this way (in code behind):
This is only an example (I'd like to insert scripts and styles etc.):
this.Header.Title = "I just set the page's title";
Is there a simple way to do this in a declarative way on in the aspx file itself?
Sometimes it would be handy to insert a client script or a style declaration or a link to an external resource.
You can do this by using content regions in the head, in exactly the same way as you would in the body of the page. eg, In your masterpage:
<head>
<link type="text/css" rel="stylesheet" href="/styles/common1.css" />
<script type="text/javascript" src="/scripts/common1.js"></script>
<asp:contentplaceholder id="ExtraStylesAndScripts" runat="server" />
</head>
And then in the page itself just something like:
<asp:content contentplaceholderid="ExtraStylesAndScripts" runat="server">
<link type="text/css" rel="stylesheet" href="/styles/extra1.css" />
<link type="text/css" rel="stylesheet" href="/styles/extra2.css" />
<script type="text/javascript" src="/scripts/extra1.js"></script>
<script type="text/javascript" src="/scripts/extra2.js"></script>
</asp:content>
For stylesheet you can use this :
HtmlLink cssRef = new HtmlLink();
cssRef.Href = "addins/main.css";
cssRef.Attributes["rel"] = "stylesheet";
cssRef.Attributes["type"] = "text/css";
Page.Header.Controls.Add(cssRef);
For Meta Tags :
HtmlMeta metaTag = new HtmlMeta();
metaTag.Name = "author";
metaTag.Content = "ScarletGarden";
Page.Header.Controls.Add(metaTag);
But there is no way to add external script files to header element.
You can add inside body element by :
if (!ClientScript.IsClientScriptIncludeRegistered("myExternalScript"))
{
ClientScript.RegisterClientScriptInclude("myExternalScript", "js/myJSFile.js");
}
Hope this helps !
You can declare the page title in the content page declaration.
<%# Title="Page Title" Page Language="C#" AutoEventWireup="true" CodeFile="Subpage.aspx.cs" Inherits="Subpage" MasterPageFile="~/MasterPage.master" %>
I haven't tried this.
But you can put HEAD element inside html with the enclosed string in asp style markup.
e.g.
<%=myTitle%>