1

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?

2
  • Do you need this in a server form post? Do you have an event handler defined? Commented Aug 10, 2016 at 19:41
  • I know absolutely nothing about either of those. Today is my second day of dealing with web-dev in my internship. I don't have an event handler defined though, I can tell you that Commented Aug 10, 2016 at 19:48

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

I think you may have it backwards? I'm trying to access a variable in my .aspx file from my .cs file
@jlee I realized that now. Answer is updated with the right context.
where does that value assignment occur? I assume you're talking about the value of the hiddenfield?
@jlee Yea, I'd have the assignment actually happen after the testing is over and you have issues. Yes to the second question, document.getElementById('hiddenfieldname').value = HCresults.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.