gridview 换页的时候会触发RowCommand事件,导致GridViewRow索引超出范围

解决方案 »

  1.   

    是不能禁止的,你把RowCommand事件暂时屏蔽就可以了。 
     
      

  2.   

    判断下,如果是翻页事件,return
      

  3.   

    但是选择行要用到RowCommand事件
      

  4.   

    会触发,RowCommand事件是GridView内生成事件时就会激发
      

  5.   

    本帖最后由 net_lover 于 2011-07-29 08:58:29 编辑
      

  6.   


    <asp:GridView ID="gvSubCate" runat="server" DataKeyNames="ID" 
            AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" 
            BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" 
            GridLines="Vertical" AllowPaging="True" onpageindexchanging="gvSubCate_PageIndexChanging" 
                                onrowcommand="gvSubCate_RowCommand" onrowdeleting="gvSubCate_RowDeleting" PageSize="5">
            <RowStyle BackColor="#F7F7DE" />
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True">
                    <ItemStyle HorizontalAlign="Center" Width="80px" />
                </asp:BoundField>
                <asp:BoundField DataField="SubCateName" HeaderText="次类名">
                    <ItemStyle HorizontalAlign="Center" Width="200px" />
                </asp:BoundField>
                <asp:BoundField DataField="CateName" HeaderText="主类名">
                    <ItemStyle HorizontalAlign="Center" Width="200px" />
                </asp:BoundField>
                <asp:CommandField HeaderText="编辑" SelectText="编辑" ShowSelectButton="True">
                    <ItemStyle HorizontalAlign="Center" Width="100px" />
                </asp:CommandField>
                <asp:CommandField HeaderText="删除" ShowDeleteButton="True">
                    <ItemStyle HorizontalAlign="Center" Width="100px" />
                </asp:CommandField>
            </Columns>
            <FooterStyle BackColor="#CCCC99" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView> protected void gvSubCate_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
           
            gvSubCate.PageIndex = e.NewPageIndex;
           
            GVBind();//数据绑定        Clear();//清空文本框,设置GridView的EditIndex=-1
        }    protected void gvSubCate_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = gvSubCate.Rows[index];//这里索引会超出范围
            int id = Convert.ToInt32(row.Cells[0].Text);
            BLL.SubCate scBLL = new SubCate();
            Model.SubCate sc = scBLL.GetModel(id);
            for (int i = 0; i < ddCateName.Items.Count; i++)
            {
                if (ddCateName.Items[i].Value == sc.CateID.ToString())
                {
                    ddCateName.SelectedIndex = i;
                }
            }
            txtSubCateName.Text = sc.SubCateName;        btnAdd.Text = " 更 新 ";
        }
      

  7.   

    换页会触发 行绑定事件  不会触发RowCommand吧  你看看代码
      

  8.   

    本帖最后由 net_lover 于 2011-07-29 09:39:55 编辑
      

  9.   


    if (!e.CommandName.Equals("Page")) 进行判断
      

  10.   

    if (!e.CommandName.Equals("Page")) 进行判断 
    是正解。