[RESOLVED] dynamically add checkboxes to a calendar control...

I want to add two checkboxes to selected days in a calendar control.  I can do that but I can not figure out how to get the checkboxes to fire an event.   I tried to get a button to fire and event by iterating through the panel without success either. Here is my code so far:

Thanks in advance,

Photon (Springfield, Missouri)

 

<%@ Page Title="" Language="VB" MasterPageFile="~/Default.master" AutoEventWireup="false" CodeFile="Caledar_Add.aspx.vb" Inherits="Admin_Caledar_Add" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
    
    <div id="home">
     <div id="Div" runat="server" style="width: 810px; height: 500px;">
   <div id="sidebar">
       
               
                 <asp:Button ID="Button_GetCheckBox" runat="server" Text="Button" />
                 <asp:Label id="Label1" runat="server" />
             </div>
       
             <asp:Panel ID="Panel1" runat="server">
           
       
       
      <div id="content">
          <h3><font face="Verdana">Adding Custom Content to Calendar</font></h3>
          <asp:Calendar id="Calendar1" runat="server"
                    ondayrender="Calendar1_DayRender"
                    onselectionchanged="Date_Selected"
                    ShowGridLines="true"
                    BorderWidth="1"
                    Font-Names="Verdana"
                    Font-Size="9px"
                    Width="500px"
                    VisibleDate="01/01/2011"
                    TitleStyle-BackColor="Red"
                    TitleStyle-Font-Size="12px"
                    TitleStyle-Font-Bold="true"
                     DayStyle-VerticalAlign="Top"
                    DayStyle-Height="50px"
                    DayStyle-Width="14%"
                    SelectedDate="1/1/0001"
                    SelectedDayStyle-BackColor="Red" />
                </asp:Panel>
  
  </div>
  </div>
  </div>

</asp:Content>

 

'**Code Behind**
Partial Class Admin_Caledar_Add

    Inherits System.Web.UI.Page

    Dim holidays(12, 31) As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        holidays(1, 1) = "New Year's Day"
        holidays(1, 26) = "Australia Day"
        holidays(2, 2) = "Groundhog Day"
        holidays(2, 14) = "Valentine's Day"
        holidays(3, 17) = "St. Patrick's Day"
        holidays(4, 1) = "April Fool's Day"
        holidays(5, 1) = "May Day"
        holidays(6, 15) = "My Birthday"
        holidays(7, 15) = "My Anniversary"
        holidays(8, 15) = "My Mother's Birthday"
        holidays(9, 24) = "Autumnal Equinox"
        holidays(12, 26) = "Boxing Day"

    End Sub
   

    Sub Calendar1_DayRender(ByVal sender As Object, ByVal 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
                Dim Hol As String = holidays(d.Date.Month, d.Date.Day)
                'Response.Write(Hol)
                If Hol <> "" Then
                   
                    Dim _checkbox_Delete As CheckBox = New CheckBox()
                    _checkbox_Delete.ID = "chkDynamicCheckBox_Delete"
                    _checkbox_Delete.Text = "Delete"
                    c.Controls.Add(New LiteralControl("<br>" + Hol.ToString + "<br/>"))
                    c.Controls.Add((_checkbox_Delete))
                   
                    AddHandler _checkbox_Delete.CheckedChanged, AddressOf _checkbox_Delete_CheckedChanged
                End If
            Catch exc As Exception
               
                Response.Write(exc.ToString())
            End Try
        End If
    End Sub

   


   

    Function _checkbox_Delete_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim objCheckbox = DirectCast(sender, CheckBox)
        Response.Write("test")
    End Function

   

    Protected Sub Button_GetCheckBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_GetCheckBox.Click
        For Each ctl As Control In Panel1.Controls
    
            If TypeOf ctl Is CheckBox Then
                'CType(ctl,CheckBox).ID
                Response.Write("test")
            End If
        Next ctl

    End Sub


End Class

 

I haven't got a response  so I will try again in a more focused way.  I have created a calendar control using ASP.net visual studio express 2008.  In the dayrender event, I have dynamically created  checkboxes.  My problem is I am not able to fire an event through an event handler.  What am I not doing correctly?  Below is my code:

Thanks in advance,

Photon

 

<%@ Page Title="" Language="VB" MasterPageFile="~/Default.master" AutoEventWireup="false" CodeFile="testcalendar.aspx.vb" Inherits="Admin_testcalendar" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
    <div id="content">
         
  <h3><font face="Verdana">Adding Custom Content to Calendar</font></h3>

    <asp:Calendar id="Calendar1" runat="server"
                    ondayrender="Calendar1_DayRender"
                    ShowGridLines="true"
                    BorderWidth="1"
                    Font-Names="Verdana"
                    Font-Size="9px"
                    Width="500px"
                    VisibleDate="01/01/2011"
                    TitleStyle-BackColor="Red"
                    TitleStyle-Font-Size="12px"
                    TitleStyle-Font-Bold="true"
                     DayStyle-VerticalAlign="Top"
                    DayStyle-Height="50px"
                    DayStyle-Width="14%"
                    SelectedDate="1/1/0001"
                    SelectedDayStyle-BackColor="Red" />
                   
    </div>

</asp:Content>

'***Code Behind****

Partial Class Admin_testcalendar
    Inherits System.Web.UI.Page

    Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
       
        e.Cell.Text = ""
        Dim c As New CheckBox()
        c.Text = e.Day.DayNumberText
        AddHandler c.CheckedChanged, AddressOf _checkbox_CheckedChanged
        c.AutoPostBack = True

        e.Cell.Controls.Add(c)

    End Sub

    Protected Sub _checkbox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim objCheckbox = DirectCast(sender, CheckBox)

        'this response.write does not fire when a checkbox is checked
        Response.Write("test")
    End Sub
End Class


 

 

 

 

Hi,

Photon

I have dynamically created  checkboxes.  My problem is I am not able to fire an event through an event handler.  What am I not doing correctly
 

When we dynamically add control in page. We should notice that in page_Init as well as page_load is the best period for us to add control.

Calendar1_DayRender evnet is a very later event in page life cycle. So "AddHandler c.CheckedChanged, AddressOf _checkbox_CheckedChanged" will not trigger the event.

Alternative way is to use Page.ClientScript.GetPostBackEventReference to add a postback event manually to checkbox.

Like:

protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Form["__EVENTTARGET"] != null)
            {
                Response.Write(Request.Form["__EVENTARGUMENT"]);
            }
        }


  protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            e.Cell.Text = "";
            CheckBox cb = new CheckBox();
            cb.ID = "cb_" + e.Day.Date.ToString();
            cb.Text = e.Day.DayNumberText;
            cb.Attributes["onclick"] = Page.ClientScript.GetPostBackEventReference(cb, "CheckChanged");  //add postback event(javascript)
           

            e.Cell.Controls.Add(cb);
        }


And also the calendar can not remain the checkbox checked state on postback, so you need to use some state management object to store the checkbox checked list and remain them manually.

Hope this can help you.

thoughts on "[RESOLVED] dynamically add checkboxes to a calendar control..."

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>