我想创建一个在线商店网站。如何使用密钥来填充网格视图来自会话
我创建了一个datalist,显示我的产品在我的网站中,我把LinkButton放在我的数据手册中,用于获取用户选择的产品的productID并将其添加到购物车。
<asp:LinkButton ID="LinkButton1" runat="server" Text="Add To Cart" CommandName="addtocart" CommandArgument='<%# Eval("id")%>' >
当用户点击LinkButton的此事件触发:
protected void theDataList_ItemCommand(object source, DataListCommandEventArgs e)
{
//this add the selected product-id to the list<int> and put it in the
// session["addtocart"]
if (e.CommandName == "addtocart")
{
int productid = Convert.ToInt32(e.CommandArgument);
Label6.Text = productid.ToString();
List<int> productsincart = (List<int>)Session["addtocart"];
if (productsincart == null)
{
productsincart = new List<int>();
}
productsincart.Add(productid);
Session["addtocart"] = productsincart;
}
之后,当用户点击该按钮,它的文字是“秀购物车”, 用户将看到购物车。 aspx页面
Response.Redirect("shoppingcart.aspx");
在此页面中我有一个gridview,我想将其绑定到会话[“addtocart”]; 当页面加载gridview显示他们的ID在会话中的选定产品[“购物车”] 但它不起作用,并且发生此错误: System.InvalidCastException:对象必须实现IConvertible。
这是相关代码:
<asp:GridView ID="GridView3" runat="server"
DataSourceID="SqlDataSource1" >
<columns>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="post" SortExpression="post" />
<asp:BoundField DataField="price" HeaderText="salary"
SortExpression="salary" />
<asp:BoundField DataField="color" HeaderText="years" SortExpression="years" />
</columns>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:employeeConnectionString4 %>"
SelectCommand="SELECT * FROM [products] WHERE ([productid] = @productid)">
<SelectParameters>
<asp:SessionParameter DefaultValue="7" Name="cart" SessionField="cart"
Type="Int32" />
</SelectParameters>
我知道这个问题是关系到GridView3和会话[“addtocart”],我觉得GridView3不能从整数列表对象转换值存储在会话[“addtocart”]整数,在我看来,错误来自会话对象转换为整数列表,但我不知道如何解决这个问题,如果任何身体帮助我,我成为非常感谢。
Session [“cart”]中的对象是一个Integer列表,其中包含用户选择他们购买的productsid列表。 它给了我这个错误:
对象必须实现IConvertible。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。
异常详细信息:System.InvalidCastException:对象必须实现IConvertible。
源错误:
在当前web请求的执行过程中生成未处理的异常。关于异常的来源和位置的信息可以使用下面的异常堆栈跟踪来标识。
堆栈跟踪:
[InvalidCastException: Object must implement IConvertible.]
System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +2880621
System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges) +141
System.Web.UI.WebControls.Parameter.GetValue(Object value, Boolean ignoreNullableTypeChanges) +63
System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext context, Control control) +301
System.Web.UI.WebControls.SqlDataSourceView.InitializeParameters(DbCommand command, ParameterCollection parameters, IDictionary exclusionList) +264
System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +472
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +19
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
System.Web.UI.WebControls.GridView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +72
System.Web.UI.Control.EnsureChildControls() +87
System.Web.UI.Control.PreRenderRecursiveInternal() +44
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842
请告诉我们以下内容:1)购物车对象是什么,2)给您错误的代码(特定线路)。通过查看你的代码,看起来问题出在你的SqlDataSource中,而不是网格视图。 –
Session [“cart”]中的对象是一个Integer列表,其中包含用户选择购买的productsid列表。 –