C# 单击列时要编辑的Gridview弹出窗口

C# 单击列时要编辑的Gridview弹出窗口,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个网格视图。我需要包括一个超链接到其中一列,所以当用户点击链接时,弹出窗口应该带有编辑和保存选项。保存后,gridview将自动刷新 以下是我的Gridview代码: <asp:GridView ID="EmployeeGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="Emp_id" onrowcancelingedit="EmployeeGridView_RowCan

我有一个网格视图。我需要包括一个超链接到其中一列,所以当用户点击链接时,弹出窗口应该带有编辑和保存选项。保存后,gridview将自动刷新

以下是我的Gridview代码:

 <asp:GridView ID="EmployeeGridView" runat="server" AutoGenerateColumns="False"
               DataKeyNames="Emp_id" onrowcancelingedit="EmployeeGridView_RowCancelingEdit" 
               onrowediting="EmployeeGridView_RowEditing" onrowdeleting="EmployeeGridView_RowDeleting" 
               onrowupdating="EmployeeGridView_RowUpdating" Width="395px" 
               CellPadding="4" ForeColor="#333333" GridLines="None" 
               onrowdatabound="EmployeeGridView_RowDataBound" AllowPaging="True" 
               onpageindexchanging="EmployeeGridView_PageIndexChanging1" PageSize="5">   
               <AlternatingRowStyle BackColor="White" />
    <Columns>
    <asp:TemplateField HeaderText = "Action"> 
                   <ItemTemplate> 
                        <asp:CheckBox ID = "chkDelete" runat = "server" AutoPostBack="True" 
                            oncheckedchanged="chkDelete_CheckedChanged" /> 
                        <br /> 
                    </ItemTemplate> 
            </asp:TemplateField> 

    <asp:TemplateField HeaderText="Sr.No">
                   <ItemTemplate>   
                       <asp:Label ID = "lblID" runat = "server" Text = '<%#Container.DataItemIndex+1 %>'></asp:Label>
                   </ItemTemplate> 
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Name">
                   <ItemTemplate> 
                        <asp:Label ID = "lblEmpName" runat = "server" Text = '<%# Eval("Emp_name") %>'></asp:Label> 
                    </ItemTemplate> 

                                    <EditItemTemplate>
                                         <asp:TextBox ID="txtempname" runat="server" Text='<%#Eval("Emp_name") %>'></asp:TextBox>
                                    </EditItemTemplate>
     </asp:TemplateField>    
     <asp:TemplateField HeaderText="Experience">
                   <ItemTemplate> 
                        <asp:Label ID = "lblEmpExp" runat = "server" Text = '<%# Eval("Emp_exp") %>'></asp:Label> 
                   </ItemTemplate> 

                                  <EditItemTemplate>
                                           <asp:TextBox ID="txtempexp" runat="server" Text='<%#Eval("Emp_exp") %>'></asp:TextBox>
                                  </EditItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField HeaderText="Address">
                    <ItemTemplate> 
                        <asp:Label ID = "lblEmpAddress" runat = "server" Text = '<%# Eval("Emp_address") %>'></asp:Label> 
                    </ItemTemplate> 

                                <EditItemTemplate>
                                            <asp:TextBox ID="txtempaddress" runat="server" Text='<%#Eval("Emp_address") %>'></asp:TextBox>
                                </EditItemTemplate>
    </asp:TemplateField>
                <asp:CommandField ShowEditButton="true" ButtonType ="Button"  
            HeaderText="Edit" ControlStyle-BackColor= "#15524A" >
<ControlStyle BackColor="#15524A"></ControlStyle>
        </asp:CommandField>
                <asp:CommandField ShowDeleteButton="true" ButtonType="Button" HeaderText="Delete" />  
    </Columns>

        <EditRowStyle BackColor="#7C6F57" />
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#E3EAEB" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F8FAFA" />
        <SortedAscendingHeaderStyle BackColor="#246B61" />
        <SortedDescendingCellStyle BackColor="#D4DFE1" />
        <SortedDescendingHeaderStyle BackColor="#15524A" />
    </asp:GridView>


背后的代码是:

public partial class _Default : System.Web.UI.Page

{
    SqlConnection connstr = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            protected void EmployeeGridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        Session["sortBy"] = e.SortExpression;
        FillGrid();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Session["sortBy"] = null;
        if (!Page.IsPostBack)
        {
            FillGrid();
            BindGrid();
        } 

    }
    public void FillGrid()
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("GetEmployeeInfo", con);
        SqlDataReader dr = cmd.ExecuteReader();//it reads froword only data from database
        DataTable dt = new DataTable();//object of data table that uses to conatin whole data
        dt.Load(dr);//Sql Data reader data load in data table it is DataTable Method.
        EmployeeGridView.DataSource = dt;
        EmployeeGridView.DataBind();

    }
    private void BindGrid()
    {
        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select Id, Name from tblFiles";
                cmd.Connection = con;
                con.Open();
                GridView1.DataSource = cmd.ExecuteReader();
                GridView1.DataBind();
                con.Close();
            }
        }
    }
    protected void EmployeeGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        EmployeeGridView.EditIndex = -1;
        FillGrid();

    }
    protected void EmployeeGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        EmployeeGridView.EditIndex = e.NewEditIndex;
        FillGrid();

    }

    protected void EmployeeGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int empid = Convert.ToInt32(EmployeeGridView.DataKeys[e.RowIndex].Value.ToString());//Get Each Row unique value from DataKeyNames
        string name = ((TextBox)EmployeeGridView.Rows[e.RowIndex].FindControl("txtempname")).Text;//get TextBox Value in EditItemTemplet that row is clicked
        string experience = ((TextBox)EmployeeGridView.Rows[e.RowIndex].FindControl("txtempexp")).Text;
        string address = ((TextBox)EmployeeGridView.Rows[e.RowIndex].FindControl("txtempaddress")).Text;
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("EmployeeUpdate", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@emp_id ", empid);
        cmd.Parameters.AddWithValue("@emp_name ", name);
        cmd.Parameters.AddWithValue("@emp_exp ", experience);
        cmd.Parameters.AddWithValue("@emp_address ", address);
        cmd.ExecuteNonQuery();//Sql Command Class method return effected rows use for insert,update, delete
        EmployeeGridView.EditIndex = -1;// no row in edit mode
        FillGrid();
    }
    protected void EmployeeGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int empid = Convert.ToInt32(EmployeeGridView.DataKeys[e.RowIndex].Value.ToString());
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("DeleteEmployee", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@emp_id ", empid);
        cmd.ExecuteNonQuery();
        FillGrid();

    }

    protected void EmployeeGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        EmployeeGridView.PageIndex = e.NewPageIndex;
        FillGrid();
    }

    protected void buttonDelete_Click(object sender, EventArgs e)

    {
        foreach (GridViewRow row in EmployeeGridView.Rows)
        {
            var chk = row.FindControl("chkDelete") as CheckBox;
            if (chk.Checked)
            {
                var lblID = row.FindControl("lblID") as Label;
                Response.Write(lblID.Text + "<br>");

                SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                SqlCommand comm = new SqlCommand("Delete  from tbl_employee where Emp_id=@emp_id", conn);
                // comm.CommandType = CommandType.StoredProcedure;
                conn.Open();
                comm.Parameters.AddWithValue("@emp_id", int.Parse(lblID.Text));

                comm.ExecuteNonQuery();
                conn.Close();
            }
       }
        FillGrid();
    }
    protected void buttonUpdate_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in EmployeeGridView.Rows)
        {

            var chk = row.FindControl("chkDelete") as CheckBox;
            if (chk.Checked)
            {

                SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

                var lblID = row.FindControl("lblID") as Label;
                var lblEmpName = row.FindControl("lblEmpName") as Label;
                var lblEmpExp = row.FindControl("lblEmpExp") as Label;
                var lblEmpAddress = row.FindControl("lblEmpAddress") as Label;
                //Response.Write(lblFirstName.Text + "<br>");  
                SqlCommand comm = new SqlCommand("EmployeeUpdate", conn);
                comm.CommandType = CommandType.StoredProcedure;
                comm.Parameters.AddWithValue("@emp_id", int.Parse(lblID.Text));
                comm.Parameters.AddWithValue("@emp_name", EmpName.Text);
                comm.Parameters.AddWithValue("@emp_exp", EmpExp.Text);
                comm.Parameters.AddWithValue("@Emp_address", EmpAddress.Text);

                conn.Open();
                comm.ExecuteNonQuery();
                conn.Close();
                EmpName.Visible = false;
                EmpExp.Visible = false;
                EmpAddress.Visible = false;
            }

        }
        FillGrid();
    }
}
public分部类\u默认值:System.Web.UI.Page
{
SqlConnection connstr=新的SqlConnection(System.Configuration.ConfigurationManager.ConnectionString[“ConnectionString”].ConnectionString);
受保护的无效EmployeeGridView_排序(对象发送方,GridViewSortEventArgs e)
{
会话[“sortBy”]=e.SortExpression;
FillGrid();
}
受保护的无效页面加载(对象发送方、事件参数e)
{
会话[“sortBy”]=null;
如果(!Page.IsPostBack)
{
FillGrid();
BindGrid();
} 
}
公共空间填充网格()
{
SqlConnection con=新的SqlConnection(System.Configuration.ConfigurationManager.ConnectionString[“ConnectionString”].ConnectionString);
con.Open();
SqlCommand cmd=newsqlcommand(“GetEmployeeInfo”,con);
SqlDataReader dr=cmd.ExecuteReader();//它只从数据库中读取froword数据
DataTable dt=new DataTable();//用于保存整个数据的数据表的对象
dt.Load(dr);//Sql数据读取器在数据表中加载数据它是DataTable方法。
EmployeeGridView.DataSource=dt;
EmployeeGridView.DataBind();
}
私有void BindGrid()
{
string constr=ConfigurationManager.ConnectionString[“ConnectionString”].ConnectionString;
使用(SqlConnection con=newsqlconnection(cont))
{
使用(SqlCommand cmd=new SqlCommand())
{
cmd.CommandText=“从tblFiles中选择Id、名称”;
cmd.Connection=con;
con.Open();
GridView1.DataSource=cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
}
}
受保护的无效EmployeeGridView\u RowCancelingEdit(对象发送方,GridViewCancelEditEventArgs e)
{
EmployeeGridView.EditIndex=-1;
FillGrid();
}
受保护的无效EmployeeGridView\u行编辑(对象发送方,GridViewEditEventArgs e)
{
EmployeeGridView.EditIndex=e.NewEditIndex;
FillGrid();
}
受保护的无效EmployeeGridView\u行更新(对象发送方,GridViewUpdateEventArgs e)
{
int empid=Convert.ToInt32(EmployeeGridView.DataKeys[e.RowIndex].Value.ToString());//从DataKeyNames中获取每一行的唯一值
字符串名称=((TextBox)EmployeeGridView.Rows[e.RowIndex].FindControl(“txtempname”)).Text;//在单击该行的EditItemTemplet中获取TextBox值
字符串经验=((文本框)EmployeeGridView.Rows[e.RowIndex].FindControl(“txtempexp”).Text;
字符串地址=((文本框)EmployeeGridView.Rows[e.RowIndex].FindControl(“txtempaddress”).Text;
SqlConnection con=新的SqlConnection(System.Configuration.ConfigurationManager.ConnectionString[“ConnectionString”].ConnectionString);
con.Open();
SqlCommand cmd=newsqlcommand(“EmployeeUpdate”,con);
cmd.CommandType=CommandType.storedProcess;
cmd.Parameters.AddWithValue(“@emp_id”,empid);
cmd.Parameters.AddWithValue(“@emp_name”,name);
cmd.Parameters.AddWithValue(“@emp_exp”,experience);
cmd.Parameters.AddWithValue(“@emp_address”,地址);
cmd.ExecuteNonQuery();//Sql命令类方法返回用于插入、更新和删除的受影响行
EmployeeGridView.EditIndex=-1;//编辑模式下没有行
FillGrid();
}
受保护的无效EmployeeGridView_行删除(对象发送方,GridViewDeleteEventArgs e)
{
int empid=Convert.ToInt32(EmployeeGridView.DataKeys[e.RowIndex].Value.ToString());
SqlConnection con=新的SqlConnection(System.Configuration.ConfigurationManager.ConnectionString[“ConnectionString”].ConnectionString);
con.Open();
SqlCommand cmd=新的SqlCommand(“deletemployee”,con);
cmd.CommandType=CommandType.storedProcess;
cmd.Parameters.AddWithValue(“@emp_id”,empid);
cmd.ExecuteNonQuery();
FillGrid();
}
受保护的无效EmployeeGridView\u页面索引交换(对象发送方,GridViewPageEventArgs e)
{
EmployeeGridView.PageIndex=e.NewPageIndex;
FillGrid();
}
受保护的无效按钮删除\单击(对象发送者,事件参数e)
{
foreach(EmployeeGridView.Rows中的GridViewRow行)
{
var chk=row.FindControl(“chkDelete”)作为复选框;
如果(已检查)
{
var lblID=row.FindControl(“lblID”)作为标签;
响应。写入(lblID.Text+“
”; SqlConnection conn=新的SqlConnection(System.Configuration.ConfigurationManager.ConnectionString[“ConnectionString