Home > DeveloperSection > Forums > ajax calendar for textbox in gridview
pramod niralakeri
pramod niralakeri

Total Post:1

Points:7
Posted on    March-05-2015 7:49 PM

 .NET C# 
Ratings:


 1 Reply(s)
 663  View(s)
Rate this:
i need solution for... AJAX calendar for Textbox in dynamic datagridView where any number of textboxes can be created... but i need AJAX calendar with validation like date should be within 5 from the today's date
and unique date in each textbox i mean no date should clash with textbox with any other


Pawan Shukla

Total Post:36

Points:260
Posted on    March-09-2015 3:57 AM

you can try this code

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

 

<!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 runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:ScriptManager ID="ScriptManager1" runat="server">

        </asp:ScriptManager>

        <br />

        <br />

        <table width="800px" align="center">

            <tr>

                <td colspan="2" align="center">

                    <b>Employee Details</b>

                </td>

            </tr>

            <tr>

                <td colspan="2">

                    <asp:GridView ID="gvEmployeeDetails" runat="server" Width="100%" AutoGenerateColumns="false"

                        ShowFooter="true" OnRowCommand="gvEmployeeDetails_RowCommand" OnRowDeleting="gvEmployeeDetails_RowDeleting"

                        OnRowUpdating="gvEmployeeDetails_RowUpdating" OnRowCancelingEdit="gvEmployeeDetails_RowCancelingEdit"

                        OnRowEditing="gvEmployeeDetails_RowEditing">

                        <Columns>

                            <asp:TemplateField HeaderText="Employee ID">

                                <ItemTemplate>

                                    <asp:Label ID="lblEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "id") %>'></asp:Label>

                                </ItemTemplate>

                                <EditItemTemplate>

                                    <asp:Label ID="lblEditEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "id") %>'></asp:Label>

                                </EditItemTemplate>

                                <FooterTemplate>

                                    <asp:TextBox ID="txtAddEmpID" runat="server"></asp:TextBox>

                                </FooterTemplate>

                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="Name">

                                <ItemTemplate>

                                    <asp:Label ID="lblName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "name") %>'></asp:Label>

                                </ItemTemplate>

                                <EditItemTemplate>

                                    <asp:TextBox ID="txtEditName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "name") %>'></asp:TextBox>

                                </EditItemTemplate>

                                <FooterTemplate>

                                    <asp:TextBox ID="txtAddName" runat="server"></asp:TextBox>

                                </FooterTemplate>

                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="City">

                                <ItemTemplate>

                                    <asp:Label ID="lblCity" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "city") %>'></asp:Label>

                                </ItemTemplate>

                                <EditItemTemplate>

                                    <asp:TextBox ID="txtEditCity" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "city") %>'></asp:TextBox>

                                </EditItemTemplate>

                                <FooterTemplate>

                                    <asp:TextBox ID="txtAddCity" runat="server"></asp:TextBox>

                                </FooterTemplate>

                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="Date of Joining">

                                <ItemTemplate>

                                    <asp:Label ID="lblCountry" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "City") %>'></asp:Label>

                                </ItemTemplate>

                                <EditItemTemplate>

                                    <asp:TextBox ID="txtEditCountry" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "City") %>'></asp:TextBox>

                                    <asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtEditCountry"

                                                          PopupButtonID="txtEditCountry" Format="dd/MM/yyyy">

                                    </asp:CalendarExtender>

                                </EditItemTemplate>

                                <FooterTemplate>

                                    <asp:TextBox ID="txtAddCountry" runat="server"></asp:TextBox>

                                     <asp:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="txtAddCountry"

                                                           PopupButtonID="txtAddCountry" Format="dd/MM/yyyy">

                                    </asp:CalendarExtender>

                                </FooterTemplate>

                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="Action">

                                <ItemTemplate>

                                    <asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit" ImageUrl="~/Images/icon-edit.png"

                                        Height="32px" Width="32px" />

                                    <asp:ImageButton ID="imgbtnDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/Delete.png" />

                                </ItemTemplate>

                                <EditItemTemplate>

                                    <asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" ImageUrl="~/Images/icon-update.png" />

                                    <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/icon-Cancel.png" />

                                </EditItemTemplate>

                                <FooterTemplate>

                                    <asp:LinkButton ID="lbtnAdd" runat="server" CommandName="ADD" Text="Add" Width="100px"></asp:LinkButton>

                                </FooterTemplate>

                            </asp:TemplateField>

                        </Columns>

                    </asp:GridView>

                </td>

            </tr>

            <tr>

                <td colspan="2" style="background-image: url(Images/header.jpg); height: 30px">

                </td>

            </tr>

        </table>

    </div>

    </form>

</body>

</html>

default.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Xml;

using System.Data;

using System.Data.SqlClient;

 

public partial class _Default : System.Web.UI.Page

{

 

