System.ArgumentOutOfRangeException:索引超出范围。必须是非负并小于集合的大小。参数名称:索引 [英] System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
问题描述
我得到这个错误
System.ArgumentOutOfRangeException:索引超出范围。必须是非负并小于集合的大小。
参数名称:索引
源错误:
153线:会话[猫] = e.CommandArgument.ToString();结果
154线:结果
155线:会话[CAT_ID] =
DataList3.DataKeys [e.Item.ItemIndex]的ToString(); //错误结果
156线:Response.Redirect的(?Disp_Category.aspx ID = C);
157线:}
块引用>请一些一什么是错的这个code
保护无效DataList3_ItemCommand(对象源,DataListCommandEventArgs E)
{
会话[L_Id] = ddlLanguage.SelectedValue;
会话[猫] = e.CommandArgument.ToString(); 会话[CAT_ID] = DataList3.DataKeys [e.Item.ItemIndex]的ToString();
的Response.Redirect(Disp_Category.aspx ID = C?);
}在aspx文件我有这个
< ASP:DataList控件ID =DataList3=服务器的DataKeyNames =C_ID的DataSourceID =SqlDataSource3OnItemCommand =DataList3_ItemCommand>
<&ItemTemplate中GT;
<表格的cellpadding =0CELLSPACING =0边界= 1 WIDTH = 150>
&所述; TR>
&所述; TD>
< ASP:LinkButton的ID =LinkButton1=服务器CommandArgument ='<%#的eval(C_Name)%>'
文字='<%#的eval(C_Name)%>'的CausesValidation =FALSE>< / ASP:LinkButton的>< / TD>
< / TR>
< /表>
< / ItemTemplate中>
< / ASP:DataList控件>< ASP:SqlDataSource的ID =SqlDataSource3=服务器的ConnectionString =数据源= \\ SQLEX $ P $干燥综合征; AttachDbFilename = D:\\新建项目\\符号\\ App_Data文件\\音乐。中纤板;集成安全=真;用户实例=真
的ProviderName =System.Data.SqlClient的的SelectCommand =SELECT * FROM [类别] ORDER BY [C_Name]>
< / ASP:SqlDataSource的>
解决方案您是不正确的。
我建议设置CommandArgument
的ID(C_ID),而不是名称(c_name)。
这是您的钥匙。在ItemCommand
可以通过id来获取全部记录。
此外:的Response.Redirect(?Disp_Category.aspx ID = C);
变为
的Response.Redirect(?〜/ Disp_Category.aspx ID = C); // ID = C?当然?
I am Getting this error System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Source Error:
Line 153: Session["Cat"] = e.CommandArgument.ToString();
Line 154:
Line 155: Session["Cat_ID"] = DataList3.DataKeys[e.Item.ItemIndex].ToString();//error
Line 156: Response.Redirect("Disp_Category.aspx?id=C"); Line 157: }Please some one what is wrong with this code
protected void DataList3_ItemCommand(object source, DataListCommandEventArgs e) { Session["L_Id"] = ddlLanguage.SelectedValue; Session["Cat"] = e.CommandArgument.ToString(); Session["Cat_ID"] = DataList3.DataKeys[e.Item.ItemIndex].ToString(); Response.Redirect("Disp_Category.aspx?id=C"); }
on aspx file i have this
<asp:DataList ID="DataList3" runat="server" DataKeyNames="C_Id" DataSourceID="SqlDataSource3" OnItemCommand="DataList3_ItemCommand"> <ItemTemplate> <table cellpadding="0" cellspacing="0" border=1 width=150> <tr> <td> <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("C_Name") %>' Text='<%# Eval("C_Name") %>' CausesValidation="False"></asp:LinkButton></td> </tr> </table> </ItemTemplate> </asp:DataList><asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\New Projects\Sym\App_Data\Music.mdf;Integrated Security=True;User Instance=True" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [Category] ORDER BY [C_Name]"> </asp:SqlDataSource>
解决方案You are not right. I suggest to set the id (c_id) and not the name (c_name) in
CommandArgument
. That's your key. InItemCommand
you can get entire record by id. Moreover:Response.Redirect("Disp_Category.aspx?id=C");
becomes
Response.Redirect("~/Disp_Category.aspx?id=C"); //id=C? sure?
这篇关于System.ArgumentOutOfRangeException:索引超出范围。必须是非负并小于集合的大小。参数名称:索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!