Edit,Delete with GridView in Asp.net || Save data with ModalPopUp Extender show in Gridview with Edit,Delete Options in Asp.net || ModalPoPUp Extender with GridView in Asp.net


The following article is showing about Save data with ModalPopUp Extender show in Gridview with Edit,Delete Options in Asp.net
Onething you should remember that, if you use any Ajax toolkit controls in your web page then you must add ScriptManager tag.
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
Create a Web Form named as " MPGridview  .aspx".
Copy and paste the following code in your MPGridView .aspx section.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 42%;
            height: 131px;
        }
        .style3
        {
            width: 153px;
        }
        .style5
        {
            width: 90px;
        }
        .style6
        {
            width: 89px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </asp:ToolkitScriptManager>
    <div>
    <asp:Button ID="btnShowPopup" runat="server" style="display:none" />

      <asp:ModalPopupExtender ID="Modlpop" runat ="server" TargetControlID="btnShowPopup"
    PopupControlID ="pnlShow" CancelControlID="btnClose"
            BackgroundCssClass="modalBackground" DropShadow="True"
            PopupDragHandleControlID="pnlShow">
    </asp:ModalPopupExtender>

    <asp:Panel ID="pnlShow" runat ="server" style="display:block;">
    <div style="border:Solid 3px #D55500;background-color:#D55500; text-align:left;">
   
   
        <table class="style1">
            <tr>
                <td class="style6">
                    <asp:Label ID="lblProductID" runat="server" Text="Product_ID"></asp:Label>
                </td>
                <td class="style3">
                    <asp:TextBox ID="txtProductID" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style6">
                    <asp:Label ID="lblProductName" runat="server" Text="Product Name"></asp:Label>
                </td>
                <td class="style3">
                    <asp:TextBox ID="txtProductName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style6">
                    <asp:Label ID="lblAmount" runat="server" Text="Amount"></asp:Label>
                </td>
                <td class="style3">
                    <asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style6">
                    <asp:Button ID="btnSave" runat="server" Text="Save"onclick="btnSave_Click" />
                </td>
                <td class="style3">
                    <asp:Button ID="btnClose" runat="server" Text="close" />
                </td>
            </tr>
        </table>
    </div>
    </asp:Panel>
        <asp:GridView ID="grd1" runat="server" AutoGenerateColumns="False"
            onrowcommand="grd1_RowCommand" CellPadding="4" ForeColor="#333333"
            GridLines="None">
            <AlternatingRowStyle BackColor="White" />
        <Columns>
        <asp:TemplateField HeaderText="Product_ID">
        <ItemTemplate>
        <asp:Label ID="lblProID" runat ="server" Text ='<%#Eval("Product_ID") %> '></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Product_Name">
        <ItemTemplate>
        <asp:Label ID="lblProName" runat ="server" Text ='<%# Eval("Product_Name") %> '></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Amount">
        <ItemTemplate>
        <asp:Label ID="lblProAmount" runat ="server" Text ='<%#Eval("Amount")%> '></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Edit">
        <ItemTemplate>
        <asp:LinkButton CommandName="cmdBind"  runat="server" Text='<%#Eval("Product_ID")%> ' ID="hypeno"></asp:LinkButton>
        </ItemTemplate>
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Delete">
        <ItemTemplate>
        <asp:LinkButton CommandName="cmdDelBind" OnClientClick ="return confirm('Are You Sure Deletion?');"
         runat="server"  Text="Delete" ID="LinkButton1"></asp:LinkButton>
         </ItemTemplate>
        </asp:TemplateField>
        </Columns>

            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center"/>
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333"/>
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>


    </div>
    </form>
</body>
</html>


Then Write the following code in your MPGridview .aspx.cs file

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

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=naresh;Initial Catalog=Register;User ID=sa;Password=123");
    protected void Page_Load(object sender, EventArgs e)
    {
        BindList();
        Modlpop.Show();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (btnSave.Text != "Update")
        {
            con.Open();
            SqlCommand cmd =new SqlCommand("Insert into Product values ('" + txtProductID.Text + "','" + txtProductName.Text + "','" + txtAmount.Text + "')", con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
        }
        else
        {
            con.Open();
            SqlCommand cmd =
                new SqlCommand("Update Product set Product_ID='" + txtProductID.Text +"',Product_Name='" + txtProductName.Text + "',Amount='" + txtAmount.Text + "' where Product_ID='" + Session["EditId"] + "'", con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
        }
        BindList();
    }

    private void BindList()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from Product", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        grd1.DataSource = ds;
        grd1.DataBind();
    }
    protected void grd1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "cmdBind")
        {
            LinkButton lb = (LinkButton)e.CommandSource;
            Session["EditId"] = lb.Text;
            DataTable dt = new DataTable();
            SqlDataAdapter sqlda = new SqlDataAdapter("SELECT * FROM Product WHERE Product_ID ='" + Session["EditId"] + "'", con);
            sqlda.Fill(dt);
            txtProductID.Text = dt.DefaultView[0][0].ToString();
            txtProductName.Text= dt.DefaultView[0][1].ToString();
            txtAmount.Text = dt.DefaultView[0][2].ToString();
            btnSave.Text = "Update";
            BindList();
        }

        if (e.CommandName == "cmdDelBind")
        {
            LinkButton lb = (LinkButton)e.CommandSource;
            Session["DelitId"] = lb.Text;
            con.Open();
            SqlCommand cmd = new SqlCommand("Delete From Product where  Product_ID='" + Session["DelitId"] + "'", con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            txtProductID.Text = "";
            txtProductName.Text= "";
            txtAmount.Text= "";
            BindList();
        }
    }
}

 Run your application and see the output like below:


0 comments:

Post a Comment