當在嵌套中繼器的頁腳中查找數量框控件時,收到空引用錯誤。當執行OnItemCommand函數時(不是數據綁定到中繼器時發生此錯誤,這是我之前遇到但已修復的問題)。在嵌套中繼器的頁腳內查找控件? (.NET 2.0,C#)
我是一個新手,所以我不明白所有這一切,我試圖通過它,因爲我去,但我不知道爲什麼FindControl裏面的一個foreach repeateritem(其中msdn says includes the header and footer of a repeater! )在該中繼器的頁腳中找不到控制。這讓我瘋狂。
請幫忙!
更新:我更改了代碼,但仍然存在相同的問題 - 我錯誤地引用了ddl,並且我一直將對象引用設置爲未設置爲對象的實例。
下面的代碼:
.NET:
<asp:Content ID="ProductRepeater" ContentPlaceHolderID="ProductRepeater" Runat="Server">
<asp:Repeater ID="chairRepeater" OnItemCommand="productRepeater_ItemCommand" OnItemDataBound="chairRepeater_ItemDataBound" runat="server">
<ItemTemplate>
...
<asp:Repeater ID="variantRepeater" OnItemDataBound="variantRepeater_ItemDataBound" runat="server">
<ItemTemplate>
<li>
<asp:RadioButton ID="radioBtn" GroupName="collections" runat="server"></asp:RadioButton>
<asp:HiddenField ID="variantId" runat="server" />
<asp:Literal ID="Image1" runat="server" />
<asp:Literal ID="collectionName" runat="server" />
–
<asp:Literal ID="listPrice" runat="server" />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
<asp:DropDownList ID="quantityLister" runat="server" />
</FooterTemplate>
</asp:Repeater>
<asp:ImageButton ID="addToCart" ImageUrl="assets/images/_addtocart.gif" runat="server" />
</div>
</div>
</ItemTemplate>
<SeparatorTemplate> <br />
</SeparatorTemplate>
<FooterTemplate> </FooterTemplate>
</asp:Repeater>
C#:
protected void productRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
BasketHelper basketHelper = new BasketHelper(SiteContext.Current.ShoppingBasketName);
OrderForm orderForm = basketHelper.GetOrderForm();
bool basketUpdated = false;
string catalogName = ConfigurationManager.AppSettings["PatioCatalogName"];
string productId = ((HiddenField)e.Item.FindControl("productId")).Value;
string variantId = "";
Repeater variantRepeater = (Repeater)e.Item.FindControl("variantRepeater");
foreach (RepeaterItem item in variantRepeater.Items)
{
RadioButton radioBtn = item.FindControl("radioBtn") as RadioButton;
if (radioBtn.Checked == true)
{
variantId = ((HiddenField)item.FindControl("variantId")).Value;
}
}
int quantity = 0;
DropDownList quantityLister = (DropDownList)variantRepeater.FindControl("quantityLister");
string ddlvalue = quantityLister.SelectedValue;
int.TryParse(ddlvalue, out quantity);
if (quantity > 0)
{
orderForm.LineItems.Add(new LineItem(catalogName, productId, variantId, quantity));
basketUpdated = true;
}
if (basketUpdated)
{
basketHelper.Basket.Save();
Response.Redirect(
String.Format(
CultureInfo.InvariantCulture,
"~/cart.aspx?{0}={1}",
SiteConstants.ActionQueryStringKey,
SiteConstants.RunPipelineCartAction),
true);
}
}
而這裏的時候我按下購買按鈕我得到的錯誤:
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 116: int quantity = 0;
Line 117: DropDownList quantityLister = (DropDownList)variantRepeater.FindControl("quantityLister");
Line 118: string ddlvalue = quantityLister.SelectedValue;
Line 119: int.TryParse(ddlvalue, out quantity);
Line 120:
Source File: c:\Inetpub\patios\chaircovers.aspx.cs Line: 118
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
ChairCovers.productRepeater_ItemCommand(Object source, RepeaterCommandEventArgs e) in c:\Inetpub\patios\chaircovers.aspx.cs:118
System.Web.UI.WebControls.Repeater.OnItemCommand(RepeaterCommandEventArgs e) +108
System.Web.UI.WebControls.Repeater.OnBubbleEvent(Object sender, EventArgs e) +68
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.RepeaterItem.OnBubbleEvent(Object source, EventArgs e) +123
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.ImageButton.OnCommand(CommandEventArgs e) +111
System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +176
System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
我不是,我只是使用控件的ID(「quanitityLister」);當我執行父級中繼器的功能時,我試圖抓住的控件的ID是否發生變化? (我可能沒有使用正確的術語,我很抱歉。) – 2009-12-16 18:56:34
嘗試我的編輯,看看會發生什麼 – 2009-12-16 21:06:30
我已經意識到數量是variantRepeater中的項目組的數量,而且我沒有查找數量對於variantRepeater中的每個項目,所以我剛剛更新了我的代碼,但我仍然收到相同的錯誤:未將對象引用設置爲對象的實例。讓我發佈我的更新代碼。我似乎無法得到我的頁腳下拉列表的粘貼值! – 2009-12-16 21:45:26