开发者

How to register a VB.Net User Control with jQuery

开发者 https://www.devze.com 2022-12-27 22:00 出处:网络
Greetings and thank you in a开发者_StackOverflowdvance 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

Greetings and thank you in a开发者_StackOverflowdvance 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)


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.


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.

0

精彩评论

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

关注公众号