在asp.net的gridview控件中查询有关自动编辑按钮的信息

时间:2019-04-30 06:36:41

标签: c# asp.net gridview

我在asp.net项目中使用gridview控件的自动编辑按钮。我单击编辑按钮时会遇到问题,它会触发gridview的rowcommand事件和rowediting事件,但是当我再次单击编辑按钮时,网格行却未进入编辑模式,现在网格行进入​​了编辑模式问题是为什么我需要单击两个编辑按钮次。

这是我的html(aspx)代码。

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" AutoGenerateEditButton="True">
           <Columns>                       
              <asp:BoundField HeaderText="Employee Number" DataField="empno" ReadOnly="True">
                  <ItemStyle Width="100px" />
              </asp:BoundField>
              <asp:TemplateField HeaderText="Employee Name">                         
              </asp:TemplateField>
           </Columns>
         <HeaderStyle CssClass="gridheader" />
        </asp:GridView>

        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
     </div>
    </form>
</body>
</html>

C#代码

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.Xml.Linq;
using System.Xml;

namespace LMS
{
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[1] {new DataColumn("empno", typeof(int))});
            for (int i = 0; i < 10; i++)
            {
                DataRow dr;
                dr = dt.NewRow();
                dr["empno"] = i.ToString("00");

                dt.Rows.Add(dr);

                GridView1.DataSource = dt;
                GridView1.DataBind();
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridView1.EditIndex = -1;
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

.DataBind添加到GridView1_RowEditing中:

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[1] { new DataColumn("empno", typeof(int)) });
    for (int i = 0; i < 10; i++)
    {
        DataRow dr;
        dr = dt.NewRow();
        dr["empno"] = i.ToString("00");

        dt.Rows.Add(dr);

        GridView1.DataSource = dt;
        Session["dt"] = dt;
        GridView1.DataBind();
    }
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    GridView1.DataSource = Session["dt"] as DataTable;
    GridView1.DataBind();
}