Hi,
Ok, first let me say that I do very little ASP.NET, mostly WinForms. So this
might not be the best solution. But after some investigation into your
problem this is what I have concluded.
When adding the controls dynamically the events must be wired in the
Page_Load since the events are fired then. If it is only done when your Cell
is rendered, the ASP.NET event firing has already been done so it is to
late. Additionally the control must belong to a control collection before
the events are fired. With this in mind, my solution entailed placing a
PlaceHolder on the page. Then in the Page_Load event I created the button,
added it to the PlaceHolder controls collection and wired the event. After
this the events Fire. Then in the Page_PreRender event I clear the
PlaceHolder controls collection, this prevents the control actually showing
in the PlaceHolder. In the DayRender I added the Button to the Cells control
collection causing the Button to appear in the correct place. I hope this
helps and I really hope there is a better solution!
See Code Below
NB: I changed the date format to work on my locale, so you will have to
change that!!!
Regards
Chris Taylor
<%@ Page Language="vb" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<HTML>
<HEAD>
<script language="vb" runat="server">
Friend WithEvents b as Button
Sub Page_Load(sender As Object, e As EventArgs)
pnlUserInfo.Visible = False
pnlCalendar.Visible = True
AddHandler button1.Click, AddressOf b_click
b = new Button()
b.text = "hello"
b.ID = "b"
PlaceHolder1.controls.Add(b)
AddHandler b.Click, AddressOf b_click
End Sub
sub Page_PreRender(sender As Object, e As EventArgs)
placeholder1.controls.clear()
end sub
Sub Calendar1_DayRender(sender As Object, e As DayRenderEventArgs)
Dim d As CalendarDay
Dim c As TableCell
d = e.Day
c = e.Cell
If d.IsOtherMonth Then
c.Controls.Clear
Else
Try
If e.Day.Date = DateTime.Parse("25/8/2003") Then
e.Cell.Controls.Add(b)
End If
Catch exc As Exception
Response.Write(exc.ToString())
End Try
End If
End Sub
Sub Date_Selected(sender As Object, e As EventArgs)
pnlCalendar.Visible = False
pnlUserInfo.Visible = True
End Sub
Sub b_click(sender As Object, e As EventArgs)
If txt1.text = "" Or txt1.text = "mornin" Then
txt1.text = "hello to you too."
Else
txt1.text = "mornin"
End If
End Sub
</script>
</HEAD>
<body>
<form id="Form1" runat="server">
<asp
anel id="pnlCalendar" runat="server">
<asp:textbox id="txt1" runat="server"></asp:textbox>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<asp
laceHolder id="PlaceHolder1" runat="server"></asp
laceHolder>
<asp:calendar id="calendar1" runat="server" cellpadding="0"
width="600px" daystyle-height="65px"
borderwidth="1px" onselectionchanged="Date_Selected"
ondayrender="Calendar1_DayRender" showgridlines="true"></asp:calendar>
</asp
anel><asp
anel id="pnlUserInfo" runat="server">
<asp:label id="label1" runat="server" text="Blah Blah"></asp:label>
</asp
anel></form>
</body>
</HTML>