System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Param



Default.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("intestSport"(

(SetPreviousDataHobby((函数的行(

我认为这应该为您指明正确的方向

  • DataGrid的行和列从索引0 开始

  • hobbyGrid.Rows[rowIndex].Cells[5]表示有6列,你认为这是真的吗?

  • 错误消息只是指示您访问的索引超出了数据结构的限制。

  • 您可以通过hobbyGrid.Rows[0].Cells.Count-1;验证row[0]可用的columns数量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循环中的数据行,只添加一行。如果这是你所期望的,就忽略我吧。否则,将下面的线移动到循环内部。

dtHobbyTable.Rows.Add(drHobbyRow);

2( 另一个问题是您正在更新的项目上运行for循环。相反,在hobbyGrid.Rows.Count.上进行循环是件好事

相关内容

最新更新