当前位置:编程学习 > C#/ASP.NET >>

C# nhibernate gridview 点击编辑后,再点击更新,更新不了

using System;
using Utrl.Entities;
using Business.Service;
using System.Web.UI.WebControls;
using Utrl.DTO;
public partial class Account_Modules_UserMgr_Default : System.Web.UI.Page
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Account_Modules_UserMgr_Default));
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetBind();
        }
        panelErrorUserNum.Visible = false;

    }
    private void SetBind()
    {
        this.GridView1.DataSource = UserService.GetUserInfo();
        this.GridView1.DataBind();
    }

 
   protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        object autoID = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
        Jhygl_User user = new Jhygl_User();
        if (autoID != null)
        {
            user.user_id = autoID.ToString();
            UserService.DeleteUser(user.user_id);
            SetBind();
        }

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

   protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {

       string o_user_id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
       int i = e.RowIndex + 1;
       GridViewRow gvr = this.GridView1.Rows[e.RowIndex];
       string user_num = ((TextBox)(gvr.Cells[1].Controls[0])).Text.Trim();
       string user_name = ((TextBox)(gvr.Cells[2].Controls[0])).Text.Trim();


       Jhygl_UserDTO userDTO = new Jhygl_UserDTO();
       userDTO.user_num = user_num;
       userDTO.user_name = user_num;
       if (o_user_id == null)
       {

       }
       else
       {
           userDTO.user_id = o_user_id.ToString();
           UserService.UpdateUser(userDTO);
       }
       this.GridView1.EditIndex= -1;
       SetBind();

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


   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       //如果是绑定数据行 
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
           {
               ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('确认删除?')");
           }
       }

   }

}


  <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#DEDFDE"
            Width="100%" BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Names="Arial"
            Font-Size="9pt" ForeColor="Black" GridLines="Vertical" OnRowDeleting="GridView1_RowDeleting"
            OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="False" 
            DataKeyNames="user_id" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound">
            <PagerSettings Mode="NextPreviousFirstLast" />
            <FooterStyle BackColor="#990000" Font-Bold="true" ForeColor="White" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <Columns>
                <asp:BoundField DataField="user_id" HeaderText="user_id"
                    Visible="false" ReadOnly="true " />
                <asp:BoundField DataField="user_num" HeaderText="员工号"  />
                <asp:BoundField DataField="user_name" HeaderText="姓名"  />
                <asp:BoundField DataField="user_disabled" HeaderText="是否禁用" 
                    ReadOnly="true" />
                <asp:CommandField ShowEditButton="True" CausesValidation="false" />
                <asp:ButtonField CommandName="Delete" Text="删除" />
            </Columns>
        </asp:GridView>


点击编辑后出现,更新和取消, 点击更新后,进入更新的函数GridView1_RowUpdating,但是执行这一句this.GridView1.Rows[e.RowIndex];的时候就出错了, 说必须为非负值并小于集合大小,到底是什么地方出错了呢,小妹我找了很久都没有找到, 麻烦大侠们帮一下











C# NHibernate GridView ASP
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,