Click here to Skip to main content
14,879,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
javascript function not running inside contentplace holder

<asp:Content ID="Content6" ContentPlaceHolderID="cphIcerik" runat="server">
<script type="text/javascript">

      function ValidateDate(sender, args) {
          var selectedDate = sender.get_selectedDate();
          document.getElementById('HiddenField1').value = selectedDate;

<asp:TextBox ID="txtdate" runat="server" Width="200px""></asp:TextBox>
                   <asp:ImageButton runat="Server" ID="Image1" ImageUrl="~/images/calendar.png" />
                   <asp:CalendarExtender ID="CalendarExtender1" runat="server"  TargetControlID="txtdate"  PopupButtonID="Image1" OnClientDateSelectionChanged="ValidateDate"></asp:CalendarExtender>
                   <asp:HiddenField ID="HiddenField1" runat="server" />
                   <asp:Button ID="dummybtn" runat="server" Text="Click"  style="display:none" OnClick="dummybtn_Click" />

when I select a day from calender button click event will run and which date selected I know because there is some another codes in button click event and I will use selected date.
if I use normal webform <head><body> etc. this code run because javascript inside head tag other codes inside body tag there is no problem.

but I create a masterpage and webform from masterpage this code not running.
I think javascript function not running inside contentplaceholder there is no head tag there is no body tag etc.

How can I do please help.
Sinisa Hajnal 27-Nov-14 4:53am
Your assumption is wrong. You have head tag and body tag is just that they are positioned in master page. Your final page has both elements of master page and of content page. Something else is the cause.

1 solution

Maybe your calendar is not instantiated if you moved the script in the master page.

Debug: Open Developer tools, click on console.

Change your javascript function to:
function ValidateDate(sender, args) {
          var selectedDate = sender.get_selectedDate();
          document.getElementById('HiddenField1').value = selectedDate;


Check if anything is undefined (and you'll immediately see if the function is called by seeing the messages in the console).
Member-2338430 27-Nov-14 7:23am
ok I solve this problem but there is another problem

function dateSelectionChanged(sender, args) {
var selectedDate = sender.get_selectedDate("dd/MM/yyyy");

document.getElementById('<%= Page.Master.FindControl("cp").FindControl("HiddenField1").ClientID %>').value = selectedDate;
(document.getElementById('<%= Page.Master.FindControl("cp").FindControl("dummybtn").ClientID %>')).click();

selected date comes like that this is very bad :
Thu Nov 27 2014 00:00:00 GMT+0200 (GTB Standart Saati)
I wanto to tacke like that 2014-11-27

Sinisa Hajnal 27-Nov-14 9:12am
Date formatting is separate issue. Either use one of datetime libraries (I've tried moment.js) or use .getHours(), .getMinutes(), .getDate(), .getMonth() etc...methods on selectedDate and create your own string...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900