views:

992

answers:

7

I'm trying to read the value of a C# property from my code behind file into some JQuery script (see below). The JQuery selector I've written accesses an ASP.Net GridView and then a CheckBox field within the gridview. Whenever a checkbox is checked or un-checked the code is hit, but I need to access the C# property from the code behind to take the appropriate action based on the value of the property.

    $(".AspNet-GridView-Normal > td > input").click(function() {

        //Need to access the C# property here

        //Take action here based on the value of the C# property

     });
+1  A: 

Well you can't.

You need to render the C# property in some element (perhaps a hidden field) and then look at it that way.

But explain further: What property are you trying to check?

Noon Silk
The property is just an int in the code behind, like this: public int AvailableInstalls { get; set; }
Russ Clark
Right, well do it the hidden field way. Mark Kanof has given you the exact code to do it.
Noon Silk
+4  A: 

This may be stating the obvious, but the code behind doesn't exist on the client side where your jQuery code is executing. What you could do is assign the value of the property to a hidden field on the server side so that when you need to check it with jQuery on the client side it will be available. So you might do the following on the client side.

Markup:

<asp:HiddenField ID="hfValueINeedToKnow" runat="server"/>

Code Behind:

hfValueINeedToKnow.Value = <some value>;

jQuery:

$("#<%= hfValueINeedToKnow.ClientID %>").val();

You might need to make some minor changes to support a value for each row of the grid, but hopefully this explains the general idea.

Mark Kanof
I'm having problems reading the value of the HiddenField, here is the JQuery code I'm using to access it: var availableInstalls = $("#<%= hfAvailableInstalls.ClientID %>").val();and I'm setting the value of the hidden field to "2" in the PageLoad method in the code behind, but I always get a value of "undefined" for the var AvailableInstalls when I walk through in debug mode. Any ideas what may be causing this?
Russ Clark
@Russ Clark - what is appearing in your HTML for the hidden input field, and the actual rendered value in the jQuery selector?
Zhaph - Ben Duguid
A: 

What I've done for this scenario in the past is to print out my code value into the markup and store whatever it is in a javascript variable, thus making a copy of it available to client-side code. This is a silly example, but hopefully it makes sense:

<%
 var messsge = "Hello World!"
%>

<html>
<head>
<script type="text/javascript">
 function ShowMessage()
 {
  var msg = '<%= message %>';
  if(msg)
   alert(msg);
 }
</script>
</head>
</html>
Nathan Taylor
+2  A: 

You mentioned in a comment that the value is an int. And I see it's also a public property in your codebehind. This is trivial now - you don't need to escape the value, nor access it in some round-about way, and you get type safety for free:

<script>
$(".AspNet-GridView-Normal > td > input").click(function() {

    var AvailableInstalls = <%= AvailableInstalls %>;

});
</script>
Crescent Fresh
Based on the later comment from OP "the variable will need to be decremented each time a checkbox is checked, and incremented each time a checkbox is un-checked" declare the JS variable outside of the function, but this should work nicely.
Zhaph - Ben Duguid
A: 

There isn't a really clean way to do this. Your best bet would probably be to use the ClientScript.RegisterClientScriptBlock functionality built into ASP.NET. Here's a good primer.

private int myValue;

protected void Page_Load(object sender, EventArgs e) {
    ClientScript.RegisterClientScriptBlock(typeof(Page), 
        "vars", "<script>var myParams = { p1: " + myValue + ", p2: 'My Name' };</script>");
}

This will put the supplied script on your page towards the top of the form. You can change that too. Obviously, it isn't the prettiest; you are essentially string concatenating a different language, but it will work, and for simple variable declaration isn't too rough on the eyes.

swilliams
A: 

ASP Embedded in JavaScript always makes me nervous from the perspective of script injection attacks and the inability to unit-test your JavaScript.

This build upon an ealier answer:

<script type="text/javascript">
$(".AspNet-GridView-Normal > td > input").click(function() {

    var AvailableInstalls = $("#MyHidden").val();

});
</script>

You could move it to a hidden variable:

<input type="hidden" id="#MyHidden" value="<%= AvailableInstalls %>" />

However this doesn't get around the problem of injection. So you could you could add a server-side hidden variable and set it from the Page_Load event function in ASP.NET.

(P.s. you also need the attribute type="text/javascript" in your script tag to make it valid HTML).

James Wiseman
A: 

If you will put your value into an asp:HiddenField with id hfValueINeedToKnow, the simplest way to retrieve this value client side is

var jsvar = $("[id$=hfValueINeedToKnow]").val();

So you can also place this code in a separate .js file.

tanathos