[英]System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'
[英]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 行:Session["Cat"] = e.CommandArgument.ToString();
第 154 行:
第 155 行:Session["Cat_ID"] = DataList3.DataKeys[e.Item.ItemIndex].ToString();//錯誤
第 156 行:Response.Redirect("Disp_Category.aspx?id=C"); 第 157 行:}
請問這個代碼有什么問題
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");
}
在aspx文件上我有這個
<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>
你看起來不太對勁。 我建議在CommandArgument
設置 id (c_id) 而不是名稱 (c_name)。 那是你的鑰匙。 在ItemCommand
您可以通過 id 獲取整個記錄。 而且:
Response.Redirect("Disp_Category.aspx?id=C");
變成
Response.Redirect("~/Disp_Category.aspx?id=C"); //id=C? sure?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.