Category Archives: Calendar

[RESOLVED] Calendar control and querystring

Is it possible to click on the day within the calendar control and pass a query string.  The calendar is database driven. 

TheDirtyBird

click on the day within the calendar control and pass a query string. 

You can utilize Calendar SelectionChanged event and use Response.Redirect to pass date as queryparameter

<asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged">
</asp:Calendar>
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    Response.Redirect("somepage.aspx?dt=" + Calendar1.SelectedDate.ToShortDateString(), false);
}

[RESOLVED] Textboxes and Calendar Controls

How would I create a row by column structure dynamically that has column 1 as text boxes and column 2 as calendar controls? The number of rows is some positive integer. Is it best to store these controls in a grid?

What would the code look like?

Thanks, Dave

drl

has column 1 as text boxes and column 2 as calendar controls? 

You can use a GridView with two template fields.

<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox runat="server" ID="TextBox1"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

And you need to add your code for databinding the gridview or setting text/selected date for calendar.

Santhosh, thank you for your reply. Now how would I access say the textboxes in each row of the grid? Do they all have the same name "TextBox1"? Then which is which?

Please show a line or two of code to access the textboxes in the grid. Thanks. 

If you want to show just few rows of empty textboxes and calendar, you need to bind Gridview to an integer array.

//Just to bind 5 empty rows of data
GridView1.DataSource = new[] { 1, 2, 3, 4, 5 };
GridView1.DataBind();

If you want to access values from GridView, you can do this

//Retrieving values
foreach (GridViewRow row in GridView1.Rows)
{
    TextBox TextBox1 = (TextBox)row.FindControl("TextBox1");
    Calendar Calendar1 = (Calendar)row.FindControl("Calendar1");

    //values
    string text = TextBox1.Text;
    string date = Calendar1.SelectedDate.ToShortDateString();
    //do something with values here
}

Change ControlIDs in the FindControl according to your gridview markup. Try that way and see if it works.

GridView1.DataSource = new[] { 1, 2, 3, 4, 5 };

The compiler did not like this line - type expected at the "["... any suggestions? Thanks. 

drl

type expected at the "["

Ok, just specify int.

//Just to bind 5 empty rows of data
GridView1.DataSource = new int[] { 1, 2, 3, 4, 5 };
GridView1.DataBind();

GridView1.DataSource = new int[] { 1, 2, 3, 4, 5 };

GridView1.DataBind();

I ran just that much and the textboxes are clear. They have no value.

What's up?

drl

I ran just that much and the textboxes are clear. They have no value.

Can you pls explain clearly what you are tyring to do. TextBox will not contain any text as such unless until you specify which property to bind to using Text Ex: Text='<%# Eval("Some Property")%>'

I thought you are just trying to present a tabular Data Entry screen wherein user enters value and picks a date. If that's not the requirement, explain what you are trying to do.

"I thought you are just trying to present a tabular Data Entry screen wherein user enters value and picks a date." 

Yes, this is what I want with the addition of when the page first displays, the textboxes are filled with the integers initialized in the Gridview1.Datasource = new int[] = {1,2,3,4,5} statement.

drl

when the page first displays, the textboxes are filled with the integers

Check this

<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Number" ItemStyle-VerticalAlign="Top">
            <ItemTemplate>
                <asp:TextBox runat="server" Text='<%# Container.DataItem %>' ID="TextBox1"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Date">
            <ItemTemplate>
                <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Just to bind 5 empty rows of data
        GridView1.DataSource = new int[] { 1, 2, 3, 4, 5 };
        GridView1.DataBind();
    }
}

Works perfectly, thanks!

[RESOLVED] Databinding asp:calendar control

Is it possible to make asp:calendar control databindable to SQL database? Like GridView or ListView. I don't see how to do it and find it unbelievable. Or is there third party calendar control that is easy to use?

IgorB

Is it possible to make asp:calendar control databindable to SQL database?
 

What do you want to bind in calendar control? are you looking to select dates in calendar depending on SQL data? If so check DayRender Event(This event is raised when each day is created in the control hierarchy for the Calendar control.)

Ps:You may find this article useful..

http://odetocode.com/Articles/223.aspx

[RESOLVED] Colorize Textbox Dates

I'm trying to colorize the Dates I have in a textbox if the dates are in the Future.  For example if the Date is Jan. 1st 2023 then I want that font Blue, however, if that date is current or from the past I want to leave it alone.  I'm using Visual Studio 2010 - C# is the language.  

Thanks for any help!

Try this..

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            if (e.Day.Date > DateTime.Today)
            {
                e.Cell.ForeColor = System.Drawing.Color.Blue;
                //e.Day.IsSelectable = false;
            }
        }


 

 

Thanks for the reply!  But how do I tie this into my textboxes?   

skip10

But how do I tie this into my textboxes?   
 

Like this..

txtDate.Text = DateTime.Today.AddDays(3).ToString();
            if (Convert.ToDateTime(txtDate.Text) > DateTime.Today)
            {
                txtDate.ForeColor = System.Drawing.Color.CadetBlue;
            }
            else
            {
                txtDate.ForeColor = System.Drawing.Color.Black;
            }


 

[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.