簡體   English   中英

System.ArgumentOutOfRangeException: '指定的參數超出了有效值的范圍。 參數名稱:asp.net中的index'

[英]System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: index' in asp.net

默認.aspx

        <Columns>
            <asp:TemplateField HeaderText="Type">
                <ItemTemplate>
                    <asp:DropDownList ID="typeHobby" runat="server">
                        <asp:ListItem style="display:none">--Select--</asp:ListItem>
                        <asp:ListItem Value="Sports">Sports</asp:ListItem>
                        <asp:ListItem Value="FineArt">Fine Arts</asp:ListItem>
                        <asp:ListItem Value="Other">Other</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                     <asp:TextBox  ID="nameSport" style="margin:2px" CssClass="cap" pattern="[A-Za-z]{2,15}" runat="server" ></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Details of participation">
                <ItemTemplate>
                     <asp:TextBox ID="detailSport" style="margin:2px" CssClass="cap" pattern="[A-Za-z]{2,15}" runat="server"></asp:TextBox> 
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Distinction achieved">
                <ItemTemplate>
                     <asp:TextBox ID="distSport" style="margin:2px" CssClass="cap" pattern="[A-Za-z]{2,15}" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Whether still intrested">
                <ItemTemplate>
                    <asp:DropDownList ID="intrestSport" runat="server">
                        <asp:ListItem style="display:none">--Select--</asp:ListItem>
                        <asp:ListItem Value="yes">Yes</asp:ListItem>
                        <asp:ListItem Value="no">No</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
                <FooterStyle HorizontalAlign="Right" />
                    <FooterTemplate>
                        <asp:Button ID="addHobby" runat="server" Text="Add" OnClick="ButtonAdd_Click_Hobby" CausesValidation="false" />
                    </FooterTemplate>
            </asp:TemplateField>
        </Columns>

    </asp:GridView>

默認.aspx.cs

protected void ButtonAdd_Click_Hobby(object sender, EventArgs e)
        {
            addHobby();
        }

        protected void addHobby()
        {
         //   MessageBox.Show("add hobby");
            try
            {
                int rowIndex = 0;
                if (ViewState["HoobyTable"] != null)
                {
                    MessageBox.Show("if true");
                    DataTable dtHobbyTable = (DataTable)ViewState["HoobyTable"];
                    DataRow drHobbyRow = null;
                    if (dtHobbyTable.Rows.Count > 0)
                    {
                        MessageBox.Show(dtHobbyTable.Rows.Count.ToString());
                        for (int i = 1; i <= dtHobbyTable.Rows.Count; i++)
                        {
                            //extract the TextBox values
                            MessageBox.Show("loop");
                            DropDownList txh1 = (DropDownList)hobbyGrid.Rows[rowIndex].Cells[1].FindControl("typeHobby");
                            TextBox txh2 = (TextBox)hobbyGrid.Rows[rowIndex].Cells[2].FindControl("nameSport");
                            TextBox txh3 = (TextBox)hobbyGrid.Rows[rowIndex].Cells[3].FindControl("detailSport");
                            TextBox txh4 = (TextBox)hobbyGrid.Rows[rowIndex].Cells[4].FindControl("distSport");
                            DropDownList txh5 = (DropDownList)hobbyGrid.Rows[rowIndex].Cells[5].FindControl("intrestSport");
                            MessageBox.Show("Hello" + txh1.Text);
                            drHobbyRow = dtHobbyTable.NewRow();
                            drHobbyRow["Slno"] = i + 1;

                            dtHobbyTable.Rows[i - 1]["hoType"] = txh1.Text;
                            dtHobbyTable.Rows[i - 1]["Name"] = txh2.Text;
                            dtHobbyTable.Rows[i - 1]["Detail"] = txh3.Text;
                            dtHobbyTable.Rows[i - 1]["Distinction"] = txh4.Text;
                            dtHobbyTable.Rows[i - 1]["Interest"] ="interest";

                            rowIndex++;
                        }
                        dtHobbyTable.Rows.Add(drHobbyRow);
                        ViewState["HoobyTable"] = dtHobbyTable;

                        hobbyGrid.DataSource = dtHobbyTable;
                        hobbyGrid.DataBind();
                    }
                }
                else
                {
                    Response.Write("ViewState is null");
                }
            }
            catch (Exception e)
            {
                // MessageBox.Show(e.ToString());
            }
            //Set Previous Data on Postbacks
          SetPreviousDataHobby();

        }
        private void SetPreviousDataHobby()
        {
            int rowIndex = 0;
            if (ViewState["HoobyTable"] != null)
            {
                DataTable dt = (DataTable)ViewState["HoobyTable"];
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {

                        DropDownList txh1 = (DropDownList)hobbyGrid.Rows[rowIndex].Cells[1].FindControl("typeHobby");
                        TextBox txh2 = (TextBox)hobbyGrid.Rows[rowIndex].Cells[2].FindControl("nameSport");
                        TextBox txh3 = (TextBox)hobbyGrid.Rows[rowIndex].Cells[3].FindControl("detailSport");
                        TextBox txh4 = (TextBox)hobbyGrid.Rows[rowIndex].Cells[4].FindControl("distSport");
                        DropDownList txh5 = (DropDownList)hobbyGrid.Rows[rowIndex].Cells[5].FindControl("intrestSport");
                        MessageBox.Show("type hobby" + dt.Rows[i]["hoType"].ToString());
                        txh1.Text = dt.Rows[i]["hoType"].ToString();
                        txh2.Text = dt.Rows[i]["Name"].ToString();
                        txh3.Text = dt.Rows[i]["Detail"].ToString();
                        txh4.Text = dt.Rows[i]["Distinction"].ToString();
                        txh5.Text = dt.Rows[i]["Interest"].ToString();


                        rowIndex++;
                    }
                }
            }
        }

