1

Given:

<input id="datepicker" type="text" />

Where the id = datepicker tells the javascript to attach all the datepicker code to the form element, how do I turn this into a server control?

For example:

<input runat="server" id="datepicker" type="text" />

Doesn't work because ASP.net generates it's own ID's.

Edit

<asp:TextBox runat="server" ID="dateTo" class="datepicker"></asp:TextBox>

Renders as

<input name="ctl00$mainContent$dateTo" type="text" id="ctl00_mainContent_dateTo" class="datepicker" />

And doesn't work!

4 Answers 4

5

I think it's a good practice to have a class which triggers a date picker to be created for everything that has it, such as 'js-date-picker'

then to avoid duplication of code, you can just write the following JS once:

$(document).ready(function() { 
    $(".js-date-picker").datepicker();
});

to get ASP.net to set a class on the textbox correctly so it uses this class, just add CssClass="js-date-picker" to the tag:

<asp:TextBox runat="server" ID="dateTo" CssClass="js-date-picker" />

Hope this clarifies things for you.

Sign up to request clarification or add additional context in comments.

Comments

3

It's not a good idea to bind a datepicker to an ID. better is to give it to the class (in asp.net: CssClass) then, multiple inputs can have datepickers. so in short, bind the datepicker to a class. This also fixes your asp.net problem about the ID's

<input id="aspid" class="datepicker" type="text" />

<asp:TextBox runat="server" ID="dateTo" CssClass="datepicker"></asp:TextBox>

then in your jquery selector:

 $(".datepicker").datepicker()

1 Comment

Setting class="datepicker" doesn't seem to work with jquery-ui
0

You need to reference the client ID of the control in the Jquery datepicker initialisation, like such:

$(function() {
    $("#<%=dateTo.ClientID %>").datepicker();
});

Seems to work :)

1 Comment

but it aint very extendable now is it... if you place the jquery binding code in your masterpage, then, to make a datepicker, the only thing you need to do is add the CssClass to your input and you're done.
0

Simple!! Use clientidmode="static" in your input text control.

Example- input runat="server" id="datepicker" type="text" clientidmode="Static"

This should solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.