Monday 29 June 2015

Gridview Edit code using modalpopup in asp.net with c#: ?


Hi Guys,
 This blog we explain how to edit gridview using Ajax modalpopup Extender.

Modaledit.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="modaledit.aspx.cs" Inherits="modaledit" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%--<%@ Register As" %>--%>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1
        {
            width: 62%;
        }
        .auto-style2
        {
            width: 156px;
        }
    </style>
    <style type="text/css">
        .modalBackground {
background-color:Gray;
filter:alpha(opacity=70);
opacity:0.7;
z-index:10000;
}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmpId">
          <Columns>
              <asp:BoundField DataField="EmpId" HeaderText="EmpId" />
              <asp:BoundField DataField="Name" HeaderText="Name" />
              <asp:BoundField DataField="City" HeaderText="City" />
              <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
                  <asp:ImageButton ID="myimage" ImageUrl="~/Images/edit_it.jpg" runat="server" Height="20" Width="20" OnClick="image_btnclick" />
                </ItemTemplate>
              </asp:TemplateField>
          </Columns>
        </asp:GridView>
        <asp:Button ID="btnshow" runat="server" Text="ShowPopup" />
        <asp:Label ID="lblresult" runat="server" Text="Result"></asp:Label>
        <asp:ModalPopupExtender ID="sanmodal" runat="server" TargetControlID="btnshow" CancelControlID="btnCancel" BackgroundCssClass="modalBackground" PopupControlID="Panel1"></asp:ModalPopupExtender>
        <asp:Panel ID="Panel1" BorderColor="#333399" runat="server"  Width="500px" Height="200" ForeColor="Black">
            <table style="background-color:yellow;border-right-style:dotted;" class="auto-style1">
                <tr>
                    <td class="auto-style2">EmpId</td>
                    <td>
                        <asp:TextBox ID="txtEmpId" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2">Name</td>
                    <td>
                        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2">City</td>
                    <td>
                        <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2">&nbsp;</td>
                    <td>
                        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="update_it" CommandName="Save" />
                        <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
                        <asp:Label ID="lblmsg" runat="server" Text="Msg"></asp:Label>
                    </td>
                </tr>
            </table>
        </asp:Panel>
    </form>
</body>
</html>

using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Drawing;

public partial class modaledit : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sai"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bindme();
        }
    }
    protected void bindme()
    {
        SqlDataAdapter sdr = new SqlDataAdapter("select * from sanmodal", con);
        DataSet ds = new DataSet();
        sdr.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void update_it(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("update sanmodal set Name=@Name,City=@City where EmpId=@EmpId",con);
        con.Open();
        cmd.Parameters.AddWithValue("Name", txtName.Text);
        cmd.Parameters.AddWithValue("City", txtCity.Text);
        cmd.Parameters.AddWithValue("EmpId", Convert.ToInt32(txtEmpId.Text));
        cmd.ExecuteNonQuery();
        con.Close();
        lblresult.Text = "Details update Successfully";
        bindme();
         
    }
    protected void image_btnclick(object sender, EventArgs e)
    {
        ImageButton btndetails = sender as ImageButton;
        GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
        txtEmpId.Text = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();
        txtName.Text = gvrow.Cells[1].Text;
        txtCity.Text = gvrow.Cells[2].Text;
        this.sanmodal.Show();
       
    }
}

No comments:

Post a Comment