System.ArgumentOutOfRangeException: '指定的參數超出了有效值的范圍。 參數名稱:index' -> 在行中出現類似上面的錯誤: DropDownList txh5 = (DropDownList)hobbyGrid.Rows[rowIndex].Cells[5].FindControl("intrestSport");

(來自 SetPreviousDataHobby() 函數的行)

我相信這應該為您指明正確的方向

  • DataGrid索引0 開始

  • hobbyGrid.Rows[rowIndex].Cells[5]表示有6列,你認為這是真的嗎?

  • 該錯誤消息僅表明您正在訪問超出數據結構限制的索引。

  • 您可以驗證有多少columns在可用row[0]通過hobbyGrid.Rows[0].Cells.Count-1; Cells[5]不應超過這個

    或者

  • 您還可以檢查表中的列數,例如int cols= dtHobbyTable.Columns.Count

錯誤是因為,您正在訪問從 1 到 5 的單元格。因此在 Cells[5] 處,它拋出超出范圍的異常。 請修改您的代碼以從 0 開始:

DropDownList txh1 = (DropDownList)hobbyGrid.Rows[rowIndex].Cells[0].FindControl("typeHobby");
TextBox txh2 = (TextBox)hobbyGrid.Rows[rowIndex].Cells[1].FindControl("nameSport");
TextBox txh3 = (TextBox)hobbyGrid.Rows[rowIndex].Cells[2].FindControl("detailSport");
TextBox txh4 = (TextBox)hobbyGrid.Rows[rowIndex].Cells[3].FindControl("distSport");
DropDownList txh5 = (DropDownList)hobbyGrid.Rows[rowIndex].Cells[4].FindControl("intrestSport");

附帶但相關的說明: 1) 修復此錯誤后,您可能會遇到另一個問題:在 forloop 之外添加數據行。 因此,您基本上是在 for 循環中覆蓋數據行並僅添加一行。 如果這是您的預期,請忽略我。 否則,將下面的行移到 for 循環內。

dtHobbyTable.Rows.Add(drHobbyRow);

2) 另一個問題是您正在更新的項目上運行 for 循環。 相反,在 hobbyGrid.Rows.Count 上有 for 循環是件好事。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM