Asp.Net webform 4.5
I am referencing a script with a REV in a master page
<script src="<%# "/content/js/master.js?"+ RevID %>"></script>
RevID is a Public string in code behind.
This use to be in -head- section, and worked very well with
Page.Header.DataBind();
I now wish (as recommended) to move all scripts to the end of body.
when done, Page.Header.DataBind(); does not work anymore and I get src="".
Page.DataBind();
does work BUT it also re bind all control in child pages, so it is not a solution.
so how can I use
<%# ... %>
in the body section without
Page.DataBind();
?
As I have mentioned in the comment, if you want to use the code nuggets with # you will have to call DataBind method. Alternatively, if you have a public string field in code behind like this:-
public string RevID = "3";
Them you can simply access it like this:-
<script src="<%= "/content/js/master.js?"+ RevID %>"></script>
and this should work fine.
Related
I am passing parameter to HTML page with NodeJs router using:
res.render('dashboard', {ph_user: fname});
I can use this parameter in my HTML code with:
<div id="greeting">Welcome: <%= ph_user %></div>
However, i also would like to use this parameters with script on HTML page.
I am sure I can extract if from element by ID, but that doesn't seem like the most efficient way. What is the best way, to access/use parameter in script on HTML page.
Any guidance is greatly appreciated.
You can pass the variables like so:
<script type="text/javascript">
var ph_user = <%- ph_user %>;
// or window.ph_user = <%- ph_user %>
</script>
Be sure you understand the differences and risks between escaped and un-escaped variables. You can put this script above your other scripts and then it will be available to the other scripts.
So here's what I'm trying to accomplish:
I have a client facing page that loads, and when that happens I automatically run a series of 4 quick tests, if any of those tests fail I update the HCresults variable.
.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script language="javascript" src="script/XmlHttp.js?vers=<%=VersionInfo %>" type="text/javascript"></script>
<script src="client_scripts/JQuery/jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript">
var HCresults = "";
</script>
</asp:Content>
I want to access the value of HCresults after the page finishes loading from my code behind page. Is there a way to do this?
You can write a webmethod in your code behind;
pseudo code:
public static var HCresultsCS;
[webmethod]
public static void grabHCresults(var HCresultsfromJS)
{
HCresultsCS= HCresultsfromJS;
}
make an AJAX post to this webmethod with HCresults you're setting on a test failure as parameter;
Access the HCresultsCS from CS now. Check for nulls! I can't comment
This link might be helpful:
http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx
Unfortunately not using JS, you can store the value in a hiddenfield however to retrieve in C#. Make sure that you set the attribute runat = "server" for the control. I should also mention you'll use element.value = value to assign the value.
I've managed to get the following to execute properly, meaning that I've set the renering etc. correctly. I do get to see the alert window.
#{ Layout = "~/Views/Shared/_Default.cshtml"; }
<h1>Eye candies</h1>
#section Footie
{
<script type="text/javascript">
alert("Ola!");
</script>
}
However, when I change that to execute a different script, showing a cool globe with visitors, I see nothing, nada, ziltch! No error, no nothing. Just empty page.
When I paste the URL into a browser, I get a JS source code back, so I'm assuming that this part works. But I have no ideas left how to investigate it further.
#section Footie
{
<script src="#Url.Content("//ra.revolvermaps.com/0/0/5.js?i=0w5jyvctga6&m=0&s=180&c=ff0000&cr1=ffffff")"
async="async"
type="text/javascript"></script>
}
I've added a static HTML page and pasted in the script there. Works like a charm! So it's definitely related to script execution under a view in MVC (or possible Razor syntax).
Suggestions?
Take a look at the url you are passing to the Url.Content method
#Url.Content("//ra.revolvermaps.com/0/0/5.js?i=0w5jyvctga6&m=0&s=180
&c=ff0000&cr1=ffffff")
In the url, the & are already encoded to & Url.Content method is going to encode the & again and you will get the result as
"//ra.revolvermaps.com/0/0/5.js?i=0w5jyvctga6&m=0&s=180&
c=ff0000&cr1=ffffff"
Now you have & :)
The solution is to either not use the Url.Content() method
<script type="text/javascript" src="//ra.revolvermaps.com/0/0/5.js?i=0w5jyvctga6&
m=0&s=180&c=ff0000&cr1=ffffff" async="async"></script>
or do not encode the url passing to Url.Content() method let the method take care of encoding it.
<script type="text/javascript" src="#Url.Content("//ra.revolvermaps.com/0/0/5.js
?i=0w5jyvctga6&m=0&s=180&c=ff0000&cr1=ffffff")" async="async"></script>
I have a ASPX file to process some validation from my users. I need to prepear some codes and IDs for user to work with my data. I wrote a Validation.aspx file, checking everything about my users in Page_Load. I want to use some javascript functions from html files.
There are some javascript functions inside ASPX file, I make them in runtime from gathering data by validation.aspx Page_Load.
I want to put a script inside html files like this:
<script src="validation.aspx?a=1234" type="text/javascript" language="javascript" ><script/>
<script> RunValidationAnswer(); <script/>
The RunValidationAnswer(); function is made in runtime form user data (retriving from QueryString [a=1234] ). I can't access RunValidationAnswer(); in html files.
If i put the RunValidationAnswer(); in a JS file I can access it but I lose the powerfull operations inside Page_Load of aspx file.
plz help me to find a way to solve my problem.
I wrote this sample script in Validation.aspx
<script type="text/javascript" language="javascript">
function RunValidationAnswer()
{
alert("hi");
}
<script/>
It is compeletely accessible inside validation.aspx but I can't access this function from other files.
I want somthing like this inside other files:
<script src="validation.aspx?a=1234" type="text/javascript" language="javascript" ><script/>
<script> RunValidationAnswer(); <script/>
Did you set the content type of ASPX page to be javascript so the browser would know that is Javascript file, cause by default the content type of any aspx is HTML
Response.ContentType = "text/javascript"
** add ; for C#
Validation.aspx should be empty file except for this line
<%# Page Language="C#" AutoEventWireup="true" CodeFile="ScriptTest.aspx.cs" Inherits="ScriptTest" %>
And in code file, you write JS by response.write
public partial class ScriptTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/javascript";
Response.Write("alert('javascript works')");
}
}
You could also just use ASPX page -without code file- like this
<%# Page Language="C#" AutoEventWireup="true" ContentType="text/javascript" %>
alert("script works <%=DateTime.Now.ToString() %>");
I am using an open source wysiwyg editor on one of my asp.net web pages to create news pages... On one page It is put into place like this:
Registered at top of asp.net web page...
<%# Register Src="~/WebUserControls/HTMLEditorControl.ascx" TagName="HTMLEditorControl" TagPrefix="uc2" %>
Incorporated into the page:
<div>
<uc2:HTMLEditorControl ID="HelpTextBox" runat="server" />
</div>
In the code behind there is a Save method that basically saves the above editor data using the id:
dataset.column = htmlTextArea.GetHTML ;
When I try to bring up the page with the editor, I get the error: 'WYSIWYG' is undefined at Line 900, which is:
<script language="javascript" type="text/javascript" >
WYSIWYG.attach('ctl00_ContentPlaceHolder_HelpTextBox_htmlTextArea');
</script>
What's confusing, I have another page set up identically, that produces the same WYSIWYG.attach source, but it handles it with no problem at all. The only difference is the names of the pages. The page that works produces the following, with no problem:
<script language="javascript" type="text/javascript" >
WYSIWYG.attach('ctl00_ContentPlaceHolder_htmlTextArea_htmlTextArea');
</script>
So I'm at a loss...
Does the name of your code-behind class match the class name of your aspx page? Does the aspx page point to the correct code behind file?
My guess is that you copied and pasted but forgot to change that.