quot;Specified argument was out of the range of valid valuesquot;(“指定的参数超出了有效值的范围)
问题描述
我正在使用文本框在 ASP.Net GridView 控件中添加动态行.但是当我点击我的添加新行按钮时出现以下错误.
<块引用>指定的参数超出了有效值的范围.参数名称:index
可能是什么错误?
我的.aspx文件中的代码
<asp:gridview id="Gridview1" runat="server" autogeneratecolumns="false" gridlines="None"宽度="100%" 高度="100%"><列><asp:TemplateField><项目模板><div class="divRow"><div class="divColumn"><div><asp:Label ID="lbl1" runat="server" Text="Type Name" CssClass="formLable"></asp:Label><div><asp:TextBox ID="txt_type" Width="200" runat="server" CssClass="txtbox"></asp:TextBox>
<跨度><asp:RequiredFieldValidator ID="TYPE_NAME_VAL" runat="server" ControlToValidate="txt_type"ErrorMessage="需要类型."Display="Dynamic" CssClass="error"></asp:RequiredFieldValidator></span>
<div class="divColumn"><div><asp:Label ID="lbl2" runat="server" Text="Total" CssClass="formLable"></asp:Label>
<div><asp:TextBox ID="txt_total" Width="200" runat="server" CssClass="txtbox"></asp:TextBox>
<div class="divRow"><div class="divColumn"><div><asp:Label ID="lbl3" runat="server" Text="Max" CssClass="formLable"></asp:Label>
<div><asp:TextBox ID="txt_max" Width="200" runat="server" CssClass="txtbox"></asp:TextBox>
<div class="divColumn"><div><asp:Label ID="lbl4" runat="server" Text="Min" CssClass="formLable"></asp:Label>
<div><asp:TextBox ID="txt_min" Width="200" runat="server" CssClass="txtbox"></asp:TextBox>
<div class="divColumn"><div><asp:Label ID="lbl5" runat="server" Text="Rate" CssClass="formLable"></asp:Label>
<div><asp:TextBox ID="txt_rate" Width="200" runat="server" CssClass="txtbox"></asp:TextBox>
<asp:Button ID="ButtonAdd" runat="server" Text="添加新行" OnClick="ButtonAdd_Click"CausesValidation="False"/></ItemTemplate></asp:TemplateField></列></asp:gridview>
ButtonAdd_Click()的C#代码
if (ViewState["CurrentTable"] != null){DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];DataRow drCurrentRow = null;如果(dtCurrentTable.Rows.Count > 0){for (int i = 1; i <= dtCurrentTable.Rows.Count; i++){//提取文本框值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;行索引++;}dtCurrentTable.Rows.Add(drCurrentRow);ViewState["CurrentTable"] = dtCurrentTable;Gridview1.DataSource = dtCurrentTable;Gridview1.DataBind();}}别的{Response.Write("ViewState 为空");}
您似乎试图从包含 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");
I'm adding dynamic Rows in ASP.Net GridView Control with TextBoxes. But I'm getting the error below when I click on my Add New Row button.
Specified argument was out of the range of valid values. Parameter name: index
What could be the error ?
Th code in my .aspx file
<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>
C# code of ButtonAdd_Click()
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");
}
It seems that you are trying to get 5 items out of a collection with 5 items. Looking at your code, it seems you're starting at the second value in your collection at position 1. Collections are zero-based, so you should start with the item at index 0. Try this:
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");
这篇关于“指定的参数超出了有效值的范围"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!