How to get datepicker value in javascript

Posted by Rrana under ASP.NET on 9/29/2011 | Points: 10 | Views : 4372 | Status : [Member] | Replies : 4
Hi All,

I used usercontrol for datepicker in my application.
In another aspx page i used that datepicker and i want to get that datepicker value in javascript.



Thanks in advance

Sucharitha Goud
Bank Of America..



Responses

Posted by: Vuyiswamb on: 9/29/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Did you use a Calender Control to create a Data-picker and bind the selected date into a Textbox, ? Please post the Date-picker user-control html

Thank you for posting at Dotnetfunda
[Administrator]

Rrana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rrana on: 9/29/2011 [Member] Starter | Points: 25

Up
0
Down
<table>
<tr>
<td style="height: 21px">
<asp:TextBox ID="txtDatePicker" runat="server" CssClass="ReadonlyTextBox"
AutoPostBack="True" ontextchanged="txtDatePicker_TextChanged"></asp:TextBox></td>
<td style="height: 21px">
<asp:LinkButton ID="lnkDatePicker" runat="server">
<img id="imgDatePicker" runat="server" border="0" alt="Date Picker" src="../Image/calendar.gif" style="height: 15px" />
</asp:LinkButton>
<asp:RequiredFieldValidator ID="rfvDate" runat="server" ControlToValidate="txtDatePicker"
Display="None" ErrorMessage="Please Select Date!" SetFocusOnError="True"></asp:RequiredFieldValidator></td>
</tr>
</table>

Sucharitha Goud
Bank Of America..

Rrana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 9/29/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
So now it means that you when the calender is selected you assign the value of the selected date to "txtDatePicker" and now you want to access the selected Value from the main page. you need to create a property that can be accessed via your usercontrol.

Public string SelectedDate

{
get
{
return txtDatePicker.text;
}

}


and in the page that host the Usercontrol you can do something like this

DateTime myselectedDatet = Convert.ToDateTime(MyUsercontrol.SelectedDate.Value);



and this should get you a selected Date. i wrote this here , so it might not compile , i just gave you an idea on how you could do this.



Thank you for posting at Dotnetfunda
[Administrator]

Rrana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rrana on: 9/30/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks for your response.

I want to validate the date, so i need txtDatePicker.text value through javascript.

==================javascript function


var stdate = document.getElementById("<%=hdn1.ClientID%>").value ;
var eddate = document.getElementById("<%=hdn2.ClientID%>").value;



var dt1 = parseInt(stdate.substring(0, 2), 10);
var mon1 = parseInt(stdate.substring(3, 5), 10);
var yr1 = parseInt(stdate.substring(6, 10), 10);
var dt2 = parseInt(eddate.substring(0, 2), 10);
var mon2 = parseInt(eddate.substring(3, 5), 10);
var yr2 = parseInt(eddate.substring(6, 10), 10);
var startdate = new Date(yr1, mon1, dt1);
var enddate = new Date(yr2, mon2, dt2);

var StartHr = document.getElementById("<%=ddStartHr.ClientID%>").value;
var EndHr = document.getElementById("<%=ddEndHr.ClientID%>").value;
var StartMin = document.getElementById("<%=ddStartMin.ClientID%>").value;
var EndMin = document.getElementById("<%=ddEndmin.ClientID%>").value;

if (startdate > enddate)
{
alert('End Date should not be less than Start Date');
return false;
}
else if (stdate == eddate)
{
if (StartHr - EndHr < 0)
{
return true;
}
else if (StartHr - EndHr > 0)
{
alert('End Time cannot be less than Start Time !!');
return false;
}
else if ((StartHr == EndHr) && (StartMin == EndMin))
{
alert('Start Time and End Time cannot be Same !!');
return false;

if (StartMin - EndMin > 0)
{
alert('End Time cannot be less than Start Yime !!');
return false;
}
if (StartMin - EndMin < 0)
{
return true;
}

}
}

else if (startdate < enddate)
{
if (StartHr - EndHr == 0)
{
if (StartMin - EndMin < 0)
{
return true;
}
if ((StartHr == EndHr) && (StartMin == EndMin))
{
alert('Start Time and End Time cannot be same !!');
return false;
}
if (StartMin - EndMin > 0)
{
alert('Start Time cannot be greater than End Time !!');
return false;
}

}
}

}

====================
i assigned datepicker value to hidden field.(hdn1 is a hidden field).

But, i don't want to assign datapicker value to hidden fields. i want to directly get that datepicker value through javascript.


Sucharitha Goud
Bank Of America..

Rrana, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response