0

I have a C# Property CategoryID, I want to set it's value in Javascript.

I am trying to set the value CategoryID like below:

var sPath = window.location.pathname;
var catId = null;

var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
if (sPage == 'xyz.aspx')
{
    <%=CommonUtility.CategoryID=4%>;
}
else if(sPage == 'zxy.aspx')
{
    <%=CommonUtility.CategoryID=5%>;
}

But by this method I always get the value of CategoryID= 5(which is in else block) .

Please suggest me how can get the Property value based on condition.

Omar
  • 16,329
  • 10
  • 48
  • 66
Vijjendra
  • 24,223
  • 12
  • 60
  • 92

3 Answers3

4

You can't set a C# property from a client-side (js). You may use ajax to do some work, but you simply can't manipulate server-side code.

edit:

if you still wonder how it's possible you get a value, see Mike's explanation of that fact. But the truth remains. You can't. It's impossible. If you want to know the longer explanation, see how asp.net actually works, it's lifecycle etc. Simple way of putting it would be like this:

A user sends a request to the server using his browser. The server receives it, creates a requested page and instantiates needed classes etc. Then it's gets parsed and sent to the client as html (and other resources of course, like images, css...). The instantiated page class CAN'T be accessed and modified afterwards by the client, because it's already flushed by the server. Every request creates a new instance. There's no way of interacting js with c# anyway. Can you imagine what it would be like, if you could use some js to modify C# on a remote server? It doesn't make sense at all.

walther
  • 13,466
  • 5
  • 41
  • 67
  • I can set the value, but the problem is that I always get the value which is set in else block,If I am removing the else block then it gives me value which is written in if block. – Vijjendra Aug 17 '12 at 18:53
  • 2
    @Vijjendra, with all due respect, you can't. Javascript operates in a client's browser, while C# runs on a server-side. There's no way your js could alter the C# values. – walther Aug 17 '12 at 18:55
  • 1
    @Vijjendra You're setting the value in server side code before the page is event sent to the client (which is when the javascript can *start* running). You're always getting 5 because *both* statements are being run (there is no server side conditional) and the second one is later, so `CategoryID` is set to 4, and then 5. – Servy Aug 17 '12 at 18:56
  • +1 This is the classical problem of frameworks like php,asp.net,mvc etc. People gets easily confused with which code runs at server and which on client. – L.B Aug 17 '12 at 19:00
  • @L.B I think you mean classic ASP. With the ASP.NET style of coding all of the server side code is in the code behind, and all of the client side code is in the markup file. It's one of the nice things about it. – Servy Aug 17 '12 at 19:01
1

You cannot set properties in your code-behind using client-side script this way. The only way to do something like that would be to use AJAX to send data to your server, although I'm pretty sure that's not appropriate for your case.

When you call <%=CommonUtility.CategoryID = 4%>, the server actually executes that statement when it is parsing the page before it sends it to the client. The reason that the property value is 5 is that both of those statements get executed, regardless of the logic in your Javascript if block. Your client side code will not actually be executed by the browser until the server has already parsed both of those tags, which at that point it would be too late to accomplish what you want anyways.

Is there any reason that you simply can't do all of this in the code-behind on page load? Is there some reason you feel like this has to be handled in JS?

Edit:

If you are unable to access the code-behind file (.aspx.vb or .aspx.cs) then simply use a server script block in the top of your .aspx page

<% 
    If (Request.Path.ToLower().Contains("xyz.aspx")) Then
        CommonUtility.CategoryId = 4
    ElseIf (Request.Path.ToLower().Contains("zxy.aspx")) Then
        CommonUtility.CategoryId = 5
    End If
%>
mclark1129
  • 7,532
  • 5
  • 48
  • 84
0

You can't set the C# variable from client script, because all the server code runs first, then the page is sent to the browser.

The client code will end up looking like this:

var sPath = window.location.pathname;
  var catId = null;

  var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
  if (sPage == 'xyz.aspx')
  {
    4;
  }
  else if(sPage == 'zxy.aspx')
  {
    5;
  }
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005