I tryed to use a ASP.NET calendar control to fill a textbox with the selecteddate using the ATLAS updatepanel. It works!
Only to have a [x] to give the user the chance to close the calendar panel without date selection, i rebuilded the header of the calender. I used a label and 2 hyperlinks (prev next month) . Without that you need only the 3 lines source code (orange marked in the last block)
To hide and show the calendar i added a panel wich is renderd as div and hidden by display style (none,block). If you have the idea to use visible=false, the control is not reenderd into the page.
<form id=" >
<runat="server">
Daten:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><atlas:UpdatePanel ID="UpdatePanel1"
runat="server">
<ContentTemplate>
Datum:<asp:TextBox ID="TextBox1" runat="server" Width="89px"></asp:TextBox>
<asp:ImageButton ID="ImageButton1" runat="server" OnClientClick="kal1.style.display ='block';" ImageUrl="~/images/kalender.gif"
Style="left: 144px; top: 111px" /><br />
<asp:Panel ID="pnlCalender" runat="server" Height="50px" Style="left: 72px; position: relative;
top: -1px;" Width="125px" Font-Names="arial" Font-Size="Small" BackColor="#C0C0FF"
BorderColor="Blue" BorderWidth="1px">
<asp:LinkButton ID="lnkPrev" runat="server" OnClick="lnkPrev_Click"><<</asp:LinkButton>
<asp:Label ID="lblTitle" runat="server" Text="Label"></asp:Label>
<asp:LinkButton ID="lnkNext" runat="server">>></asp:LinkButton> <a onclick="kal1.style.display='none'"
style="left: 137px; position: absolute; top: 0px">x</a>
<br />
<asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="#3366CC"
BorderWidth="1px" CellPadding="1" DayNameFormat="Shortest" Font-Names="Verdana"
Font-Size="8pt" ForeColor="#003399" ShowDayHeader="false" ShowTitle="false" Height="150px"
Width="150px">
<SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<TodayDayStyle BackColor="#99CCCC" ForeColor="White" />
<SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
<WeekendDayStyle BackColor="#CCCCFF" />
<OtherMonthDayStyle ForeColor="#999999" />
<NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />
<DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" />
<TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px" Font-Bold="True"
Font-Size="10pt" ForeColor="#CCCCFF" Height="10px" />
</asp:Calendar>
</asp:Panel>
</ContentTemplate>
</atlas:UpdatePanel>
</form>
Code
At startup the code creates a jscript which find the DIV element id in the dom. Cause this DIV can be nested , (eg master page) it is good practice to use the created asp.net clientid as argument for getelementbyid. (do not hardcode it)
Then the the panel (div) is set to display none. Dont do that in the declaration of the calendar control, cause this is renderd by every AJAX callback and so the visible state would be reseted.
When the user select a day the event of the calendar is fired. At this point i had 2 options,
-fill the textbox on client side: con: must generate the the script with controlid and do coding, pro: can place textbox outside updatepanel
-fill textbox on serverside: con: overhead in callback for textbox, limitations in html design pro: easy to implement
i decided to use the second option and implemented the 3rd needed line of code in the calendar event handler.
Partial
Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ClientScript.RegisterStartupScript(
Me.GetType(), "kal1", "var kal1=document.getElementById('" + pnlCalender.ClientID + "');", True) If Not IsPostBack Then Calendar1.VisibleDate =
Date.Now ClientScript.RegisterStartupScript(
Me.GetType(), "kal1hide", "kal1.style.display='none';", True) lblTitle.Text = Calendar1.VisibleDate.Month.ToString() +
" " + Calendar1.VisibleDate.Year.ToString End If End Sub Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged TextBox1.Text = Calendar1.SelectedDate
End Sub Protected Sub lnkNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkNext.Click Calendar1.VisibleDate = Calendar1.VisibleDate.AddMonths(1)
lblTitle.Text = Calendar1.VisibleDate.Month.ToString() +
" " + Calendar1.VisibleDate.Year.ToString End Sub Protected Sub lnkPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Calendar1.VisibleDate = Calendar1.VisibleDate.AddMonths(-1)
lblTitle.Text = Calendar1.VisibleDate.Month.ToString() +
" " + Calendar1.VisibleDate.Year.ToString End Sub End
Class