Reading C# property into JQuery code

14,556

Solution 1

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.

Solution 2

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>

Solution 3

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?

Share:
14,556
Russ Clark
Author by

Russ Clark

Updated on July 23, 2022

Comments

  • Russ Clark
    Russ Clark almost 2 years

    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
    
         });