As long as you have the calendar control in place you shouldn't need to declare it. Daft question, are you using the same spelling; reason for asking is that you have called it calender2, but the correct spelling would be calendar2. A common mistake is to allow Visual Web Developer (or whatever tool you are using) assign a name, and then you mis-spell it (I daren't count the hours I have spent looking for an error only to find its a typo!)
This code works (obviously don't know whether it is at all what you are wanting to do, but does demonstrate a working framework):
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub btn_dob_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_dob.Click
Try
If (txt_dob.Text.Trim() IsNot DBNull.Value) Then
Calender2.SelectedDate = Convert.ToDateTime(txt_dob.Text)
End If
Catch ex As Exception
End Try
Calender2.Visible = True
End Sub
Protected Sub calendar_changed(ByVal sender As Object, ByVal e As System.EventArgs)
txt_dob.Text = DateTime.Parse(Calender2.SelectedDate).ToString("dd/MM/yy")
Calender2.Visible = False
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt_dob" runat="server"></asp:TextBox>
<asp:Button ID="btn_dob" runat="server" Text="Button" OnClick="btn_dob_click" />
<asp:Calendar ID="Calender2" runat="server" OnSelectionChanged="calendar_changed"></asp:Calendar>
</div>
</form>
</body>
</html>
One other thing: I notice that you are using
Calender2.SelectedDate.ToString()
. This will also put the time in. Using
datetime.parse(Calendar2.selecteddate).ToString("dd/MM/yy")
would avoid this.
One final hint: you may have done this elsewhere, but you have not put an error trap for a value that isn't a valid date. Try :
IsDate(txt_dob.text)