Wednesday, 26 September 2012

Beginning .Net : Select entire month or entire week using selection mode in calendar control with C# Examples and VB.Net Examples

Click Here to Download SelectWeekInCalendarControl.zip

You can select entire week or month in asp.net calendar control and get it's selected dates.
You can achieve using SelectionMode property of calendar control.
Selection mode property has an enum and it has four selection mode like Day , DayWeek , DayWeekMonth and None.

Here is example for this.
In this example we take calendar control and we set selection mode as DayWeek. When we set this mode after that calender control automatically add one column for select particular week.
You can specify "DayWeekMonth" value to selection mode property for select entire month and also specify "SelectMonthText" property to display select text.

ASPX Code :
        <asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="Black" 
            Font-Names="Times New Roman" Font-Size="10pt" ForeColor="Black" Height="220px"
                NextPrevFormat="FullMonth" Width="400px" SelectionMode="DayWeek" 
            DayNameFormat="Shortest" TitleFormat="Month" SelectWeekText="Select" >
                <DayHeaderStyle Font-Bold="True" Font-Size="7pt" ForeColor="#333333" 
                    Height="10pt" BackColor="#CCCCCC" />
                <DayStyle Width="14%" />
                <NextPrevStyle Font-Size="8pt" ForeColor="White" />
                <OtherMonthDayStyle ForeColor="#999999" />
                <SelectedDayStyle BackColor="#CC3333" ForeColor="White" />
                <SelectorStyle BackColor="#CCCCCC" Font-Bold="True" Font-Names="Verdana" 
                    Font-Size="8pt" ForeColor="#333333" Width="1%" />
                <TitleStyle BackColor="Black" Font-Bold="True"
                    Font-Size="13pt" ForeColor="White" Height="14pt" />
                <TodayDayStyle BackColor="#CCCC99" />
        </asp:Calendar>
         <br />

         <asp:Button runat="server" ID="btnGetSelectedDate" Text="Get Selected Date" 
        /> &nbsp;&nbsp;&nbsp;
         <b>Date : </b><asp:Label runat="server" ID="lblDate" ></asp:Label>

C# Examples :
    protected void btnGetSelectedDate_Click(object sender, EventArgs e)
    {
        foreach (DateTime dt in Calendar1.SelectedDates)
        {
            lblDate.Text = lblDate.Text + " <br/> " + dt.ToString("dd/MM/yyyy");
        }
    }

VB.net Examples :
    Protected Sub btnGetSelectedDate_Click(ByVal sender As Object, ByVal e As EventArgs)
        For Each dt As DateTime In Calendar1.SelectedDates
            lblDate.Text = lblDate.Text + " <br/> " & dt.ToString("dd/MM/yyyy")
        Next
    End Sub

Output :



 For Beginning .Net articles. Click Here...

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.



 

3 comments: