指定的参数超出了有效值范围
本文关键字:有效值 范围 参数 | 更新日期: 2023-09-27 18:17:46
我在ASP中添加动态行。Net GridView控件与文本框。但是当我点击我的添加新行按钮时,我得到了下面的错误。
指定的参数超出有效值范围。参数名称:index
错误是什么?
我的中的代码。aspx文件
<div id="Div1" class="divTable">
<asp:gridview id="Gridview1" runat="server" autogeneratecolumns="false" gridlines="None"
width="100%" height="100%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="divRow">
<div class="divColumn">
<div>
<asp:Label ID="lbl1" runat="server" Text="Type Name" CssClass="formLable"></asp:Label>
</div>
<div>
<asp:TextBox ID="txt_type" Width="200" runat="server" CssClass="txtbox"></asp:TextBox>
</div>
<span>
<asp:RequiredFieldValidator ID="TYPE_NAME_VAL" runat="server" ControlToValidate="txt_type"
ErrorMessage="Type is required." Display="Dynamic" CssClass="error"></asp:RequiredFieldValidator>
</span>
</div>
<div class="divColumn">
<div>
<asp:Label ID="lbl2" runat="server" Text="Total" CssClass="formLable"></asp:Label>
</div>
<div>
<asp:TextBox ID="txt_total" Width="200" runat="server" CssClass="txtbox"></asp:TextBox>
</div>
</div>
</div>
<div class="divRow">
<div class="divColumn">
<div>
<asp:Label ID="lbl3" runat="server" Text="Max" CssClass="formLable"></asp:Label>
</div>
<div>
<asp:TextBox ID="txt_max" Width="200" runat="server" CssClass="txtbox"></asp:TextBox>
</div>
</div>
<div class="divColumn">
<div>
<asp:Label ID="lbl4" runat="server" Text="Min" CssClass="formLable"></asp:Label>
</div>
<div>
<asp:TextBox ID="txt_min" Width="200" runat="server" CssClass="txtbox"></asp:TextBox>
</div>
</div>
<div class="divColumn">
<div>
<asp:Label ID="lbl5" runat="server" Text="Rate" CssClass="formLable"></asp:Label>
</div>
<div>
<asp:TextBox ID="txt_rate" Width="200" runat="server" CssClass="txtbox"></asp:TextBox>
</div>
</div>
</div>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click"
CausesValidation="False" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
</div>
ButtonAdd_Click()的c#代码
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt_type");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txt_total");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txt_max");
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("txt_min");
TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txt_rate");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;
dtCurrentTable.Rows[i - 1]["Column5"] = box5.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
似乎您正在尝试从具有5个项目的集合中获得5个项目。查看您的代码,似乎您是从位置1的集合中的第二个值开始的。集合是从零开始的,因此应该从索引0处的项开始。试试这个:
TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[0].FindControl("txt_type");
TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("txt_total");
TextBox box3 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("txt_max");
TextBox box4 = (TextBox)Gridview1.Rows[i].Cells[3].FindControl("txt_min");
TextBox box5 = (TextBox)Gridview1.Rows[i].Cells[4].FindControl("txt_rate");
试试这个
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("txt_type");
TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("txt_total");
TextBox box3 = (TextBox)Gridview1.Rows[i].Cells[3].FindControl("txt_max");
TextBox box4 = (TextBox)Gridview1.Rows[i].Cells[4].FindControl("txt_min");
TextBox box5 = (TextBox)Gridview1.Rows[i].Cells[5].FindControl("txt_rate");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;
dtCurrentTable.Rows[i - 1]["Column5"] = box5.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
我也得到了同样的问题,因为我试图在非基于索引中使用值0,I。E从1开始,不从0