开发者

Capturing changed values from a Repeater on postback

开发者 https://www.devze.com 2023-03-23 15:22 出处:网络
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

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:Repeat开发者_Python百科er 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>

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";
        }
    }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号