1
Answer

Event not Firing

Smart    Lucky

Smart Lucky

11y
1.1k
1
hi friends edit event is not working can nay one help me please

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


<!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>
   



    </div>
    <asp:GridView ID="GridView1" runat="server" ShowFooter="true" DataKeyNames="id" 
        CellPadding="4" ForeColor="#333333" AutoGenerateColumns="false"
        GridLines="Both" onrowediting="GridView1_RowEditing" 
        onrowcancelingedit="GridView1_RowCancelingEdit">
        <AlternatingRowStyle BackColor="White" />
        <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" />
        <Columns>
            <asp:TemplateField HeaderText="Opration">
                <EditItemTemplate>
        <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/images/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
        <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/images/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
    </EditItemTemplate>
    <ItemTemplate>
        <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/images/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" />
        <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
<%--<asp:LinkButton CommandArgument='<%#Eval("id") %>' runat="server" Text='<%#Eval("id") %>' CommandName="test"></asp:LinkButton>--%>
    </ItemTemplate>
    <FooterTemplate>
        <asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/images/AddNewitem.jpg" CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />
    </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name">
                <EditItemTemplate>
                    <asp:TextBox ID="namert" runat="server" Text='<%# Eval("name") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="namelbl" runat="server" Text='<%# Eval("name") %>' />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="addName" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="City">
                <EditItemTemplate>
                    <asp:TextBox ID="city" runat="server" Text='<%# Eval("city") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="citylbl" runat="server" Text='<%# Eval("city") %>' />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="addcity" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>













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


public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = null;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        try
        {
            con = new SqlConnection("Data Source=; Integrated Security=false; Initial Catalog=test; User ID=; Password=");
            dataBind();
        }
        catch (Exception m) { Response.Write("Connection can not Establishing<br/>"+m.Message); }
    }
    public void dataBind()
    {
        try
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from GridView", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            con.Close();
        }
        catch (Exception e) { Response.Write(e.Message); }
       
       
    }
   
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        dataBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        dataBind();
    }
}

Answers (1)