我有一个产品页面,其中DataList
使用了CheckBox
字段。我想将所有选定的已检查产品添加到添加到购物车,这将在另一个名为CartViewa.aspx
的页面上创建
我使用C#中的以下代码完成了此操作,我收到此错误Object reference not set to an instance of an object.
HTML
<asp:DataList ID="DataList1" runat="server"
RepeatColumns="3" RepeatDirection="Horizontal">
<ItemTemplate>
<div class="prod_box">
<div class="top_prod_box"></div>
<div class="center_prod_box">
<asp:LinkButton Text='<%#Eval("PName")%>' ID="lnkpro" runat="server" CssClass="linkbutton" CommandArgument='<%# Eval("PName") %>'></asp:LinkButton>
<%-- <div class="product_title" runat="server" id="ProductName"><a href="#"></a><%# Eval("PName") %></div>--%>
<div class="product_img"><span>Qty. <%# Eval("Qty") %> </span></div>
<div class="prod_price"> <span class="price">Rs. <%# Eval("Cost") %> </span></div>
<div class="prod_price"> <span class="price"> Rs. <%# Eval("Extravalue") %> </span></div>
</div>
<div class="bottom_prod_box"></div>
<div class="prod_details_tab">
<%--<a href="#" title="header=[Add to cart] body=[ ] fade=[on]"><img src="images/cart.gif" alt="" title="" border="0" class="left_bt" /></a>--%>
<%-- <a href="#" title="header=[Specials] body=[ ] fade=[on]"><img src="images/favs.gif" alt="" title="" border="0" class="left_bt" /></a>
<a href="#" title="header=[Gifts] body=[ ] fade=[on]"><img src="images/favorites.gif" alt="" title="" border="0" class="left_bt" /></a> --%>
<%--<asp:LinkButton ID="LinkButton1" OnCommand="DetailsCommand" CommandName='<%# Eval("PID") %>' CssClass="prod_details" Text="Details" runat="server"></asp:LinkButton>--%>
<%--<asp:LinkButton ID="AddToCart" Text="AddToCart" CssClass="prod_details" OnCommand="AddToCart" CommandName='<%# Eval("PID") %>' runat ="server"></asp:LinkButton>
</div> --%>
<asp:CheckBox ID="CheckBox1" runat="server" CssClass="checkbox" CommandArgument='<%# Eval("Pid") %>' />
<asp:LinkButton ID="AddToCart" runat="server" CommandName='<%# Eval("Pid") %>' CssClass="cart" OnCommand="AddToCart"></asp:LinkButton>
<asp:LinkButton ID="LinkButton1" OnCommand="DetailsCommand" CommandName='<%# Eval("Pid") %>' CssClass="prod_details" Text="Details" runat="server"></asp:LinkButton>
</div>
</div>
</ItemTemplate>
</asp:DataList>
<asp:Button ID="Button1" Text="Button" onclick="Button1_Click" runat="server"
CommandArgument="'<%# Eval('Pid') %>'" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT Pid, [PName], [Cost] FROM [Pdetails]"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>">
</asp:SqlDataSource>
Products.aspx.cs上的C#页面代码是:
protected void Button1_Click(object sender, EventArgs e)
{
Button button = (sender as Button);
//Get the command argument
string commandArgument = button.CommandArgument;
//Get the Repeater Item reference
RepeaterItem item1 = button.NamingContainer as RepeaterItem;
//-----------I am Getting Error Here-----------
//----------------------------------------------------------------------------------
//Get the repeater item index
int index = item1.ItemIndex; //---I m getting error on this line---
//-----------------------------------------------------------------------------------
DataTable dt = (DataTable)Session["TaskTable"];
foreach (DataListItem item in DataList1.Items)
{
CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
LinkButton lnkbtnActivate = (LinkButton)item.FindControl("lnkpro");
if (chk.Checked == true)
{
DataRow[] DataRows;
string Result = lnkbtnActivate.Text;
DataRows = dt.Select(Result);
foreach (DataRow dr in DataRows)
{
try
{
//dr.Delete();
DataTable dt2 = ds.GetData("select PName,Cost from Pdetails where Pid='" + index + "'");
DataTable taskTable = (DataTable)Session["TaskTable"];
DataRow findrow = taskTable.Rows.Find(dt2.Rows[0]["PName"]);
if (findrow == null)
{
DataRow dr2 = taskTable.NewRow();
dr2["Pid"] = index;
dr2["PName"] = dt2.Rows[0]["PName"];
dr2["Cost"] = dt2.Rows[0]["Cost"];
dr2["Qty"] = 1;
dr2["TCost"] = Convert.ToSingle(dr2["Cost"]) * Convert.ToSingle(dr2["Qty"]);
taskTable.Rows.Add(dr2);
}
Session["TaskTable"] = taskTable;
Response.Redirect("CartView.aspx");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
}
答案 0 :(得分:1)
item1
可能是空的:
RepeaterItem item1 = button.NamingContainer as RepeaterItem;
类型为button.NamingContainer
的{{1}}?如果不是,使用RepeaterItem
进行投射将返回null。您需要找出as
的类型,然后适当地进行投射。找出它的类型的简单方法是在我引用的行上放置断点并将鼠标悬停在button.NamingContainer
上。否则,在继续之前检查NamingContainer
是否为空。
答案 1 :(得分:0)
此行永远不会在当前设置中返回RepeaterItem
:
RepeaterItem item1 = button.NamingContainer as RepeaterItem;
按钮的NamingContainer
是页面本身。
答案 2 :(得分:0)
问题在于按钮asp:Button ID="Button1"
。
</asp:DataList>
<asp:Button ID="Button1" Text="Button" onclick="Button1_Click" runat="server"
CommandArgument="'<%# Eval('Pid') %>'" />
它位于DataList
之外,它应该在其中。
应该是它的合适位置。
<asp:LinkButton ID="LinkButton1" OnCommand="DetailsCommand"
CommandName='<%# Eval("Pid") %>' CssClass="prod_details" Text="Details"
runat="server"></asp:LinkButton>
<asp:Button ID="Button1" Text="Button" onclick="Button1_Click" runat="server"
CommandArgument="'<%# Eval('Pid') %>'" />
</div>
</div>
</ItemTemplate>
</asp:DataList>