Saturday, 26 July 2014

use Ajax CalenderExtender Control inside gridview in asp.net

Ajax Calender inside Gridview with insert, edit record




Description:-

in this example we explain that how to use Ajax CalenderExtender control inside Gridview in Asp.Net. Or how to use Ajax CalenderExtender control in gridview to add birthrate of the employee in database .

In previous example we already explain that how to bind Dropdownlist inside gridview.

Here in this example we have one table called employee and have two field like empname and employee birthdate. In gridview footer row we are add one textbox footer for employee name and one Ajax CalenderExtender control  for employee birthdate and when click on submit button this footer rows data like employye name and employee birthdate are bind to the gridview.

Dynamically Bind data in Ajax Accordion Panel Ajax Accordion panel Example in asp.net


how to Restrict the size of file upload when upload by user restrict the size of file upload control in asp.net




Default2.aspx:-

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

<%@ Register Assembly="AjaxControlToolkit" stud_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 id="Head1" runat="server">
    <title>how to use Ajax CalenderExtender control inside Gridview in Asp.Net</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>Students Details</b>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:GridView ID="grd_studentdetail" runat="server" Width="100%" AutoGenerateColumns="false"
                        ShowFooter="true" OnRowCommand="grd_studentdetail_RowCommand" OnRowDeleting="grd_studentdetail_RowDeleting"
                        OnRowUpdating="grd_studentdetail_RowUpdating" OnRowCancelingEdit="grd_studentdetail_RowCancelingEdit"
                        OnRowEditing="grd_studentdetail_RowEditing">
                        <Columns>
                            <asp:TemplateField HeaderText="Student ID">
                                <ItemTemplate>
                                    <asp:Label ID="lblstud_id" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "stud_id") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:Label ID="lblEditstud_id" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "stud_id") %>'></asp:Label>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtAddstud_id" runat="server"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="stud_name">
                                <ItemTemplate>
                                    <asp:Label ID="lblstud_name" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "stud_name") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtEditstudent_name" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "stud_name") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtAddstudent_name" 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, "dateofjoin") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtEditCountry" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "dateofjoin") %>'></asp:TextBox>
                                    <asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtEditCountry"
                                        PopupButtonID="txtEditCountry" Format="dd/MM/yyyy">
                                    </asp:CalendarExtender>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtAddDateofjoin" runat="server"></asp:TextBox>
                                    <asp:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="txtAddDateofjoin"
                                        PopupButtonID="txtAddDateofjoin" Format="dd/MM/yyyy">
                                    </asp:CalendarExtender>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Action">
                                <ItemTemplate>
                                    <asp:ImageButton ID="imgbtnEdit" runat="server" command_name="Edit" ImageUrl="~/edit.png"
                                        Height="32px" Width="32px" />
                                    <asp:ImageButton ID="imgbtnDelete" runat="server" command_name="Delete" ImageUrl="~/delete.png" />
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:ImageButton ID="imgbtnUpdate" runat="server" command_name="Update" ImageUrl="~/update.png" />
                                    <asp:ImageButton ID="imgbtnCancel" runat="server" command_name="Cancel" ImageUrl="~/Cancel.png" />
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:LinkButton ID="lbtnAdd" runat="server" command_name="ADD" Text="Add" Width="100px"></asp:LinkButton>
                                </FooterTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
          
        </table>
    </div>
    </form>
</body>
</html>

Default2.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 Default2 : System.Web.UI.Page
{

    SqlConnection conn = new SqlConnection("Data Source=SPIDER;Initial Catalog=Demo;Integrated Security=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 student";
        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)
        {
            grd_studentdetail.DataSource = FromTable;
            grd_studentdetail.DataBind();
        }
        else
        {
            FromTable.Rows.Add(FromTable.NewRow());
            grd_studentdetail.DataSource = FromTable;
            grd_studentdetail.DataBind();
            int TotalColumns = grd_studentdetail.Rows[0].Cells.Count;
            grd_studentdetail.Rows[0].Cells.Clear();
            grd_studentdetail.Rows[0].Cells.Add(new TableCell());
            grd_studentdetail.Rows[0].Cells[0].ColumnSpan = TotalColumns;
            grd_studentdetail.Rows[0].Cells[0].Text = "No records Found";
        }
        ds.Dispose();
        conn.Close();
    }

    protected void grd_studentdetail_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label lblstud_id = (Label)grd_studentdetail.Rows[e.RowIndex].FindControl("lblstud_id");

        conn.Open();
        string cmdstr = "delete from student where stud_id=@stud_id";
        SqlCommand cmd = new SqlCommand(cmdstr, conn);
        cmd.Parameters.AddWithValue("@stud_id", lblstud_id.Text);
        cmd.ExecuteNonQuery();
        conn.Close();
        BindData();

    }
    protected void grd_studentdetail_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.command_name.Equals("ADD"))
        {
            TextBox txtAddstud_id = (TextBox)grd_studentdetail.FooterRow.FindControl("txtAddstud_id");
            TextBox txtAddstudent_name = (TextBox)grd_studentdetail.FooterRow.FindControl("txtAddstudent_name");
            TextBox txtAddDateofjoin = (TextBox)grd_studentdetail.FooterRow.FindControl("txtAddDateofjoin");
            conn.Open();
            string cmdstr = "insert into student(stud_id,stud_name,dateofjoin) values(@stud_id,@stud_name,@dateofjoin)";
            SqlCommand cmd = new SqlCommand(cmdstr, conn);
            cmd.Parameters.AddWithValue("@stud_id", txtAddstud_id.Text);
            cmd.Parameters.AddWithValue("@stud_name", txtAddstudent_name.Text);
            cmd.Parameters.AddWithValue("@dateofjoin", txtAddDateofjoin.Text);

            cmd.ExecuteNonQuery();
            conn.Close();
            BindData();
        }
    }
    protected void grd_studentdetail_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lblEditstud_id = (Label)grd_studentdetail.Rows[e.RowIndex].FindControl("lblEditstud_id");
        TextBox txtEditstudent_name = (TextBox)grd_studentdetail.Rows[e.RowIndex].FindControl("txtEditstudent_name");
        TextBox txtEditCountry = (TextBox)grd_studentdetail.Rows[e.RowIndex].FindControl("txtEditCountry");
        conn.Open();
        string cmdstr = "update student set stud_name=@stud_name,designation=@designation,city=@city,dateofjoin=@dateofjoin where stud_id=@stud_id";
        SqlCommand cmd = new SqlCommand(cmdstr, conn);
        cmd.Parameters.AddWithValue("@stud_id", lblEditstud_id.Text);
        cmd.Parameters.AddWithValue("@stud_name", txtEditstudent_name.Text);
        cmd.Parameters.AddWithValue("@dateofjoin", txtEditCountry.Text);
        cmd.ExecuteNonQuery();
        conn.Close();

        grd_studentdetail.EditIndex = -1;
        BindData();

    }
    protected void grd_studentdetail_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grd_studentdetail.EditIndex = -1;
        BindData();
    }
    protected void grd_studentdetail_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grd_studentdetail.EditIndex = e.NewEditIndex;
        BindData();
    }
}



0 comments:

Post a Comment