Archive

Archive for the ‘jQuery’ Category

jQuery – enable / disable (readonly) and how to reference .Net Server side Controls

September 8th, 2010 No comments

Ok, I am just starting to play around with jQuery and these are a couple of tips that I have found useful.

1. How to access .net server side controls
The code to get the value from a html input control <input id=”name”> might be as follow:
var name = $(‘#name’).val();

For a server side control like <asp:TextBox ID=”txtName” runAt=”sever” /> you can reference it as follows:
var name = $(‘#<%=txtName.ClientID%>’).val();

I have a user control for date selection called the DateSelector, and it has a control inside it called txtDate which stores the selected date.
If I want to get the value from within a user control like this I can do:

var dte = $(‘#<%=dteDateSelector.ClientID%>_txtDate’).val();

2. Disable a control
In this case I want to disable a text box from the txtName example above:
$(‘#<%=txtName.ClientID%>’).attr(“disabled”, true);
to clear it
$(‘#<%=txtName.ClientID%>’).removeAttr(“disabled”)

A problem with disabling controls in this way is that their values cannot be read in javascript, so another way is to use the “ReadOnly” attribute
$(‘#<%=txtName.ClientID%>’).attr(“readonly”, true);
to clear it
$(‘#<%=txtName.ClientID%>’).removeAttr(“readonly”)

Cheers

VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)

Categories: ASP .Net, jQuery Tags: