在回发时从中继器捕获更改的值

发布于 2024-11-26 14:26:09 字数 4388 浏览 3 评论 0原文

这困扰着我,我读过无数的解决方案,但似乎都有点不对劲。我有一个中继器,每个项目有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[浮城] 2024-12-03 14:26:09

首先,请确保您正在执行以下操作:

if (!IsPostBack)
{
    BindDetails();
}

这将确保您不会因每次回发而绑定并丢失数据。

另外,我不明白需要:

if ( IsPostBack && UIControlUtil.GetPostBackControl(Page) == lnkAddToCart)

为什么不只为 ButtonLinkBut​​ton 实现 OnClick 处理程序并移动所有将数据从重复中拉出到处理程序中的代码?

另请确保已启用 ViewState,否则您将无法遍历 Repeater 来查找更改的值。

编辑:

检查您是否未在 Repeater 所在的容器中禁用 ViewState。您应该拥有数据。您不这样做的唯一原因是,如果 ViewState 在控件或其父级之一上被禁用,或者您正在以某种方式擦除这些值(例如重新绑定)。您可以发布更多代码,例如 Page_Load 以及之后发生的任何事件吗?

尝试在 Repeater 之后放置一个 TextBox 并在其中输入一些文本,然后发布您的 Repeater。您能看到 TextBox 中输入的文本吗?如果没有,则父控件已禁用 ViewState 或在页面级别。如果您可以看到该值,则说明您无意中在某个地方重新绑定到 Repeater 并清除了数据。您需要在进行绑定的地方放置一个断点,并确保回发时不会发生这种情况。

目前我无法为您提供更多帮助。

First off make sure you are doing a:

if (!IsPostBack)
{
    BindDetails();
}

This will ensure you are not binding with every postback and losing your data.

Also, I do not understand the need for:

if ( IsPostBack && UIControlUtil.GetPostBackControl(Page) == lnkAddToCart)

Why do you not just implement the OnClick handler for a Button or LinkButton 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 the Repeater to find the changed values.

EDIT:

Check that you have not disabled ViewState in on of the containers that the Repeater exists in. You should have the data. The only reason you wouldn't is if ViewState 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 your Page_Load and any event that will occur after it?

Try putting a TextBox after the Repeater and enter soem text in it and then post your Repeater. Can you see the text in the TextBox that you entered? If not then a parent control has disabled ViewState or at the page level. If you can see the value, then somewhere you are rebinding to Repeater 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文