none
也许这是一个GridView的Bug?每隔4行插入一行分隔行时,无法触发RowUpdating事件,却意外的触发了下一行的Editing事件 RRS feed

  • 问题

  • 我想要的效果或许你觉得没什么难的,但是,我确认这个问题真的不好解决!

    目标:
    是做一个数据绑定到GridView的效果,但要完全具备以下效果:
    1、可以编辑更新行;
    2、每隔4行,插入一个分割行,在该分割行内显示一条"============"字符串;

    问题:
    编辑更新行非常好做,这没有什么,但是,一旦我实现了第二目标(即:每隔4行插入一个分隔行)时,却意外的发现:
    除了第一个分隔行之前的行,进行编辑没有问题,其他的行,在进行Edit后进行Update更新,均出现不触发执行RowUpdating事件,却意外的执行了下一行的RowEditing事件,所以非常困惑,研究了一周的时间,没有任何进展。

    代码如下:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPage08.aspx.cs" Inherits="TestPage_TestPage08" %>
    
    
    
    <!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>
    
            <asp:GridView ID="GridView1" runat="server">
    
                <Columns>
    
                    <asp:BoundField HeaderText="ID" DataField="ID" ReadOnly="true" SortExpression="ID" />
    
                    <asp:TemplateField HeaderText="新闻标题" SortExpression="Title">
    
                        <ItemTemplate>
    
                            <asp:HyperLink ID="editTitle0" Text='<%# Eval("Title") %>' Target="_blank" runat="server"></asp:HyperLink>
    
                        </ItemTemplate>
    
                        <EditItemTemplate>
    
                            <asp:TextBox ID="editTitle1" Text='<%# Eval("Title") %>' runat="server"></asp:TextBox>
    
                        </EditItemTemplate>
    
                    </asp:TemplateField>
    
                </Columns>
    
            </asp:GridView>
    
        </div>
    
        </form>
    
    </body>
    
    </html>
    
    
    
    
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    
    using KingsLee_DataBaseManager_V40;//数据管理对象所在的命名空间,非常确认:这和我这个问题没有任何关系
    
    public partial class TestPage_TestPage08 : System.Web.UI.Page
    {
        KLDBManager dbm;//数据库操作对象,不用管它,非常确认:这和我这个问题没有任何关系
        protected void Page_Load(object sender, EventArgs e)
        {
            dbm = new KLDBManager();
    
            GridView1.DataKeyNames = new string[] { "ID" };
    
            GridView1.AutoGenerateColumns = false;
            GridView1.AutoGenerateEditButton = true;
    
            GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
            GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
            GridView1.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);
    
            GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
    
            if (!IsPostBack)
            {
                ToBind();
            }
        }
    
        void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            int _perRowCount = 4;
            int _columnSpan = 3;
            if (_perRowCount > 0 && e.Row.RowIndex >= 0)
            {
                if ((e.Row.RowIndex + 1) != 0 && (e.Row.RowIndex + 1) % _perRowCount == 0)
                {
                    GridViewRow _row = new GridViewRow(0, e.Row.RowIndex, DataControlRowType.Separator, DataControlRowState.Normal);
                    TableCell _cell = new TableCell();
                    _cell.ColumnSpan = _columnSpan;
                    _cell.Text = "=========================";
                    _row.Cells.Add(_cell);
                    GridView1.Controls[0].Controls.Add(_row);
                    //GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + 1, _row);
                }
            }
        }
    
        void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string _title = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("editTitle1")).Text.Trim();
            string _id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string sqlEdit = "UPDATE TDL_Article SET Title='" + _title + "' WHERE ID=" + _id;
            dbm.ToDo(sqlEdit, false, false);
            GridView1.EditIndex = -1;
            ToBind();
        }
    
        void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            ToBind();
        }
    
        void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            ToBind();
        }
    
        void ToBind()
        {
            string sqlString1 = "SELECT TOP 20 ID,Title FROM TDL_Article";
            DataSet ds = dbm.GetDs(sqlString1);//获得DataSet,不用管它,非常确认:这和我这个问题没有任何关系
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
    
    2010年3月28日 3:32

答案

  • 你好,

    根据你的代码我这边做了个测试,没有发现你的问题。你可以按照我下面的代码测试试试。

    <%@ Page Language="C#" %>
    
    <%@ Register src="../WebUserControl2.ascx" tagname="WebUserControl2" tagprefix="uc1" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Import Namespace="System.Data" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            
           
            int _perRowCount = 1;
            int _columnSpan = 3;
            if (_perRowCount > 0 && e.Row.RowIndex >= 0)
            {
                if ((e.Row.RowIndex + 1) != 0 && (e.Row.RowIndex + 1) % _perRowCount == 0)
                {
                    GridViewRow _row = new GridViewRow(0, e.Row.RowIndex, DataControlRowType.Separator, DataControlRowState.Normal);
                    TableCell _cell = new TableCell();
                    _cell.ColumnSpan = _columnSpan;
                    _cell.Text = "=========================";
                    _row.Cells.Add(_cell);
                    GridView1.Controls[0].Controls.Add(_row);
                    //GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + 1, _row);
                }
            }
    
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.AutoGenerateEditButton = true;
            if (!this.IsPostBack)
            {
                this.GridView1.DataSource = GetImagesTab;
                this.GridView1.DataBind();
            }
    
        }
    
       
    
    
    
        protected DataTable GetImagesTab
        {
            get
            {
                DataSet ds = new DataSet();
                if (ViewState["ds"] == null)
                {
                    using (SqlConnection scon = new SqlConnection(this.SqlDataSource1.ConnectionString))
                    {
                        SqlCommand scom = scon.CreateCommand();
                        scom.CommandText = "SELECT Id,Name Name FROM [Category]";
                        SqlDataAdapter sda = new SqlDataAdapter(scom);
                        sda.Fill(ds);
                        ViewState["ds"] = ds;
                    }
                    return ds.Tables[0];
    
                }
                else
                {
                    ds = ViewState["ds"] as DataSet;
                    return ds.Tables[0];
                }
    
    
            }
            set
            {
                ViewState["ds"] = value;
            }
        }
    
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value);
            string name = (this.GridView1.Rows[e.RowIndex].Cells[1].FindControl("editTb") as TextBox).Text;
            using (SqlConnection scon = new SqlConnection(this.SqlDataSource1.ConnectionString))
            {
                scon.Open();
                SqlCommand scom = scon.CreateCommand();
                scom.CommandText = string.Format("update [Category] set Name='{0}' where Id='{1}'",name,id);;
                scom.ExecuteNonQuery();
                scon.Close();          
            }
            e.Cancel = true;
            this.GridView1.EditIndex = -1;
            GetImagesTab = null;
            this.GridView1.DataSource = GetImagesTab;
            this.GridView1.DataBind(); 
            
        }
    
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
    
        }
    
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            this.GridView1.DataSource = GetImagesTab;
            this.GridView1.DataBind(); 
        }
    
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            this.GridView1.DataSource = GetImagesTab;
            this.GridView1.DataBind(); 
        }
    </script>
    
    <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" AutoGenerateColumns="False" 
                DataKeyNames="Id"
                onrowcreated="GridView1_RowCreated" onrowupdating="GridView1_RowUpdating" 
                onrowediting="GridView1_RowEditing" 
                onrowcancelingedit="GridView1_RowCancelingEdit">
                <Columns>
                   
           <%--         <asp:CommandField ShowEditButton="True" />--%>
                   
                    <asp:TemplateField>
                    <ItemTemplate>
                    <%# DataBinder.Eval((Container as GridViewRow).DataItem,"Name") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                    
                    <asp:TextBox runat="server" ID="editTb"></asp:TextBox>
                    </EditItemTemplate>
                    </asp:TemplateField>              
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:test_EmployeeInfo_SKConnectionString %>" 
                SelectCommand="SELECT [Name], [Id] FROM [Category]" >
            </asp:SqlDataSource>
        
        </div>
    
        </form>
    </body>
    </html>
    

     


    Microsoft Online Community Support
    2010年3月31日 2:45

