views:

65

answers:

2

I'm working on a .Net/C# web application and had been using a javascript based calendar to allow users to select dates. This has been working fine.

Today I changed the code to include a master page and the datepicker which is called from a childpage has stopped working, the problem being that the Textbox which the date is returned to is no longer valid, due to the fact the form declaration now sits on the master page.

The code is href="javascript:;" onclick="calendarPicker('form1.TBApplyDate');" title="Select Date from Calendar">Select

"Error Line: 1 Error: 'window.opener.document.form1.TBApplyDate' is null or not an object"

I've seen plenty of examples online of collecting the textbox client ID and passing that in the various permutations below, but I always get the same error.

href="javascript:;" onclick="calendarPicker('ctl00_MainContent_TBApplyDate');" title="Select Date from Calendar">Select


href="javascript:;" onclick="calendarPicker(form1.<%=TBApplyDate.ClientID%>');" title="Select Date from Calendar">Select


Can anyone provide any help on how I can reference the textbox succesfully?

Thanks in advance.

+1  A: 

It is because element IDs get "mangled" by the Master Page

I would recommend using jQuery and its selectors to help you grab the necessary controls

In jquery you can grab a control like this:

$('[id$=myContrl]')

Therefore you should be able to do this

<a href="javascript:;" onclick="calendarPicker($('[id$=TBApplyDate]'));" title="Select Date from Calendar">Select</a>

Edit: You can also try this

<a href="javascript:;" onclick="calendarPicker($('#<%= TBApplyDate.ClientID %>'));" title="Select Date from Calendar">Select</a>
jmein
Thanks for the pointer, unfortunately I've not been able to get it to work.I've referenced JQuery - <script type="text/javascript" src="../Scripts/jquery-1.4.2.js"></script> and edited the line as you've shown above. The page doesn't error anymore but selecting a date causes nothing to happen. The date pop up window stays open and the date is not transferred to the text box. Also tried "[id$=TBApplyDate.ClientID].I'm a complete newbie to Jquery - been googling all day trying various things out, but getting nowhere!
GordonK
@GordonK I have updated my answer. Hopefully it helps
jmein
using the ClientID is definitely the way to go here since that can be modified by Asp.Net anytime you make a change such as adding a master page, add the control to a panel, etc.
Chris Conway
Thanks for your help jmein - your answer got me in the right direction, however I eventually got it working with.. <script type="text/javascript"> $(document).ready(function() { $("input[name*='ApplyDate']").datepicker(); }); </script>
GordonK
A: 

Right click on your browser and view source and make sure the ID of the textbox and the ID specified in your jQuery routine are the same. If not, use @jmein's answer to get the correct client id of the text box.

If they are the same, then there's something wrong with how you have your jquery setup.

Chris Conway