0

I have a website I need to find the id of my selected profile and assign that value to a variable in javascript and show that in an alert box where should i write the function which javascript needs to call in the app_code folder'class or a web page's code file

var some_variable = '<%=the_method()  %>';
Steve B
  • 36,818
  • 21
  • 101
  • 174
Amanjot Singh
  • 101
  • 1
  • 1
  • 12
  • where do i need to write "the_method()" in my server side code? moreover please tell how to pass an argument to this method which goes from client side – Amanjot Singh Jul 06 '12 at 08:02

2 Answers2

0

I'm going to assume ASP.NET WebForms and not MVC...

if you have in your page.aspx a variable named:

public string myProfile = "profile1";

you will be able to pick that up in your HTML just like anything else:

<% Response.Write( myProfile ) %>

in javascript you can do the same:

<script>
    var myProfile = '<%= myProfile %>';
</script>

You can also call methods:

in your page.aspx

public string getUserName() {
    return String.Format("{0} {1}", User.fname, User.lname);
}

in your javascript

<script>
    var myProfile = '<%= getUserName() %>';
</script>

Remember that you need to decorate your variables and methods publicly, as if not, by default they will be private and not accessible from outside the scope of your code behind file.

balexandre
  • 73,608
  • 45
  • 233
  • 342
  • how to pass an argument to this method which goes from client side – Amanjot Singh Jul 06 '12 at 08:06
  • you can go from asp.net to javascript but not from javascript to .NET this way... you need to invoke a URL and pass the value. That tutorial you can [find in my answer](http://stackoverflow.com/a/6330501/28004) to other question. Let me know if you have any troubles. – balexandre Jul 06 '12 at 08:09
0

You should use Webservices or Webmethods to communicate with the server side methods from your JavaScript code

yogi
  • 19,175
  • 13
  • 62
  • 92