yyyyyy夜cr 2019-08-09 16:10 采纳率: 100%
浏览 2050
已采纳

指定的参数已超出有效值的范围。 参数名: index?

asp.net初学者
出错代码旁边注释掉的是之前的写法。。不会改啊要抓狂了!!!!麻烦各界大神指正!还挺着急的。。悬赏我可以加,回答的时候贴上改正后的代码好不????

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>adminlist</title>
    <link rel="stylesheet" type="text/css" href="css/index.css"/>
    <link rel="stylesheet" type="text/css" href="css/adminList.css" />
</head>
<body>
    <form id="form1" runat="server">
        <!-- 管理员列表 -->
    <div id="admin_list">
        <div class="header radius">
            <h3>管理员列表</h3>
        </div>
        <div class="admin_list_content">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound">
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="id" HeaderText="" ReadOnly="True" />
                    <%--只能读取 不能编辑改变值 这是搜寻的标杆--%>
                    <asp:BoundField DataField="List" HeaderText="管理员列表" />
                    <asp:BoundField DataField="Name" HeaderText="管理员姓名" />
                    <asp:BoundField DataField="Type" HeaderText="管理员类型" />
                    <asp:BoundField DataField="Range" HeaderText="管理员区域" />
                    <asp:CommandField HeaderText="修改" ButtonType="image" EditImageUrl="./images/change.png" ShowEditButton="True" />
                    <asp:CommandField HeaderText="删除" ButtonType="image" EditImageUrl="./images/remove.png" ShowEditButton="True" />
                </Columns>
                <RowStyle ForeColor="Black" />
                <%--字体颜色--%>
                <PagerStyle BackColor="white" ForeColor="white" HorizontalAlign="Left" />
                <HeaderStyle BackColor="White" Font-Bold="True" ForeColor="Black" />
                <%--背景色 题头颜色  --%>
            </asp:GridView>
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:TextBox ID="Num" TextMode="SingleLine" runat="server" Height="30px" Width="30px"></asp:TextBox>
            <asp:TextBox ID="List" TextMode="SingleLine" runat="server" Height="30px" Width="230px"></asp:TextBox>
            <asp:TextBox ID="Name" TextMode="SingleLine" runat="server" Height="30px" Width="320px"></asp:TextBox>
            <asp:TextBox ID="Type" TextMode="SingleLine" runat="server" Height="30px" Width="225px"></asp:TextBox>
            <asp:TextBox ID="Range" TextMode="SingleLine" runat="server" Height="30px" Width="320px"></asp:TextBox>
            <asp:Button ID="AddAdmin" runat="server" OnClick="AddAdmin_Click" Text="添加管理员" Width="100px" Height="30px" />
        </div>
    </div>
    </form>
</body>
</html>




using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Last
{
    public partial class Last : System.Web.UI.Page
    {
        SqlConnection sqlcon;
        SqlCommand sqlcom;
        string strCon = "server=.;database=ado;uid=sa;pwd=123456";
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取一个值,该值显示页上呈现第一次还是正在加载中发出的响应
            if (!IsPostBack)//刷新后展现修改后的内容
            {
                bind();
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;//将文本框输入的内容传到gv代码里面,代码操控着数据库
            bind();
        }

        //删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string sqlstr = "delete from ado.dbo.ziliao where 管理员列表='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";//获取要删除的id
            sqlcon = new SqlConnection(strCon);
            sqlcom = new SqlCommand(sqlstr, sqlcon);//命令删除语句
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();//执行
            sqlcon.Close();
            bind();
        }

        //更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string sqlstr = "update ado.dbo.adminList set List='"+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "',Name='"+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',Type='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',Range='"+ GridView1.Rows[e.RowIndex].Cells[3].Controls[0].ToString().Trim() + "' where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            SqlHelper.ExecuteNonQuery(sqlstr);
            sqlcon.Close();
            GridView1.EditIndex = -1;//退出当前编辑状态(GridView1是从第0行开始编辑),切换到浏览模式
            bind();
        }

        //取消
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;//退出当前编辑状态(GridView1是从第0行开始编辑),切换到浏览模式
            bind();
        }

        //绑定
        public void bind()
        {
            string sqlstr = "select * from ado.dbo.adminList";
            sqlcon = new SqlConnection(strCon);
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet myds = new DataSet();
            sqlcon.Open();
            myda.Fill(myds, "ado.dbo.adminList");
            GridView1.DataSource = myds;//将GridView的数据库绑定到指定数据库
            GridView1.DataBind();
            sqlcon.Close();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#d7d7f1'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    e.Row.Cells[6].Text = " <img src='Images/change.png' style='width: 20;height: 20;'/>" + e.Row.Cells[1].Text;
            //    e.Row.Cells[7].Text = " <img src='Images/remove.png' style='width: 20;height: 20;'/>" + e.Row.Cells[1].Text;
            //}
        }
        SqlConnection con = new SqlConnection("server=.;database=ado;uid=sa;pwd=123456");//创建连接对象
        protected void AddAdmin_Click(object sender, EventArgs e)
        {
            string Sql = "select * from ado.dbo.adminList where List='" + List.Text + "'";
            SqlDataAdapter da = new SqlDataAdapter(Sql, con); //创建适配器
            DataSet ds = new DataSet(); //创建数据集
            da.Fill(ds, "table"); //填充数据集
            if (da.Fill(ds, "table") > 0) //同用户名
            {
                Response.Write("<script>alert('新增失败,存在相同用户名')</script>");//输出信息
            }
            else//不同用户名
            {
                string text5 = Num.Text.Trim();
                string text1 = List.Text.Trim();
                string text2 = Name.Text.Trim();
                string text3 = Type.Text.Trim();
                string text4 = Range.Text.Trim();
                string str = "insert into ado.dbo.adminList (id,List,Name,Type,Range) VALUES('" + text5 + "','" + text1 + "','" + text2 + "','" + text3 + "','" + text4 + "')";
                if (SqlHelper.ExecuteNonQuery(str) > 0)//执行成功
                {
                    Response.Redirect(Request.Url.ToString());
                }
                else//执行失败
                {
                    Response.Write("<script>alert('增加失败,请检查系统内部')</script>");//输出信息
                }
            }
        }
    }
}

图片说明
图片说明
图片说明

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-08-09 22:17
    关注

    调试下,cells有没有1,controls有没有0。就这两个地方可能出错

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 halcon根据玻璃面板纹路取区域
  • ¥15 HFSS设计小型化180度耦合器
  • ¥15 使用CInternetSession,CHttpFile读取网页文件时有些电脑上会卡住怎么办?
  • ¥15 水下机器人的半物理仿真研究
  • ¥15 微服务假死,一段时间后自动恢复,如何排查处理
  • ¥50 webrtc-streamer TCP rtsp
  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题