Infolink

 

Search This Blog

Jul 9, 2015

Make custom calendar in c#

calendar.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            createcalendar(DateTime.Now.Month.ToString(), DateTime.Now.Year);
        }
    }

    public void createcalendar(string mt, int yr)
    {
        DataTable dtSource = new DataTable();
        dtSource.Columns.Add("Sunday");
        dtSource.Columns.Add("Monday");
        dtSource.Columns.Add("Tuesday");
        dtSource.Columns.Add("Wednesday");
        dtSource.Columns.Add("Thursday");
        dtSource.Columns.Add("Friday");
        dtSource.Columns.Add("Saturday");

        int year = yr;

        int month = Convert.ToInt16(mt);
        for (int y = 1; y <= 12; y++)
        {
            ddlmonth.Items.Add(new ListItem(y.ToString(), y.ToString()));
        }
        ddlmonth.SelectedValue = month.ToString();
        lblyear.Text = Convert.ToString(year);
        int DaysInMonth = DateTime.DaysInMonth(year, month);

        int i = 1;


        for (int weak = 0; weak <= 5; weak++)
        {
            DataRow newday = dtSource.NewRow();
            for (int day = 1; day <= 7; day++)
            {
                if (i > DaysInMonth) break;
                DateTime dDate = DateTime.ParseExact(i.ToString() + "/" + month.ToString() + "/" + year.ToString(), "d/M/yyyy", null);
                DayOfWeek dayWeek = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(dDate);
                newday[dayWeek.ToString()] = i.ToString();
                i++;

                if (dayWeek.ToString() == "Saturday") break;
            }
            dtSource.Rows.Add(newday);
            if (i > DaysInMonth) break;
        }

        WeekRepeater.DataSource = dtSource;
        WeekRepeater.DataBind();

        hdnyear.Value = year.ToString();


    }
    protected void lnkprev_Click(object sender, EventArgs e)
    {
        createcalendar(ddlmonth.SelectedValue, Convert.ToInt16(hdnyear.Value) - 1);
    }
    protected void lnknext_Click(object sender, EventArgs e)
    {
        createcalendar(ddlmonth.SelectedValue, Convert.ToInt16(hdnyear.Value) + 1);
    }
    protected void ddlmonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        createcalendar(ddlmonth.SelectedValue, Convert.ToInt16(hdnyear.Value));
    }
    protected void btnshow_Click(object sender, EventArgs e)
    {
        createcalendar(ddlmonth.SelectedValue, Convert.ToInt16(hdnyear.Value));
    }

calendar.aspx


<div id="calbg">
    <div id="calcontent">
        <table>
            <tr>
                <td>
                    <asp:TextBox runat="server" ID="txtdatefrom"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtdateto"></asp:TextBox>
                </td>
                <td>
                    <asp:Button runat="server" ID="btnshow" Text="SHOW" OnClick="btnshow_Click" />
                </td>
            </tr>
        </table>
        <table>
            <tr>
                <td>
                    <asp:LinkButton runat="server" ID="lnkprev" Text="<" OnClick="lnkprev_Click"></asp:LinkButton>
                </td>
                <td colspan="5">
                    <asp:DropDownList runat="server" ID="ddlmonth" OnSelectedIndexChanged="ddlmonth_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
                    <asp:Label runat="server" ID="lblyear"></asp:Label>
                </td>
                <td>
                    <asp:LinkButton runat="server" ID="lnknext" Text=">" OnClick="lnknext_Click"></asp:LinkButton>
                </td>
            </tr>
        </table>
        <asp:Repeater ID="WeekRepeater" runat="server" OnItemDataBound="WeekRepeater_ItemDataBound">
            <HeaderTemplate>
                <table>

                    <tr>
                        <th>Su
                        </th>
                        <th>Mo
                        </th>
                        <th>Tu
                        </th>
                        <th>Wed
                        </th>
                        <th>Thu
                        </th>
                        <th>Fr
                        </th>
                        <th>Sa
                        </th>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td bgcolor="#CCFFCC">
                        <asp:Label runat="server" ID="Label1" Text='<%# Eval("Sunday") %>' />
                    </td>
                    <td bgcolor="#CCFFCC">
                        <asp:Label runat="server" ID="Label2" Text='<%# Eval("Monday") %>' />
                    </td>
                    <td bgcolor="#CCFFCC">
                        <asp:Label runat="server" ID="Label3" Text='<%# Eval("Tuesday") %>' />
                    </td>
                    <td bgcolor="#CCFFCC">
                        <asp:Label runat="server" ID="Label4" Text='<%# Eval("Wednesday") %>' />
                    </td>
                    <td bgcolor="#CCFFCC">
                        <asp:Label runat="server" ID="Label5" Text='<%# Eval("Thursday") %>' />
                    </td>
                    <td bgcolor="#CCFFCC">
                        <asp:Label runat="server" ID="Label6" Text='<%# Eval("Friday") %>' />
                    </td>
                    <td bgcolor="#CCFFCC">
                        <asp:Label runat="server" ID="Label7" Text='<%# Eval("Saturday") %>' />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
        <asp:HiddenField runat="server" ID="hdnyear" />
    </div>
</div>

Apr 22, 2014

c# - Convert comma separated string to array

Split separates strings. Often strings have delimiter characters in their data. Split handles splitting upon string and character delimiters.Methods can be combined. Using IndexOf and Substring together is another way to split strings.
e.g.
Use Split to separate parts from a string. If your input is "A B C", split on the space to get an array of "A", "B" and "C".
<

string str = "1,2,3,4";

        // 1st way
        string[] strArray = str.Split(new char[] { ',' });
        int[] intArray = new int[strArray.Length];
        for (int i = 0; i < strArray.Length; i++)
        {
            intArray[i] = int.Parse(strArray[i]);
        }

        // 2nd way
        string[] array = str.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
Related Posts Plugin for WordPress, Blogger...