开发者

jQuery datepicker not working in ASP.NET page

开发者 https://www.devze.com 2023-02-13 03:50 出处:网络
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 any开发者_StackOverflow中文版one know why?

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 any开发者_StackOverflow中文版one 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>


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!


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).


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消