Ajax UpdatePanel Use Update,Delete in Gridview Avoiding Page Refreshing Using Asp.Net C#

Ajax UpdatePanel Using Update,Delete in Gridview 

Using Ajax UpdatePanel  update,delete process in Gridview control for avoiding page refreshing Using in Asp.Net C#.

Insert,Update,Delete in Gridview Check Following Link

                             
                                  DEMO

                               

                               Download
                 
                        HTML CODING


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ajax UpdatePanel Use Update,Delete in Gridview Using Asp.Net C#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="Center">
        <br />
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"><ContentTemplate>
            <h4>Ajax UpdatePanel Using Update,Delete in Gridview</h4>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
                <asp:TemplateField HeaderText="Employee Id">
                    <EditItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Employee Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Empname") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Empname") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

        </asp:GridView>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="GridView1" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbcon %>" SelectCommand="SELECT * FROM [Employee]"></asp:SqlDataSource>
    </form>
</body>
</html>


                                               C# CODING

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


public partial class Updatepanelsss : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridBind();       
        }
    }
    protected void GridBind()
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Employee", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        SqlDataReader rd = cmd.ExecuteReader();
        if (rd.Read())
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();           
        }
        con.Close();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label lblEmpid = (Label)GridView1.Rows[e.RowIndex].FindControl("Label1");
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from Employee where Id='"+lblEmpid.Text+"'", con);
        cmd.ExecuteNonQuery();
        con.Close();

        ScriptManager.RegisterStartupScript(sender as Control,this.GetType(),"Scripts","alert('Data Deleted')",true);

        GridBind();

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lblEmpid = (Label)GridView1.Rows[e.RowIndex].FindControl("Label3");
        TextBox txtEmpname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("update Employee set Empname='"+txtEmpname.Text+"' where Id='" + lblEmpid.Text + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();

        ScriptManager.RegisterStartupScript(sender as Control, this.GetType(), "Scriptsss", "alert('Data Updated')", true);

        GridView1.EditIndex = -1;
        GridBind();
    }

}





Frist - Create New Webform - add 

Scriptmanager,Updatapanel gridview from toolbox




Next - Updatepanel - Property - Select Collections - Add AsyncPostBack - add Control id = Gridview1 - Ok . Next - UpdateMode = Conditional(Updatepanel Property).





Next - Html Code  Trigger  control id = Gridview1 shown below 





Next - Add C# Coding - Important need generate popup using ScriptManager.RegisterStatrupScript 


0 comments:

Post a Comment