Saturday, 30 November 2013

Asp.net using Linq

Leave a Comment
calender.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Calender.aspx.cs" Inherits="CalenderForTatvasoft.Calender" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnGetDates").click(function (e) {
                if ($('select option:selected[value=""]').length > 0) {
                    e.preventDefault();
                    e.stopPropagation();
                    alert('please select all select boxes');
                }
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h1>Calender</h1>
            <asp:Label Text="" ID="lblMessage" ForeColor="Red" runat="server" />
            <table border="0" cellpadding="5" cellspacing="5">
                <tr>
                    <td>From Date :
                    <asp:TextBox ID="txtFromDate" runat="server" ReadOnly="true"></asp:TextBox>
                    </td>
                    <td>To Date :
                    <asp:TextBox ID="txtToDate" runat="server" ReadOnly="true"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td style="height: 4px;"></td>
                </tr>
                <tr>
                    <td align="center">
                        <asp:Calendar ID="calExtendarFromDate" runat="server" OnSelectionChanged="calExtendarFromDate_SelectionChanged">
                            <OtherMonthDayStyle BackColor="Gray" />
                        </asp:Calendar>
                    </td>
                    <td align="center">
                        <asp:Calendar ID="calExtendarToDate" runat="server" OnSelectionChanged="calExtendarToDate_SelectionChanged">
                            <OtherMonthDayStyle BackColor="Gray" />
                        </asp:Calendar>
                    </td>
                </tr>
            </table>
            <table border="0" cellpadding="5" cellspacing="5">
                <tr>
                    <td>Select Criteria
                    </td>
                    <td>
                        <asp:RadioButton Text="Repeated" ID="rdbRepeatOn" Checked="true" GroupName="RepeatMode"
                            AutoPostBack="true" runat="server" OnCheckedChanged="rdbRepeatOn_CheckedChanged" />
                        <asp:RadioButton Text="Repeated On" ID="rdbRepeatedOn" GroupName="RepeatMode" runat="server"
                            AutoPostBack="true" OnCheckedChanged="rdbRepeatedOn_CheckedChanged" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Panel runat="server" ID="pnlRepeated">
                            <table border="0" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td>
                                        <asp:DropDownList runat="server" ID="ddlEvery">
                                            <asp:ListItem Value="1" Text="Every" />
                                            <asp:ListItem Value="2" Text="Every Other" />
                                            <asp:ListItem Value="3" Text="Every Third" />
                                            <asp:ListItem Value="4" Text="Every Forth" />
                                        </asp:DropDownList>
                                    </td>
                                    <td>
                                        <asp:DropDownList runat="server" ID="ddlRepretedType">
                                            <asp:ListItem Value="1" Text="Day" />
                                            <asp:ListItem Value="2" Text="Week" />
                                            <asp:ListItem Value="3" Text="Month" />
                                            <asp:ListItem Value="4" Text="Year" />
                                        </asp:DropDownList>
                                    </td>
                                </tr>
                            </table>
                        </asp:Panel>
                        <asp:Panel runat="server" ID="pnlRepeatedOn" Visible="false">
                            <table border="0" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td>
                                        <asp:DropDownList runat="server" ID="ddlRepeatOnDay">
                                            <%--<asp:ListItem Value="">-select-</asp:ListItem>--%>
                                            <asp:ListItem Value="0">Sunday</asp:ListItem>
                                            <asp:ListItem Value="1">Monday</asp:ListItem>
                                            <asp:ListItem Value="2">Tuesday</asp:ListItem>
                                            <asp:ListItem Value="3">Wednesday</asp:ListItem>
                                            <asp:ListItem Value="4">Thrusday</asp:ListItem>
                                            <asp:ListItem Value="5">Friday</asp:ListItem>
                                            <asp:ListItem Value="6">Saturday</asp:ListItem>
                                        </asp:DropDownList>
                                    </td>
                                    <td>
                                        <asp:DropDownList runat="server" ID="ddlRepeatOnType">
                                            <%--<asp:ListItem Value="">-select-</asp:ListItem>--%>
                                            <asp:ListItem Value="1">First</asp:ListItem>
                                            <asp:ListItem Value="2">Second</asp:ListItem>
                                            <asp:ListItem Value="3">Third</asp:ListItem>
                                            <asp:ListItem Value="4">Fourth</asp:ListItem>
                                        </asp:DropDownList>
                                    </td>
                                    <td>
                                        <asp:DropDownList runat="server" ID="ddlRepeatOnMonth">
                                            <%--<asp:ListItem Value="">-select-</asp:ListItem>--%>
                                            <asp:ListItem Value="1">Month</asp:ListItem>
                                            <asp:ListItem Value="3">3 Months</asp:ListItem>
                                            <asp:ListItem Value="4">4 Months</asp:ListItem>
                                            <asp:ListItem Value="6">6 Months</asp:ListItem>
                                            <asp:ListItem Value="12">Year</asp:ListItem>
                                        </asp:DropDownList>
                                    </td>
                                </tr>
                            </table>
                        </asp:Panel>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Button Text="GetDates" ID="btnGetDates" ClientIDMode="Static" OnClick="btnGetDates_Click"
                            runat="server" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:DataList runat="server" ID="dlDates" RepeatColumns="3" RepeatDirection="Horizontal">
                            <ItemTemplate>
                                <div style="padding: 15px">
                                    <table border="0" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td>
                                                <%# Container.ItemIndex + 1 %>
                                            </td>
                                            <td>&nbsp;:)&nbsp;&nbsp;
                                            </td>
                                            <td>
                                                <%# Container.DataItem %>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </ItemTemplate>
                        </asp:DataList>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

calender.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CalenderForTatvasoft
{
    public partial class Calender : System.Web.UI.Page
    {

        #region Properties
        public DateTime FromDate { get { return Convert.ToDateTime(txtFromDate.Text); } }
        public DateTime ToDate { get { return Convert.ToDateTime(txtToDate.Text); } }
        #endregion

        #region Page Events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblMessage.Text = "";
            }

        }
        #endregion

        #region Control Events
        protected void calExtendarFromDate_SelectionChanged(object sender, EventArgs e)
        {
            txtFromDate.Text = calExtendarFromDate.SelectedDate.ToShortDateString();
        }

        protected void calExtendarToDate_SelectionChanged(object sender, EventArgs e)
        {
            txtToDate.Text = calExtendarToDate.SelectedDate.ToShortDateString();
        }

        protected void rdbRepeatOn_CheckedChanged(object sender, EventArgs e)
        {
            pnlRepeated.Visible = rdbRepeatOn.Checked;
            pnlRepeatedOn.Visible = !rdbRepeatOn.Checked;
        }

        protected void rdbRepeatedOn_CheckedChanged(object sender, EventArgs e)
        {
            pnlRepeatedOn.Visible = rdbRepeatedOn.Checked;
            pnlRepeated.Visible = !rdbRepeatedOn.Checked;
        }

        protected void btnGetDates_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFromDate.Text) || string.IsNullOrEmpty(txtToDate.Text))
            {
                lblMessage.Text = "Please select dates first.";
                return;
                if (FromDate > ToDate)
                {
                    lblMessage.Text = "From date must be less than to date.";
                    return;
                }
            }

            if (pnlRepeatedOn.Visible)
                BindRepeatedOn();
            else
                BindRepeated();
        }
        #endregion

        #region Methods
        /// <summary>
        /// To Bind dates based on Repeated Criteria
        /// </summary>
        private void BindRepeated()
        {
            List<DateTime> lstDates = new List<DateTime>();
            int SelectedEveryInterval = Convert.ToInt16(ddlEvery.SelectedValue);
            int SelectedRepeatType = Convert.ToInt16(ddlRepretedType.SelectedValue);
            int ExamptionRange = 0;
            if (SelectedRepeatType == 1)
                ExamptionRange = SelectedEveryInterval * SelectedRepeatType;
            else if (SelectedRepeatType == 2)
                ExamptionRange = SelectedEveryInterval * 7;
            else if (SelectedRepeatType == 3 || SelectedRepeatType == 4)
                ExamptionRange = SelectedEveryInterval;

            DateTime NewDate = FromDate;
            lstDates.Add(NewDate);
            while (NewDate < ToDate)
            {
                if (SelectedRepeatType == 1 || SelectedRepeatType == 2)
                    NewDate = NewDate.AddDays(ExamptionRange);
                else if (SelectedRepeatType == 3)
                    NewDate = NewDate.AddMonths(ExamptionRange);
                else if (SelectedRepeatType == 4)
                    NewDate = NewDate.AddYears(ExamptionRange);
                lstDates.Add(NewDate);
            }
            dlDates.DataSource = lstDates.Where(l => l >= FromDate && l <= ToDate).Select(s => s.ToShortDateString()).ToList();
            dlDates.DataBind();
        }

        /// <summary>
        /// To Bind dates based on RepeatedOn Criteria
        /// </summary>
        private void BindRepeatedOn()
        {
            List<DateTime> lstDates = new List<DateTime>();
            int SelectedDay = Convert.ToInt16(ddlRepeatOnDay.SelectedValue);
            int SelectedTime = Convert.ToInt16(ddlRepeatOnType.SelectedValue);
            int SelectedMonths = Convert.ToInt16(ddlRepeatOnMonth.SelectedValue);
            DateTime FromDateMSD = new DateTime(FromDate.Year, FromDate.Month, 1);
            DateTime tempDate = FromDateMSD.AddDays(((DayOfWeek)SelectedDay < FromDateMSD.DayOfWeek ? 7 : 0) + (DayOfWeek)SelectedDay - FromDateMSD.DayOfWeek).AddDays((SelectedTime - 1) * 7);
            if (tempDate <= FromDate)
                FromDateMSD = FromDateMSD.AddMonths(1);
            int Count = 0;
            while (FromDateMSD <= ToDate)
            {
                FromDateMSD = FromDateMSD.AddMonths(Count == 0 ? 0 : SelectedMonths);
                DateTime NewDate = FromDateMSD.AddDays(((DayOfWeek)SelectedDay < FromDateMSD.DayOfWeek ? 7 : 0) + (DayOfWeek)SelectedDay - FromDateMSD.DayOfWeek).AddDays((SelectedTime - 1) * 7);
                lstDates.Add(NewDate);
                Count++;
            }
            dlDates.DataSource = lstDates.Where(l => l >= FromDate && l <= ToDate).Select(s => s.ToShortDateString()).ToList();
            dlDates.DataBind();
        }
        #endregion
    }

}

0 comments:

Post a Comment