Sunday, 1 September 2013

DatePicker in Asp.net

1. Open Visual Studio
2. Create a New Website
3. Create Web Page
4. Place TextBox, Button and Calendar Control



5. Write Code on Page_Load Event
 protected void Page_Load(object sender, EventArgs e)
    {
        Calendar1.Visible = false;
    }

6. Write Code on Button Click Event

 protected void Button1_Click(object sender, EventArgs e)
    {
        if (Calendar1.Visible == false)
        {
            Calendar1.Visible = true;
        }
        else
        {
            Calendar1.Visible = false;
        }
    }

7. Write Code on Calendar Event
 protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        txtdate.Text=Calendar1.SelectedDate.ToString("dd-MM-yyyy");
    }


8.Full Code
DatePicker.aspx Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 227px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table class="style1">
        <tr>
            <td class="style2">
                <asp:TextBox ID="txtdate" runat="server" Width="173px"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="..." />
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Calendar ID="Calendar1" runat="server" 
                    onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    <div>
    
    </div>
    </form>
</body>
</html>






DatePicker..aspx.cs Code

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

public partial class DatePicker : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Calendar1.Visible = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Calendar1.Visible == false)
        {
            Calendar1.Visible = true;
        }
        else
        {
            Calendar1.Visible = false;
        }
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        txtdate.Text=Calendar1.SelectedDate.ToString("dd-MM-yyyy");
    }
}

No comments:

Post a Comment

Thanks

SQL server tricky questions