Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access jQuery Controls on Server Side?

Is there a library or framework built for invoking jQuery / Javascript functions from the server side?

For example, say I'm using a jQuery UI datepicker control on my page. The datepicker's format (e.g., MM/dd/yyyy) is configurable on a per-user basis. In this scenario I'd have to retrieve user preferences on the server, record the user-chosen format in a hidden control, send the HTML page to the client, and finally extract the format from the hidden control on the client side. Does any library exist that facilitates this long-winded process?

like image 968
S. Valmont Avatar asked Mar 20 '26 04:03

S. Valmont


1 Answers

I like to use custom attributes for this sort of thing.

<asp:TextBox runat="server" id="myDatePicker" ClientIDMode="Static" 
  format="MM/dd/yyyy" />

Render the attribute when you render the page based on the user settings, then the client code can just grab it whenever it's needed and apply as appropriate:

var format = $('#myDatePicker').attr('format');

I sometimes use a simple infrastructure for this purpose which can JSON+base64 encode objects to include in a generic "data" property, then unwind it at the client, to add more complex information to a control.

In other situations I just grab the settings when I need them from the client using a service called through ajax, but this way is more efficient, if you already know what it is when the page is rendered.

like image 104
Jamie Treworgy Avatar answered Mar 21 '26 18:03

Jamie Treworgy