无法使用命令事件删除gridview行 [英] Unable to delete a gridview row using command event

查看:49
本文介绍了无法使用命令事件删除gridview行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Emp.aspx.cs" Inherits="Test17716.Emp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>Name</td>
                    <td>
                        <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Address</td>
                    <td>
                        <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Qualification</td>
                    <td>
                        <asp:DropDownList ID="ddlqualification" runat="server">
                            <%-- <asp:ListItem Text="mca" Value="1"></asp:ListItem>
                            <asp:ListItem Text="mba" Value="2"></asp:ListItem>
                            <asp:ListItem Text="b.tech" Value="3"></asp:ListItem>
                            <asp:ListItem Text="bca" Value="4"></asp:ListItem>--%>
                        </asp:DropDownList></td>
                </tr>
                <tr>
                    <td>Gender</td>
                    <td>
                        <asp:RadioButtonList ID="rdbGender" runat="server" RepeatColumns="2">
                            <%--<asp:ListItem Text="MALE" Value="1"></asp:ListItem>--%>
                        </asp:RadioButtonList></td>
                </tr>
                <tr>
                    <td>Hobies</td>
                    <td>
                        <asp:CheckBoxList ID="Chklist" runat="server" RepeatColumns="3"></asp:CheckBoxList></td>
                </tr>
                <tr>
                    <td>Files</td>
                    <td>
                        <asp:FileUpload ID="fufile" runat="server"></asp:FileUpload></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <asp:GridView ID="grd" DataKeyNames="Eid" runat="server"  OnRowCommand="grd_RowCommand" AutoGenerateColumns="False">
                            <Columns>

                                <asp:TemplateField HeaderText="Name">
                                    <ItemTemplate>
                                        <a href="employeeDtaials.aspx?Eid=<%#Eval("Eid") %>"><%#Eval("Name") %></a>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Address">
                                    <ItemTemplate>
                                        <%#Eval("Address") %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Qualification">
                                    <ItemTemplate>
                                        <%#Eval("QName") %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Gender">
                                    <ItemTemplate>
                                        <%#Eval("Gender") %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Hobbies">
                                    <ItemTemplate>
                                        <%#Eval("Hobbies") %>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="FileName">
                                    <ItemTemplate>
                                        <%--<a href="employeeDtaials.aspx?Eid=<%#Eval("Eid") %>">--%>
                                            <img src="/Files/<%#Eval("FileName") %>" style="width: 50px">
                                        </a>

                                        &nbsp;&nbsp;

                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Action">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkEdit" Text="View" CommandArgument='<%#Eval("Eid") %>' CommandName="EDT" runat="server"></asp:LinkButton>
                                        <asp:LinkButton ID="lnkDelete" Text="Delete" CommandArgument='<%#Eval("Eid") %>' CommandName="DEL" runat="server"></asp:LinkButton>
                                        
                                    </ItemTemplate>

                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>


            </table>

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





CS Code在这里:





CS Code goes here:

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;
using System.Configuration;
using System.IO;

namespace Test17716
{
    public partial class Emp : System.Web.UI.Page
    {
        SqlCommand cmd;
        SqlDataAdapter da;
        SqlDataReader dr;
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["abc"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               Fill_Qualification();
               RdbBind();
               Hobies();
               fill_Grid();
            }
        }

