tags:

views:

2150

answers:

4

I need to set a single property in a jQuery command using a value that is calculated in the code-behind. My initial thought was to just use <%= %> to access it like this:

.aspx

<script type="text/javascript" language="javascript">
$('.sparklines').sparkline('html', {
    fillColor: 'transparent',
    normalRangeMin: '0',
    normalRangeMax: <%= NormalRangeMax() %>
});
</script>

.aspx.cs

protected string NormalRangeMax() {
    // Calculate the value.
}

It smells odd to have to call from the ASPX page to just get a single value though. Not to mention I have an entire method that does a small calculation just to populate a single property.

One alternative would be to create the entire <script> block in the code-behind using clientScriptManager.RegisterClientScriptBlock. But I really don't like putting entire chunks of JavaScript in the code-behind since its, well, JavaScript.

Maybe if I end up having many of these methods I can just put then in a partial class so at least they are physically separate from the rest of the code.

What method would you recommend as being easy to understand and easy to maintain?

+1  A: 

Personally, I would use the <% %> method. This is what the view is for. I don't like the RegisterClientScriptBlock at all. If you ever move to MVC you will get used to the <% %> ... :)

BobbyShaftoe
+1  A: 

I ran into this problem a while back. I recommend <% %> for single variable stuff. I find the RegisterClientScriptBlock function useful only if I ever need the code-behind to determine which scripts to run.

Matthew Doyle
+3  A: 

The <% %> works fine. One thing that I do is set a value in a hidden field on the page (then writing the necessary javascript to extract that value), this is nice because I can change that hidden field via javascript and when/if the page posts back I can get that new value from code behind as well.

If you need to call the method on demand, you could do an jQuery AJAX call to a ASP.NET WebMethod to grab the data and re-populate the various options. You can find a good tutorial on how to do that here: http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Below is some sample code using the hidden field method (using the datepicker control, but you'll get the idea):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtCalendar" runat="server" />
            <asp:HiddenField ID="hfTest" runat="server" />
        </div>
    </form>

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;

    <script type="text/javascript" src="http://ui.jquery.com/latest/ui/ui.datepicker.js"&gt;&lt;/script&gt;

    <script type="text/javascript">
    var dateMinimum = new Date($("#<%= hfTest.ClientID %>").val());

    $(function() {
        $("#<%= txtCalendar.ClientID %>")
            .datepicker({
                minDate: dateMinimum
            });
    });
    </script>
</body>

And the code behind Page_Load method:

protected void Page_Load(object sender, EventArgs e)
{
    // Set Value of Hidden Field to the first day of the current month.
    this.hfTest.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).ToString("MM/dd/yyyy");
}
Jon Erickson
A: 

Rick has a nice article about passing server vars to client script

redsquare