views:

139

answers:

2

Greetings and thank you in advance for the help. I created a user control in VB.NET that uses a jQuery datepicker. I am at an impasse. The code I have works for one datepicker control on the page but because I can only register the client script once, it will not work for multiple instances/datepickers on the same page. What is the best way to go about initiating the jQuery script for this control so I can use it multiple times on one page? Can I append to ClientScript.RegisterStartupScript? Thanks again.

Dim sJS As String = "$(function() { " & vbCrLf
sJS += " var dates = $('#" & txtDate1.UniqueID
sJS += "').datepicker({" & vbCrLf
sJS += " changeMonth: '" & _bMonthDropDown & "', changeYear: '" & _bMonthDropDown
sJS += "', numberOfMonths: '" & _numberOfMonths
...
sJS += "'}); });" & vbCrLf
Page.ClientScript.RegisterStartupScript(GetType(Page), "DatePicker", sJS, True)
A: 

Hey,

If you are using MS AJAX, you can always add this to the user control, which will ensure only one entry gets registered:

<asp:ScriptManagerProxy id="asm" runat="server">
   <Scripts>
      <asp:ScriptReference Path="~/jquery.datepicker.min.js" />
   </Scripts>
</asp:ScriptManagerProxy>

And duplicates are ignored. Or, you can have the user control let the page do the registration. Lastly, you could add a property to the user control that says registerdatepicker, which when true, the user control uses the page.clientscript object to register the script.

Brian
At this time we need the code to work with IE6 without ActiveX AJAX isn't something we are ready to implement for this usercontrol. Thank you for the method though and I will refer back to this option once its deemed acceptable to celebrate the death of IE6 at our office.
BornReady
A: 

You can make just a minor change to get the behavior you want. Just change this:

Page.ClientScript.RegisterStartupScript(GetType(Page), "DatePickerVars", sJS, True)

To have something unique for a key, like this:

Page.ClientScript.RegisterStartupScript(GetType(Page), "DatePickerVars" + txtDate1.UniqueID, sJS, True)

That key (combined with the type) is what determines if this script is already registered, which you'd want for example if you had 30 of a user control and only wanted to run a certain script once. In this case though, you want each user control to register the script that's unique to it, so give it a key that won't be found as already being registered for this page.

Nick Craver
Unfortunately, VS likes to insert this client-side javascript anywhere it wants in the document and not in the header which interfered with initialization.
BornReady
Looking over the code I submitted, this is the best solution for what I was trying to do.
BornReady