0

I am new to C# and want to get my C# value in javascript

I found that by doing var javacriptVariable = "<%=CsVariable%>"; it should get the value CsVariable aut of my .cs file. However when I log it it logs <%=CsVariable%> as a string

How can I get the value out m C# file?

5
  • Sounds like you are not getting the value from C# and are just assigning that as a string. How are you logging and where is your javascript e.g. inline, in its own file etc.? Commented Mar 10, 2020 at 11:05
  • @majita that exactly whats happening. the part i use for testing this is var javacriptVariable = "<%=this.CsVariable%>"; console.log(javacriptVariable) Commented Mar 10, 2020 at 11:10
  • Are you using ASP.NET Web Forms? Commented Mar 10, 2020 at 11:27
  • @ErikT. no i am not using that Commented Mar 10, 2020 at 11:51
  • As far as I know that is just the syntax if you are using ASP.NET Web Forms. What kind of project do you have? Is it ASP.NET at all? Commented Mar 10, 2020 at 11:56

3 Answers 3

1

The Approach I usually take is to assign the C# value to a hidden input field in which you can obtain the value data from javascript. In ASP.NET (Which it appears you are using, correct me if I am wrong) you can do something like

<asp:HiddenField runat="server" id="hdnCsVariable" />

And then in the backend .cs file on page load

hdnCsVariable.Value = "My C# Value";

And finally in the JS since ASP.NET will (usually) generate some hideous id, you can use the "ends with" selector to get your hidden input like so

console.log(document.querySelector("input[id$='hdnCsVariable']").value); // logs "My C# Value"
Sign up to request clarification or add additional context in comments.

3 Comments

As for your last point, it's much more reliable to use the ClientID property from the control rather than hardcoding it like you've done.. <%= hdnCsVariable.ClientID %>
@Adriani6 is it possible to get this client id in a javascript file that does not have access to the aspx page?
Yes, attach the ID as a global variable and retrieve it in other JS files. There's many ways however OP does not mention a separate JS file.
0

While Dans solution will work, here's an alternative that I'd personally go for..

RegisterStartupScript

Example usage:

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "script", string.Format("var myVariable = {0};", 123), true);

The following will render whatever you put inside the 3rd argument as a script on the page, therefore I'd put it in a PostBack or perhaps inside onPageLoad.

Comments

0
  1. In Asp.net you can easily use
  2. Use Variable
  3. protected global::System.Web.UI.WebControls.TextBox MyTextBox;
  4. '<%= MyTextBox.ClientID %>'
  5. Use Function
  6. '<%=UI.ClassName.GetFunctionName%>'

Comments

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.