        public void Fill_Qualification()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_Qualification_Select", con);
            cmd.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            SqlDataAdapter sdr = new SqlDataAdapter(cmd);
            sdr.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ddlqualification.DataValueField = "QID";
                ddlqualification.DataTextField = "QName";
                ddlqualification.DataSource = ds;
                ddlqualification.DataBind();
                ddlqualification.Items.Insert(0,new ListItem("--Select--","0"));
            }
            con.Close();
        }

        public void Hobies()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Myhobies", con);
            cmd.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                Chklist.DataValueField = "HID";
                Chklist.DataTextField = "HobiesNAme";
                Chklist.DataSource = ds;
                Chklist.DataBind();

            }
            con.Close();
        }

        public void RdbBind()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("GenderBind", con);
            cmd.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                rdbGender.DataValueField = "GID";
                rdbGender.DataTextField = "GName";
                rdbGender.DataSource = ds;
                rdbGender.DataBind();
               
            }
            con.Close();
        }
        public void fill_Grid()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("usp_Emp_Get", con);
            cmd.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            SqlDataAdapter sdr = new SqlDataAdapter(cmd);
            sdr.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                grd.DataSource = ds;
                grd.DataBind();
            }
            con.Close();
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            string HOB = "";
            for (int i = 0; i < Chklist.Items.Count; i++)
            {
                if (Chklist.Items[i].Selected == true)
                {
                    HOB += Chklist.Items[i].Text + ",";
                }
            }
            HOB = HOB.TrimEnd(',');

            string FN = Path.GetFileName(fufile.PostedFile.FileName);
            fufile.SaveAs(Server.MapPath("Files" + "\\" + FN));

            con.Open();
            SqlCommand cmd = new SqlCommand("usp_Emp_Insert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Name", txtname.Text);
            cmd.Parameters.AddWithValue("@Address", txtaddress.Text);
            cmd.Parameters.AddWithValue("@Qualification", ddlqualification.SelectedValue);
            cmd.Parameters.AddWithValue("@GID", rdbGender.SelectedValue);
            cmd.Parameters.AddWithValue("@Hobbies", HOB);
            cmd.Parameters.AddWithValue("@FileName", FN);
            cmd.ExecuteNonQuery();
            con.Close();
            fill_Grid();

            txtaddress.Text = "";
            txtname.Text = "";

        }

        protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DEL")
            {
                  con.Open();
                  LinkButton lnkbtn = sender as LinkButton;
                  GridViewRow grdRow = lnkbtn.NamingContainer as GridViewRow;
                  string EmpId = grd.DataKeys[grdRow.RowIndex].Value.ToString();
                
                
                cmd = new SqlCommand("sp_Emp_Delete",con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Id", EmpId);
                
                cmd.ExecuteNonQuery();
                con.Close();
            }
            else
            {

            }

            }
           
        }

        
    }







Please help me. I am confused how to use GridView Row command event.



What I have tried:



I have used grid view row command event but i am unable to delete the row.




Please help me. I am confused how to use GridView Row command event.

What I have tried:

I have used grid view row command event but i am unable to delete the row.

推荐答案

Ok so what you need to do is inside your method

Ok so what you need to do is inside your method
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)



..after the delete is executed, immediately re-bind the grid control to the updated data set.



Your page load only binds the first time page loads, so this is ok for users seeing the page for the first time. Postback commands like you \"DEL\" should refresh controls within before the method exits.


..after the delete is executed, immediately re-bind the grid control to the updated data set.

Your page load only binds the first time page loads, so this is ok for users seeing the page for the first time. Postback commands like you "DEL" should refresh controls within before the method exits.


Call your fill_Grid() method after executing delete to reflect the changes you’ve made in your GridView.

Call your fill_Grid() method after executing delete to reflect the changes you've made in your GridView.
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e){
            if (e.CommandName == "DEL")
            {
                  con.Open();
                  LinkButton lnkbtn = sender as LinkButton;
                  GridViewRow grdRow = lnkbtn.NamingContainer as GridViewRow;
                  string EmpId = grd.DataKeys[grdRow.RowIndex].Value.ToString();
                
                
                cmd = new SqlCommand("sp_Emp_Delete",con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Id", EmpId);
                
                cmd.ExecuteNonQuery();
                con.Close();

                fill_Grid();//this will repopulate your grid with the changes
            }
}          





I would also suggest you to debug your code. Set a break point at RowCommand event, then step into your codes to figure out what’s happening.



I would also suggest you to debug your code. Set a break point at RowCommand event, then step into your codes to figure out what's happening.


这篇关于无法使用命令事件删除gridview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