This is a migrated thread and some comments may be shown as answers.

Controls added to a RadNotification in RadAjaxPanel Page_Init don't persist values in Viewstate

1 Answer 120 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 17 Aug 2011, 12:04 PM

Hi folks,

I may be doing something stupid here, but I've spent the morning grinding my teeth long enough that I've decided to ask someone else to point out my error :)

I have a RadNotification, in a RadAjaxPanel:

<telerik:RadAjaxPanel ID="notificationPanel" runat="server" ViewStateMode="Enabled">
     <telerik:RadNotification ID="RadNotification1" KeepOnMouseOver="true" runat="server" VisibleOnPageLoad="false" ContentScrolling="Auto" 
           Width="400" Height="300" TitleIcon="none" ContentIcon="none" Animation="Fade" EnableRoundedCorners="true" EnableShadow="true" Title="Notification Title" ShowCloseButton="true" Pinned="true" Opacity="80"
           AutoCloseDelay="3000" Position="TopRight" OffsetX="-50" OffsetY="50" VisibleTitlebar="true" style="z-index: 35000;">
           <ContentTemplate>
               <asp:Table ID="shoppingCartTable" runat="server" Width="380" ViewStateMode="Enabled">
                
               </asp:Table>
               <asp:Button ID="checkoutButton" runat="server" Text="Convert" OnClick="btnCheckout_Click" />
           </ContentTemplate>
       </telerik:RadNotification>
</telerik:RadAjaxPanel>

A method (in another RadAjaxPanel) creates controls inside "shoppingCartTable" in the contenttemplate of the RadNotification.
The method which fills the table is:

private void FillCartTable()
        {
            RadNotification1.Title = "Purchase order cart";
            if (Session["PurchaseOrderCart"] == null)
            {
                Session["PurchaseOrderCart"] = new Dictionary<long, Dictionary<long, PurchaseOrderLine>>();
            }
            Dictionary<long, Dictionary<long, PurchaseOrderLine>> cart = (Dictionary<long, Dictionary<long, PurchaseOrderLine>>)Session["PurchaseOrderCart"];
 
 
            foreach (long tenderID in cart.Keys)
            {
                TableHeaderRow th = new TableHeaderRow();
                TableHeaderCell thc = new TableHeaderCell();
                thc.ColumnSpan = 4;
                thc.Text = Tender.GetTender(tenderID).Supplier.Name;
                th.Cells.Add(thc);
                shoppingCartTable.Rows.Add(th);
 
                foreach (long TenderLineID in cart[tenderID].Keys)
                {
                    PurchaseOrderLine pol = cart[tenderID][TenderLineID];
 
                    TableRow tr = new TableRow();
                    TableCell tc1 = new TableCell();
                    tc1.Text = " ";
                    tr.Cells.Add(tc1);
 
                    TableCell tc2 = new TableCell();
                    tc2.Text = pol.StockOrServiceDescription;
                    tr.Cells.Add(tc2);
 
                    TableCell tc3 = new TableCell();
                    tc3.Text = pol.QuantityOrdered + " " + pol.QuantityBase.Description + " at " + pol.Price.ToString("C", Global.currencyFormat) + " per " + pol.PricingBase.Description;
                    tr.Cells.Add(tc3);
                     
                    TableCell tc4 = new TableCell();
                    Image b = new Image();
                    b.ImageUrl = "~/images/icons/16/delete.png";
                    tc4.Controls.Add(b);
                    CheckBox c = new CheckBox();
                    c.ID = "removeFromCart_" + tenderID.ToString() + ":" + TenderLineID.ToString();
                    c.EnableViewState = true;
                    c.ClientIDMode = System.Web.UI.ClientIDMode.Static;
                    tc4.Controls.Add(c);
                    tr.Cells.Add(tc4);
 
                    shoppingCartTable.Rows.Add(tr);
                    //sb.Append("<tr><td> </td><td>" + pol.Stock.Product.Number + " (" + pol.Stock.SizeDescription + ")" + "</td><td>" + pol.QuantityOrdered + " " + pol.QuantityBase.Description + "</td></tr>");
                }
            }
            //sb.Append("</table>");
            //sb.Append("<asp:Button ID='btnCheckout' runat='server' Text='Convert' OnClick='btnCheckout_Click' />");
            //RadNotification1.Text = sb.ToString();
 
        }


Showing the notification works just fine, and my content is there. That method is called in Page_Init, so each time the controls are created very early in the page lifecycle, long before any ViewState related events, and they're created with the same IDs (and same uniqueIDs) each time the page loads.


When the onclick event fires for the notificationcontrol's button, the checkboxes are all unchecked. They do exist (see the attached image) and the IDs do match the ones in viewstate. However, they're never re-populated from viewstate.... the attached image is a snapshot taken at the end of Page_Init and shows what I'd expect (Checked is false). However, it's still false after all the viewstate events, at the end of Page_Load - despite the Form values collection quite clearly containing that control's state.


I have temporarily worked around it by manually populating them in Page_Init (by traversing the Form values collection, looking for any with an ID containing "removeFromCart" and then manually assigning the checked property). But surely this isn't the right way to be doing it? Why doesn't Viewstate automatically get set on these controls, given that the ID and UniqueID properties are identical before and after a postback?

Cheers!




1 Answer, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 22 Aug 2011, 10:06 AM
Hello Lee,

The reason could be that when an ajax request is triggered inside one of the RadAjaxPanels it does not update the client state of the controls inside the other one. I would advise you to temporarily set EnableAjax="false" inside the ajax panels and if your logic starts working correctly, to try with a RadAjaxManager and the relevant settings if this is applicable.
This can be achieved by replacing the RadAjaxPanels with regular asp Panels and adding settings to the manager where the panels update each other.

All the best,
Tsvetina
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
Ajax
Asked by
Kevin
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Share this question
or