Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 108219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:52:38+00:00 2026-05-11T01:52:38+00:00

I need to set a single property in a jQuery command using a value

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:52:39+00:00Added an answer on May 11, 2026 at 1:52 am

    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'> <html xmlns='http://www.w3.org/1999/xhtml'> <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'></script>      <script type='text/javascript' src='http://ui.jquery.com/latest/ui/ui.datepicker.js'></script>      <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'); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 95k
  • Answers 95k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer OK, I got it all sorted now. Here is the… May 11, 2026 at 6:57 pm
  • Editorial Team
    Editorial Team added an answer With appropriate cross-database notations for table references if appropriate: INSERT… May 11, 2026 at 6:57 pm
  • Editorial Team
    Editorial Team added an answer Just set ZipFile = The location of the zip file,… May 11, 2026 at 6:57 pm

Related Questions

I have what is essentially a jagged array of name value pairs - i
I need to find a way of absolutely centering the content of a LayoutPanel
I have a WinForms application with a view where the user selects a single
I have a situation where I need to do an update on a very

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.