在回发时从中继器捕获更改的值
这困扰着我,我读过无数的解决方案,但似乎都有点不对劲。我有一个中继器,每个项目有 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";
}
}
This is plaguing me and I've read countless solutions that all seem to be off a bit. I have a repeater that has 2 drop downs per item and a few items. These all have potential values 0-8 that the user can select and I want to gather the data on a postback when the user click a button on the bottom of the page.
<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">lt;%# ((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>
Here is the binding.
private void BindDetails()
{
SetDiscountContent();
rptPricing.DataSource = _Detail.Prices;
rptPricing.DataBind();
}
Here is where the data is not present.
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"]};
I've tried moving the call to BindDetails()
around from PageLoad
to OnInit
and also playing with the postback flag. I have also played with the ViewState
flags to no avail.
EDIT (Adding Page Load), please note I have played with moving this to the 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";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,请确保您正在执行以下操作:
这将确保您不会因每次回发而绑定并丢失数据。
另外,我不明白需要:
为什么不只为
Button
或LinkButton
实现OnClick
处理程序并移动所有将数据从重复中拉出到处理程序中的代码?另请确保已启用
ViewState
,否则您将无法遍历Repeater
来查找更改的值。编辑:
检查您是否未在
Repeater
所在的容器中禁用ViewState
。您应该拥有数据。您不这样做的唯一原因是,如果ViewState
在控件或其父级之一上被禁用,或者您正在以某种方式擦除这些值(例如重新绑定)。您可以发布更多代码,例如Page_Load
以及之后发生的任何事件吗?尝试在
Repeater
之后放置一个TextBox
并在其中输入一些文本,然后发布您的Repeater
。您能看到TextBox
中输入的文本吗?如果没有,则父控件已禁用ViewState
或在页面级别。如果您可以看到该值,则说明您无意中在某个地方重新绑定到 Repeater 并清除了数据。您需要在进行绑定的地方放置一个断点,并确保回发时不会发生这种情况。目前我无法为您提供更多帮助。
First off make sure you are doing a:
This will ensure you are not binding with every postback and losing your data.
Also, I do not understand the need for:
Why do you not just implement the
OnClick
handler for aButton
orLinkButton
and move all of your code to pull the data out of the repeated into the handler?Also make sure
ViewState
is enabled or you will not be able to iterate through theRepeater
to find the changed values.EDIT:
Check that you have not disabled
ViewState
in on of the containers that theRepeater
exists in. You should have the data. The only reason you wouldn't is ifViewState
is disabled on the control or one of it's parents or you are wiping the values in some way such as rebinding. Can you post more of your code like yourPage_Load
and any event that will occur after it?Try putting a
TextBox
after theRepeater
and enter soem text in it and then post yourRepeater
. Can you see the text in theTextBox
that you entered? If not then a parent control has disabledViewState
or at the page level. If you can see the value, then somewhere you are rebinding toRepeater
unintentionally and wiping your data out. You need to put a breakpoint where you do the binding and ensure that it is NOT occurring when you post back.There isn't much more help I can give you at this point.