全部回复

  • 你好,

    你是怎么触发edit的,代码上看不出你在哪可以进行编辑。


    Microsoft Online Community Support
    2010年3月30日 5:25
  • 你好,

    你是怎么触发edit的,代码上看不出你在哪可以进行编辑。


    Microsoft Online Community Support


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

     

    谢谢!~

    2010年3月30日 6:35
  • 不好意思,我是说页面上是怎么触发的。


    Microsoft Online Community Support
    2010年3月30日 9:28
  • 不好意思,我是说页面上是怎么触发的。


    Microsoft Online Community Support


    对不起,可能是你没有注意到我在Page_Load中写了一行默认编辑列设置的语句:
    GridView1.AutoGenerateEditButton = true;

    2010年3月31日 0:28
  • 你好,

    根据你的代码我这边做了个测试,没有发现你的问题。你可以按照我下面的代码测试试试。

    <%@ Page Language="C#" %>
    
    <%@ Register src="../WebUserControl2.ascx" tagname="WebUserControl2" tagprefix="uc1" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Import Namespace="System.Data" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            
           
            int _perRowCount = 1;
            int _columnSpan = 3;
            if (_perRowCount > 0 && e.Row.RowIndex >= 0)
            {
                if ((e.Row.RowIndex + 1) != 0 && (e.Row.RowIndex + 1) % _perRowCount == 0)
                {
                    GridViewRow _row = new GridViewRow(0, e.Row.RowIndex, DataControlRowType.Separator, DataControlRowState.Normal);
                    TableCell _cell = new TableCell();
                    _cell.ColumnSpan = _columnSpan;
                    _cell.Text = "=========================";
                    _row.Cells.Add(_cell);
                    GridView1.Controls[0].Controls.Add(_row);
                    //GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + 1, _row);
                }
            }
    
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.AutoGenerateEditButton = true;
            if (!this.IsPostBack)
            {
                this.GridView1.DataSource = GetImagesTab;
                this.GridView1.DataBind();
            }
    
        }
    
       
    
    
    
        protected DataTable GetImagesTab
        {
            get
            {
                DataSet ds = new DataSet();
                if (ViewState["ds"] == null)
                {
                    using (SqlConnection scon = new SqlConnection(this.SqlDataSource1.ConnectionString))
                    {
                        SqlCommand scom = scon.CreateCommand();
                        scom.CommandText = "SELECT Id,Name Name FROM [Category]";
                        SqlDataAdapter sda = new SqlDataAdapter(scom);
                        sda.Fill(ds);
                        ViewState["ds"] = ds;
                    }
                    return ds.Tables[0];
    
                }
                else
                {
                    ds = ViewState["ds"] as DataSet;
                    return ds.Tables[0];
                }
    
    
            }
            set
            {
                ViewState["ds"] = value;
            }
        }
    
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value);
            string name = (this.GridView1.Rows[e.RowIndex].Cells[1].FindControl("editTb") as TextBox).Text;
            using (SqlConnection scon = new SqlConnection(this.SqlDataSource1.ConnectionString))
            {
                scon.Open();
                SqlCommand scom = scon.CreateCommand();
                scom.CommandText = string.Format("update [Category] set Name='{0}' where Id='{1}'",name,id);;
                scom.ExecuteNonQuery();
                scon.Close();          
            }
            e.Cancel = true;
            this.GridView1.EditIndex = -1;
            GetImagesTab = null;
            this.GridView1.DataSource = GetImagesTab;
            this.GridView1.DataBind(); 
            
        }
    
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
    
        }
    
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            this.GridView1.DataSource = GetImagesTab;
            this.GridView1.DataBind(); 
        }
    
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            this.GridView1.DataSource = GetImagesTab;
            this.GridView1.DataBind(); 
        }
    </script>
    
    <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" AutoGenerateColumns="False" 
                DataKeyNames="Id"
                onrowcreated="GridView1_RowCreated" onrowupdating="GridView1_RowUpdating" 
                onrowediting="GridView1_RowEditing" 
                onrowcancelingedit="GridView1_RowCancelingEdit">
                <Columns>
                   
           <%--         <asp:CommandField ShowEditButton="True" />--%>
                   
                    <asp:TemplateField>
                    <ItemTemplate>
                    <%# DataBinder.Eval((Container as GridViewRow).DataItem,"Name") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                    
                    <asp:TextBox runat="server" ID="editTb"></asp:TextBox>
                    </EditItemTemplate>
                    </asp:TemplateField>              
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:test_EmployeeInfo_SKConnectionString %>" 
                SelectCommand="SELECT [Name], [Id] FROM [Category]" >
            </asp:SqlDataSource>
        
        </div>
    
        </form>
    </body>
    </html>
    

     


    Microsoft Online Community Support
    2010年3月31日 2:45
  • 感谢KeFang Chen;

    我测试了你的代码,的确可以通过,也满足我这个问题所要达到的目的。

    但又衍生了新的问题,详细如下:

    我对比了你我的代码之后,感觉到这个问题应该和控件生命周期有关系,所以我试着将原先在Page_Load里面的:
    GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
    移动到了OnInit之中:

     

        protected override void OnInit(EventArgs e)
        {
            GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
            base.OnInit(e);
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.AutoGenerateEditButton = true;
    
            //GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
            GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
            GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
            GridView1.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);
    
            if (!this.IsPostBack)
            {
                this.GridView1.DataSource = GetImagesTab;
                this.GridView1.DataBind();
            }
        }

    我发现,我的代码就没有了之前的问题,通过了测试,并且达到了我的目的。

    所以接着,我修改你的代码,我将

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                DataKeyNames="Id"
                onrowcreated="GridView1_RowCreated" onrowupdating="GridView1_RowUpdating"
                onrowediting="GridView1_RowEditing"
                onrowcancelingedit="GridView1_RowCancelingEdit">

    中的这四个事件全部删除,然后在你的Page_Load中添加这4个事件,修改你的Page_Load如下:

        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.AutoGenerateEditButton = true;
    
            GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
            GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
            GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
            GridView1.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);
    
            if (!this.IsPostBack)
            {
                this.GridView1.DataSource = GetImagesTab;
                this.GridView1.DataBind();
            }
        }
    

     这样运行起来,就又出现了我在顶楼写的那个奇怪的Update问题。

     -------------------------------------------------

    所以,虽然这个问题解决了,但是,却还是有令人困惑的地方,即:


    为什么将
    GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
    放在Oninit里(或控件标签中,即也相当于在Page_Load之前就注册),这样就没问题;
    但将这个RowCreated放在Page_Load之中就会出现问题呢?


     

    2010年4月1日 8:44
  • 感谢KeFang Chen;

    我测试了你的代码,的确可以通过,也满足我这个问题所要达到的目的。

    但又衍生了新的问题,详细如下:

    我对比了你我的代码之后,感觉到这个问题应该和控件生命周期有关系,所以我试着将原先在Page_Load里面的:
    GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
    移动到了OnInit之中:

     

        protected override void OnInit(EventArgs e)
    
        {
    
            GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
    
            base.OnInit(e);
    
        }
    
        
    
        protected void Page_Load(object sender, EventArgs e)
    
        {
    
            GridView1.AutoGenerateEditButton = true;
    
    
    
            //GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
    
            GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
    
            GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
    
            GridView1.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);
    
    
    
            if (!this.IsPostBack)
    
            {
    
                this.GridView1.DataSource = GetImagesTab;
    
                this.GridView1.DataBind();
    
            }
    
        }
    
    

    我发现,我的代码就没有了之前的问题,通过了测试,并且达到了我的目的。

    所以接着,我修改你的代码,我将

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                DataKeyNames="Id"
                onrowcreated="GridView1_RowCreated" onrowupdating="GridView1_RowUpdating"
                onrowediting="GridView1_RowEditing"
                onrowcancelingedit="GridView1_RowCancelingEdit">

    中的这四个事件全部删除,然后在你的Page_Load中添加这4个事件,修改你的Page_Load如下:

        protected void Page_Load(object sender, EventArgs e)
    
        {
    
            GridView1.AutoGenerateEditButton = true;
    
    
    
            GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
    
            GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
    
            GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
    
            GridView1.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);
    
    
    
            if (!this.IsPostBack)
    
            {
    
                this.GridView1.DataSource = GetImagesTab;
    
                this.GridView1.DataBind();
    
            }
    
        }
    
    
    
    

     这样运行起来,就又出现了我在顶楼写的那个奇怪的Update问题。

     -------------------------------------------------

    所以,虽然这个问题解决了,但是,却还是有令人困惑的地方,即:


    为什么将
    GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
    放在Oninit里(或控件标签中,即也相当于在Page_Load之前就注册),这样就没问题;
    但将这个RowCreated放在Page_Load之中就会出现问题呢?


     


    因为rowcreated事件是创造gridview的每行操作 所以在pageload事件中要操作所有控件加载视图 这时候gridview没有创造好行的内容造成问题
    2010年4月1日 10:51
  • 感谢 mldark_马龙;

    我对用户奖牌用户奖牌用户奖牌用户奖牌以下形式的修改进行了多个断点的测试:

        protected override void OnInit(EventArgs e)
    
        {
    
            GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
    
            base.OnInit(e);
    
        }
    
        
    
        protected void Page_Load(object sender, EventArgs e)
    
        {
    
            GridView1.AutoGenerateEditButton = true;
    
    
    
            //GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
    
            GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
    
            GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
    
            GridView1.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);
    
    
    
            if (!this.IsPostBack)
    
            {
    
                this.GridView1.DataSource = GetImagesTab;
    
                this.GridView1.DataBind();
    
            }
    
        }
    

     

    发现:

    1、首次加载页面的时候,执行顺序如下:
    OnInit —— Page_Load —— GridView1_RowCreated

    2、点击"Edit"或"Update"按钮会发的时候,执行顺序如下:
    OnInit —— GridView1_RowCreated —— Page_Load —— GridView1_RowEditing/GridView1_RowUpdating

    所以,猜测了我在楼顶出现问题的原因:

    因为在页面Preload之后,页面已经加载了视图状态,所以可能是MS在设计GridView这个控件的时候,为了Update等的需要,在GridView控件OnInit的时候就让其就执行RowCreate的操作,只有如此,才能保证每一次加载页面的视图状态是最新,也只有视图状态更新及时,才能保证在Page_Load之后执行GridView回发的事件才能正确被触发并执行;

    另外,在GridView里,本次回发操作是执行Edit还是Update事件,是要根据本次加载的视图状态来决定的(我猜测Edit和Update的执行,跟RowIndex的关系相当密切,这一点可以从执行GridView1_RowEditing或GridView1_RowUpdating之前的GridView1_ItemCommand中设置断点能看出来)

    2010年4月2日 2:24