In my C# Webforms project I have a RadDatePicker
control set to run a JavaScript
function. Each time I select a date, I get this Javascript error:
Uncaught TypeError: a[b] is not a function
Here is my code. The JavaScript is at the top of the ASPX page that has the control
function SetDateSession() {
console.log('here');
}
<telerik:RadDatePicker ID="StartDatePicker" runat="server">
<ClientEvents OnDateSelected="SetDateSession()" />
</telerik:RadDatePicker>
I'm getting the error after selecting a date, and the error occurs before the console log.
I can't figure out why this very simple code doesn't work. If I try to call the function with onclick
from an input
tag, it works.
<input type="button" id="RepBtn" onclick="SetDateSession()" value="View" runat="server"/>
I've simplified the code so much that it's hard for me to know what to try next.
Answer Summary:
Unlike HTML and ASP.NET, if you're using Telerik's OnClientClicked
and passing a JavaScript function, you should not provide the parenthesis. Providing parenthesis will cause the function to fire on page load and NOT on click.
For example, a RadButton generates this HTML:
<a id="exampleBtn" class="RadButton RadButton_Office2010Silver rbSkinnedButton"
href="javascript:void(0)">
<input class="rbDecorated" type="submit" name="btnStandard_input"
id="btnStandard_input" value="Standard Button" />
<input id="btnStandard_ClientState" name="btnStandard_ClientState"
type="hidden" />
</a>
It's an input with a type submit and when you refresh the page, it'll resubmit the form, running your JavaScript function.
What a waste of time on such a stupid thing! Basically this
<ClientEvents OnDateSelected="SetDateSession()" />
should be this
<ClientEvents OnDateSelected="SetDateSession" />
Lesson learned, Telerik! Thank you.