0

I have an aspx form called MyForm.aspx. In this form I had included a javascript file:

<script type="text/javascript" src="Scripts/MyForm.js"></script>

In MyForm.aspx.cs a have a property:

public string Username { get; set; }

How can I access this Username variable in the MyForm.js?

I tried in the following way but its not working: var username = '<%=this.Username%>'

6
  • 1
    What isn't working? Has the UserName property have a value at the time being used? Commented Jun 17, 2014 at 8:39
  • Yes it has. If I write the script in the .ascx, it is working, but in this .js file it doesn't. The username in javascript will have the value "<%=this.Username%>". Commented Jun 17, 2014 at 8:42
  • How is the javascript file related to your code file and it's property? Commented Jun 17, 2014 at 8:45
  • I think you can't, Javascript is only client-side scripting language Commented Jun 17, 2014 at 8:46
  • Do you need to pass only Username proeprty value from code-behind to your script, or some properties also? Show how the MyForm.js organized Commented Jun 17, 2014 at 8:50

2 Answers 2

1

Call the javascript from a server side generic handler file:

<script type="text/javascript" src="cogs/awesomejavascript.ashx"></script>

Output all of the javascript from the handler file:

public void ProcessRequest (HttpContext ctx)
{
        ctx.Response.ContentType = "text/plain";
        StringBuilder bild = New StringBuilder;
        bild.Append("var username = " + this.username);
        ctx.Response.Write(bild.ToString);

}

If you're not comfortable with handler files you could use an ascx file.

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

2 Comments

Doh! Thanks Patrick, edited but it looks like OP has his answer.
Yeah. For MVC it was a decent answer. Your file handler idea is good though. +1
0

The JavaScript engine will be unable to resolve the server <%=this.Username%> code in a JS file since the server tags are not part of its specification. These server brackets can be resolved only inside the aspx/ascx files by the ASP.NET engine.

You can create a function in the MyForm.js file that accepts as an argument the ID of the username field getUserName(userName){ return userName;} and call the function from the MyForm.aspx page like this:

[MyForm.aspx]

var username = '<%=this.Username%>';
getUserName(userName);

[MyForm.js]

getUserName(userName)
{
   return userName;
}

1 Comment

Your answer was a bit wrong but I edited it and it works. In MyForm.ascx I wrote a method: <script> function getUserName() { var username = '<%=this.Username%>'; return username; } </script> and in MyForm.js a I called it like: getUserName(). But thanks a lot for your help! :)

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.