这困扰着我,而且我已经阅读了无数的解决方案,这些解决方案似乎都有所不同。我有一个转发器,每个项目有2个下降和一些项目。这些都具有0-8的潜在值,用户可以选择,我想在用户点击页面底部的按钮时在回发上收集数据。
<asp:Repeater ID="rptPricing" runat="server" OnItemDataBound="rptPricing_OnItemDataBound">
<ItemTemplate>
<div class="Row clear">
<div class="Column Cat"><%# ((Price)Container.DataItem).AgeCategory %></div>
<div id="dWasPrice" runat="server" class="Column Was">
<asp:Literal ID="litPreviousPrice" runat="server" /><span class="strike"></span>
</div>
<div class="Column Now">$<%# ((Price)Container.DataItem).FullPrice %></div>
<div class="Column Deposit">
<asp:DropDownList ID="ddlCountForPart" runat="server" skucode="<%# ((Price)Container.DataItem).PartHeaderCode %>">
<asp:ListItem Text="0" Value="0"></asp:ListItem>
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
<asp:ListItem Text="4" Value="4"></asp:ListItem>
<asp:ListItem Text="5" Value="5"></asp:ListItem>
<asp:ListItem Text="6" Value="6"></asp:ListItem>
<asp:ListItem Text="7" Value="7"></asp:ListItem>
<asp:ListItem Text="8" Value="8"></asp:ListItem>
</asp:DropDownList>
</div>
<div class="Column Full">
<asp:DropDownList ID="ddlCountForFull" runat="server" skucode="<%# ((Price)Container.DataItem).FullHeaderCode %>">
<asp:ListItem Text="0" Value="0"></asp:ListItem>
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
<asp:ListItem Text="4" Value="4"></asp:ListItem>
<asp:ListItem Text="5" Value="5"></asp:ListItem>
<asp:ListItem Text="6" Value="6"></asp:ListItem>
<asp:ListItem Text="7" Value="7"></asp:ListItem>
<asp:ListItem Text="8" Value="8"></asp:ListItem>
</asp:DropDownList>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
这是绑定。
private void BindDetails()
{
SetDiscountContent();
rptPricing.DataSource = _Detail.Prices;
rptPricing.DataBind();
}
这是数据不存在的地方。
if ( IsPostBack && UIControlUtil.GetPostBackControl(Page) == lnkAddToCart)
{
//populate the cart without javascript
//find the drop down with items selected and create Order Elements
try
{
var orders = new Order[0];
foreach (RepeaterItem item in rptPricing.Items)
{
var dl = item.FindControl("ddlCountForFull");
if (dl != null)
{
Array.Resize(ref orders, orders.Length + 1);
orders[orders.Length - 1] = new Order() {
Quantity = Convert.ToInt32(dl.SelectedValue),
Sku = dl.Attributes["skucode"]};
我尝试将BindDetails()
的呼叫从PageLoad
转移到OnInit
,并且还使用回发标记。我也玩ViewState
旗帜无济于事。
编辑(添加页面加载),请注意我已将其移至OnInit。
protected void Page_Load(object sender, EventArgs e)
{
if (ContentItem == null) return;
if (!IsPostBack)
{
var control = (ContentManagement.Library.Sitecore.PreDefinedControl)ContentItem;
_Detail = (Details)control.DataElements[0];
BindDetails();
}
if (ShoppingCartHelper.GetProductsOfTInCart<blah>() > 8)
{
lnkAddToCart.Enabled = false;
lnkAddToCart.CssClass = lnkAddToCart.CssClass + " disabled cartmax";
}
}
答案 0 :(得分:2)
首先请确保您正在执行以下操作:
if (!IsPostBack)
{
BindDetails();
}
这将确保您不会受到每次回发的约束并丢失您的数据。
另外,我不明白需要:
if ( IsPostBack && UIControlUtil.GetPostBackControl(Page) == lnkAddToCart)
为什么不只为OnClick
或Button
实现LinkButton
处理程序并移动所有代码以将数据从重复数据中提取到处理程序中?
另外,请确保ViewState
已启用,否则您将无法遍历Repeater
以查找更改的值。
修改强>
检查您是否在ViewState
存在的容器中禁用了Repeater
。您应该拥有数据。您不会在控件或其中一个父节点上禁用ViewState
或者以某种方式擦除值(例如重新绑定)的唯一原因。您可以发布更多代码,例如Page_Load
以及之后会发生的任何事件吗?
尝试在TextBox
之后加Repeater
并在其中输入soem文字,然后发布Repeater
。你能看到你输入的TextBox
中的文字吗?如果没有,则父控件已禁用ViewState
或在页面级别。如果您可以看到该值,那么您无意中重新绑定到Repeater
并将数据擦除。您需要在执行绑定的位置放置断点,并确保在回发时不会发生断点。
此时我可以给你的帮助不多。