    SqlConnection conn = new SqlConnection("Data Source=local;Initial Catalog=MyDB;user id=sa;password=xyz;MultipleActiveResultSets=True;");

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            BindData();

        }

    }

 

    protected void BindData()

    {

        DataSet ds = new DataSet();

        DataTable FromTable = new DataTable();

        conn.Open();

        string cmdstr = "Select * from Employee";

        SqlCommand cmd = new SqlCommand(cmdstr, conn);

        SqlDataAdapter adp = new SqlDataAdapter(cmd);

        adp.Fill(ds);

        cmd.ExecuteNonQuery();

        FromTable = ds.Tables[0];

        if (FromTable.Rows.Count > 0)

        {

            gvEmployeeDetails.DataSource = FromTable;

            gvEmployeeDetails.DataBind();

        }

        else

        {

            FromTable.Rows.Add(FromTable.NewRow());

            gvEmployeeDetails.DataSource = FromTable;

            gvEmployeeDetails.DataBind();

            int TotalColumns = gvEmployeeDetails.Rows[0].Cells.Count;

            gvEmployeeDetails.Rows[0].Cells.Clear();

            gvEmployeeDetails.Rows[0].Cells.Add(new TableCell());

            gvEmployeeDetails.Rows[0].Cells[0].ColumnSpan = TotalColumns;

            gvEmployeeDetails.Rows[0].Cells[0].Text = "No records Found";

        }

        ds.Dispose();

        conn.Close();

    }

 

    protected void gvEmployeeDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

        Label lblEmpID = (Label)gvEmployeeDetails.Rows[e.RowIndex].FindControl("lblEmpID");

 

        conn.Open();

        string cmdstr = "delete from Employee where id=@empid";

        SqlCommand cmd = new SqlCommand(cmdstr, conn);

        cmd.Parameters.AddWithValue("@empid", lblEmpID.Text);

        cmd.ExecuteNonQuery();

        conn.Close();

        BindData();

 

    }

    protected void gvEmployeeDetails_RowCommand(object sender, GridViewCommandEventArgs e)

    {

        if (e.CommandName.Equals("ADD"))

        {

            TextBox txtAddEmpID = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddEmpID");

            TextBox txtAddName = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddName");

            TextBox txtAddDesignation = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddDesignation");

            TextBox txtAddCity = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddCity");

            TextBox txtAddCountry = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddCountry");

 

            conn.Open();

            string cmdstr = "insert into Employee(empid,name,designation,city,dateofjoin) values(@empid,@name,@designation,@city,@dateofjoin)";

            SqlCommand cmd = new SqlCommand(cmdstr, conn);

            cmd.Parameters.AddWithValue("@empid", txtAddEmpID.Text);

            cmd.Parameters.AddWithValue("@name", txtAddName.Text);

            cmd.Parameters.AddWithValue("@designation", txtAddDesignation.Text);

            cmd.Parameters.AddWithValue("@city", txtAddCity.Text);

            cmd.Parameters.AddWithValue("@dateofjoin", txtAddCountry.Text);

            cmd.ExecuteNonQuery();

            conn.Close();

            BindData();

        }

    }

    protected void gvEmployeeDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

        Label lblEditEmpID = (Label)gvEmployeeDetails.Rows[e.RowIndex].FindControl("lblEditEmpID");

        TextBox txtEditName = (TextBox)gvEmployeeDetails.Rows[e.RowIndex].FindControl("txtEditName");

        TextBox txtEditDesignation = (TextBox)gvEmployeeDetails.Rows[e.RowIndex].FindControl("txtEditDesignation");

        TextBox txtEditCity = (TextBox)gvEmployeeDetails.Rows[e.RowIndex].FindControl("txtEditCity");

        TextBox txtEditCountry = (TextBox)gvEmployeeDetails.Rows[e.RowIndex].FindControl("txtEditCountry");

 

        conn.Open();

        string cmdstr = "update Employee set name=@name,designation=@designation,city=@city,dateofjoin=@dateofjoin where empid=@empid";

        SqlCommand cmd = new SqlCommand(cmdstr, conn);

        cmd.Parameters.AddWithValue("@empid", lblEditEmpID.Text);

        cmd.Parameters.AddWithValue("@name", txtEditName.Text);

        cmd.Parameters.AddWithValue("@designation", txtEditDesignation.Text);

        cmd.Parameters.AddWithValue("@city", txtEditCity.Text);

        cmd.Parameters.AddWithValue("@dateofjoin", txtEditCountry.Text);

        cmd.ExecuteNonQuery();

        conn.Close();

 

        gvEmployeeDetails.EditIndex = -1;

        BindData();

 

    }

    protected void gvEmployeeDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

    {

        gvEmployeeDetails.EditIndex = -1;

        BindData();

    }

    protected void gvEmployeeDetails_RowEditing(object sender, GridViewEditEventArgs e)

    {

        gvEmployeeDetails.EditIndex = e.NewEditIndex;

        BindData();

    }

}


Output:



Modified On Mar-09-2015 04:13:51 AM

Don't want to miss updates? Please click the below button!

Follow MindStick