ASP.net报错提示:指定的参数已超出有效值的范围。参数名: index,添加OnRowDataBound="gvEditMember_RowD这个事件就报错
前台代码 管理员代号在第一列 管理员名称第二列 删除第三列 管理员身份第四列 更改身份第5列
- C# code
<asp:GridView ID="gvEditMember" runat="server" AllowPaging="True" AutoGenerateColumns="False" PageSize="5" DataKeyNames ="ID" Width="100%" HorizontalAlign="Center" CssClass="txt" HeaderStyle-CssClass="summary-title" OnPageIndexChanging="gvEditMember_PageIndexChanging" OnRowCancelingEdit="gvEditMember_RowCancelingEdit" OnRowDeleting="gvEditMember_RowDeleting" OnRowEditing="gvEditMember_RowEditing" OnRowUpdating="gvEditMember_RowUpdating" OnRowDataBound="gvEditMember_RowDataBound" Height="225px" CellPadding="4" EnableModelValidation="True" ForeColor="#333333" GridLines="None"> <EditRowStyle BackColor="#2461BF" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <HeaderStyle Font-Bold="True" CssClass="summary-title" BackColor="#507CD1" ForeColor="White"></HeaderStyle> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:BoundField DataField="admin_id" HeaderText="管理员代号" ReadOnly="True" > <ItemStyle HorizontalAlign="Left" Width="80px" /> <HeaderStyle HorizontalAlign="Left" /> </asp:BoundField> <asp:BoundField DataField="admin" HeaderText="管理员名称" ReadOnly="True"> <ItemStyle HorizontalAlign="Left" /> <HeaderStyle HorizontalAlign="Left" /> </asp:BoundField> <asp:CommandField ShowDeleteButton="True" <ItemStyle HorizontalAlign="Left" Width="30px" /> </asp:CommandField> <asp:BoundField DataField="class" HeaderText="管理员身份" > <HeaderStyle HorizontalAlign="Left" /> <ItemStyle HorizontalAlign="Left" /> </asp:BoundField> <asp:CommandField ShowEditButton="True" EditText="更改身份" /> </Columns> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#EFF3FB" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> </asp:GridView>
后台代码 最后那里 确定删除 ((LinkButton)(e.Row.Cells[2].Controls[0])).Attributes.Add("onclick", "return confirm('确定要删除吗?')"); 这句出错指定的参数已超出有效值的范围。参数名: index
- C# code
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.SqlClient;public partial class Manage_MemberEdit : System.Web.UI.Page{ CommonClass CC = new CommonClass(); protected void Page_Load(object sender, EventArgs e) { if (Session["grade"] != null && Session["grade"].ToString() != " ") { } else { Response.Write(CC.MessageBox("您还没登录呢!", "Login.aspx")); } if (!IsPostBack) { bind(); } } public void bind() { this.gvEditMember.DataSource = CC.GetDataSet("select * from admin where grade>0 order by admin_id", "admin"); this.gvEditMember.DataKeyNames = new string[] { "admin_id" }; this.gvEditMember.DataBind(); } protected void gvEditMember_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvEditMember.PageIndex = e.NewPageIndex; bind(); } protected void gvEditMember_RowDeleting(object sender, GridViewDeleteEventArgs e) { int IntAdminID = Convert.ToInt32(gvEditMember.DataKeys[e.RowIndex].Value.ToString()); CC.ExecSQL("Delete from admin where admin_id='" + IntAdminID + "'"); bind(); } protected void gvEditMember_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gvEditMember.EditIndex = -1; bind(); } protected void gvEditMember_RowEditing(object sender, GridViewEditEventArgs e) { gvEditMember.EditIndex = e.NewEditIndex; bind(); } protected void gvEditMember_RowUpdating(object sender, GridViewUpdateEventArgs e) { int IntAdminID = Convert.ToInt32(gvEditMember.DataKeys[e.RowIndex].Value.ToString()); string strclass = ((TextBox)(gvEditMember.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString(); if (strclass == "高级管理员") { SqlConnection myConn = CC.GetConnection(); SqlCommand myCmd = new SqlCommand("select count(*) from admin where grade='1'", myConn); myConn.Open(); int i = (int)myCmd.ExecuteScalar(); myCmd.Dispose(); myConn.Close(); if (i == 2) { Response.Write(CC.MessageBox("高级管理员已有两个,请删除后再更改!", "MemberEdit.aspx")); } else { CC.ExecSQL("Update admin set grade='1',class='高级管理员' where admin_id='" + IntAdminID + "'"); Response.Write(CC.MessageBox("添加成功!", "MemberAdd.aspx")); } } else if (strclass == "普通管理员") { CC.ExecSQL("Update admin set grade='2',class='普通管理员' where admin_id='" + IntAdminID + "'"); } else { Response.Write(CC.MessageBox("请填写“高级管理员”或者“普通管理员”!", "MemberEdit.aspx")); } gvEditMember.EditIndex = -1; bind(); } protected void gvEditMember_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { ((LinkButton)(e.Row.Cells[2].Controls[0])).Attributes.Add("onclick", "return confirm('确定要删除吗?')"); } } } 列的位置 肯定是对的 确定删除的功能能实现 把 确定删除的代码 删掉后 更改身份的功能也能运作 为什么 一添加确定删除后 就 一点击 更改身份 就 报错呢 求高手们 解决 想了 很久了 列的位置也换来换去试过啦 都不行 谢谢大家啦
[解决办法]
太长了,自己打个断点单步调试一下。。
e.Row.Cells[2].Controls[0] 看这个得到东西没?