2

I am using ASP.NET to develop a website and I am going to use the jQuery datepicker in my website. I used following code, but it won't work. Does anyone know why?

<link href="css/calendar/jquery-ui-1.8.7.custom.css" rel="stylesheet" type="text/css" />
<script  src="jquery/calendar/jquery-1.4.4.min.js" type="text/javascript"></script>
<script  src="jquery/calendar/jquery-ui-1.8.7.custom.min.js" type="text/javascript"></script>


<script type="text/javascript">
$(function() {
  $("#txtEventDate").datepicker();
}); 
</script>

Here is my textbox HTML code:

<asp:TextBox ID="txtEventDate" runat="server" Width="125px"></asp:TextBox>

4 Answers 4

13

Perhaps the ID of the textbox isn't "txtEventDate" - it might be "ctrl00-ucDatePicker-txtEventDate" or something as asp.net tends to generate the IDs on the fly.

My suggestion is to either select it via a class you have assigned to the box, or

<asp:textbox id="txtEventDate" runat="server" cssclass="date-picker" />

then apply use a class selector rather than an ID selector

$(".date-picker").datepicker()

or if the javascript is in the page you could consider...

$("#<%= txtEventDate.ClientID %>").datepicker()

or you could try a partial ID picker

$("input[id*=txtEventDate]").datepicker()

There's three solutions for you!

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

1 Comment

The class selector didn't work for me, the other 2 however did. +1
0

try this for your javascript :

$(document).ready(function() {
  $("#txtEventDate").datepicker()
}); 

It could be really easier to help if you post the relevent part of the HTML code too (the one containing the element with the txtEventDate id).

Comments

0

Have you included the latest jQuery file, in addition to the jQuery UI? It's not obvious from your code.

You've not included any of the generated HTML either, but have you made sure the text input you have in your HTML code has the correct id set?

Also, you should try and make use of the .ready function so that the code only fires when ther DOM is fully loaded:

$(document).ready(function() {
  $("#txtEventDate").datepicker()
});

EDIT: You've now altered your code to show the inclusion of jQuery, but you haven't pasted the relevant HTML code as generated by ASP. I suspect that the issue is the input id is random generated and does not match #txtEventDate.

ANOTHER EDIT: You've now included the ASP code, but you still havent't included the generated HTML source for the input - as noted by most of the answers, ASP will likely include other information in your ID tag which is preventing jQuery from recognising it.

Comments

0

If you try use this datepicker in asp.formview in edit mode the problem is in FriendlyURL, the solution you make find Here.

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.