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.
Related
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.
`
function init() {
var a = 'output of my processing';
alert('I am here'); // alert pops up
document.getElementById("<%=hdnField.ClientID %>").value = a;
}
<head>
<script src="../Scripts/myscripts.js"></script>
</head>
<body onload="init()">
<asp:HiddenField ID="hdnField" runat="server" />
</body>
`I have a page with lot of javascript; I am trying to clean it up by moving the scripts to a script folder and reference the path; Seems to work fine, except when it encounters 'document.getelementbyid(controlname.id)'- it throws 'TypeError: Cannot read property 'value' of null'
I understand it is not able to find the control. Why does that happen? I thought the DOM is already built - what difference does moving the javascript to a path make to that anyway? Any ideas on how to make it work? I would really like javascript to be moved from the page.
You're using ASP.Net inline calls inside your JS. This couldn't work, for two reasons:
It's likely you don't have your server configured to handle .js files using the ASP.Net processor.
Even if you did, the processing of the .js would be completely separate to the hosting .aspx page; meaning hdnField would not be in scope.
You would be better off passing knowledge about the items on your page directly to the JavaScript:
JS:
function init(config) {
var a = 'output of my processing';
alert('I am here'); // alert pops up
document.getElementById(config.hdnFieldID).value = a;
}
ASPX:
<head>
<script src="../Scripts/myscripts.js"></script>
</head>
<body onload="init({ hdnFieldID: '<%= hdnField.ClientID %>' })">
<asp:HiddenField ID="hdnField" runat="server" />
</body>
Hope that helps.
This answer assumes your directory structure is correct.
Move your script tag to the bottom of the body, just before . Here is a good SO answer to this question, and here is another.
In addition, in general, it's bad practice to call a JavaScript function from inside HTML elements. If you're not using jQuery, you can add a "DOMContentLoaded" event listener to run the code. With jQuery, the standard $(document).ready() has been proven to work well. Or, if you simply put your script tag at the bottom of the , and place init(); at the end of your JS file, it will all run properly. This would be for a very simple application, but simplicity is sometimes the best.
Finally, for a sanity check, you could hard-code the ID in your init function. I don't know asp.net, but you might want to check the output of <%=hdnField.ClientID %>. Are you sure you're getting the correct ID?
Good luck.
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 have a script that I want only to load if a certain condition was met, so I figured I should do this:
//Head tag
<script type="text/javascript" id="scriptArea" runat="server"></script>
//Rest of the page
.cs (Page_Load event)
if(someCondition)
{
scriptArea.InnerHtml = "Javascript code";
}
The problem is I get a null pointer exception and when it stops, I discover that scriptArea is null for some reason. Why does this happen and do you know of another solution?
Using asp.net webforms and script runat="server" ends up being server executed code, see MSDN on documentation about this.
If you just want javascript, try this instead:
<script type="text/javascript">
<asp:literal id="scriptArea" runat="server" />
</script>
Then in your code behind
scriptArea.Text = "Javascript code";
I'm very new to the AJAX and Javascript world and I'm trying to implement Scott Hanselman's example of form submission to update part of a page. I have copied his example almost word-for-word and I can't seem to get it to work. When I click the submit button the controller action is called successfully but the result is rendered in the browser as a new page, instead of updating just the span that I specified in the Ajax form.
Here is my view code:
<asp:Content ID="indexHead" ContentPlaceHolderID="head" runat="server">
<title>Home Page</title>
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<% using (Ajax.BeginForm("TestAction", new AjaxOptions { UpdateTargetId = "target" }))
{ %>
<%= Html.TextBox("TextBox")%>
<input type="submit" value="Submit" />
<span id="target" />
<% } %>
</asp:Content>
And my controller action:
public string TestAction(string TextBox)
{
return TextBox;
}
And I have included the following lines in the master page
<script src="../../Scripts/MicrosoftMvcAjax.debug.js"type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
But all it seems to do is call the action and render the result as a new page, instead of updating the target span. Here are some small screenshots to illustrate what's happening.
Screenshot 1 http://martindoms.com/scr1.JPG
Screenshot 2 http://martindoms.com/scr2.JPG
Any ideas?
You have the javascript include in your master page in the wrong order.
Reorder so that MicrosoftAjax.js is included first of the three and it will work.
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
Just throwing some ideas out there...
http://www.asp.net/learn/MVC/tutorial-33-cs.aspx
Your controller action is in a class defined like so?
public class MyController : Controller
Apparently the name of the class has to end with the word controller.