Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • chappers 18 posts 36 karma points
    Oct 23, 2010 @ 22:45
    chappers
    0

    Usercontrol postback issue/bug?

    Hello I’m developing some custom front end user controls for an Umbraco powered site which up until now has been going fine.

    The scenario is as follows:

    I have a user control that has an asp:repeater and below that an asp:button.

    At the moment all I’m doing is outputting some checkboxes in the repeater and when the user clicks the button I want to output what checkboxes were selected (its eventually going to be a filter control for some search results).

    On page load if (it’s not a postback) I bind the repeater.

    The problem is in Umbraco the repeater loses its data on postback caused by the button.  Outside of Umbraco in a normal aspx page this works as I would expect.

    Is this an issue with how Umbraco handles viewstate? I believe the repeater needs it to maintain state?

    This is running on Umbraco v 4.5.2 (Assembly version: 1.0.3891.20719)

    Below is my code – very simple at the moment as I was just testing the idea and I’ve run into problems….

     

    Frontend:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FilterResults.ascx.cs" Inherits="PM.usercontrols.FilterResults" %>

        <div class="filters">

         

          <h4>Filter Results</h4>

                

            <asp:Repeater ID="rptFeatures" runat="server">       

            <HeaderTemplate><ul></HeaderTemplate>

           

            <ItemTemplate>

                <li class="<%# Eval("CssClass") %>"><asp:CheckBox ID="chkFeature" Text='<%# Eval("FeatureName") %>' CssClass="checkbox" runat="server" /></li>

            </ItemTemplate>

           

            <FooterTemplate></ul></FooterTemplate>       

            </asp:Repeater>    

      

         

          <asp:Button ID="btnFilterResults" CssClass="submit" runat="server" Text="Update Results" onclick="btnFilterResults_Click" />

        </div>

       

        <asp:Literal ID="litDebug" runat="server" />

    Code behind:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using PM.BLL;

     

    namespace PM.usercontrols

    {

        public partial class FilterResults : System.Web.UI.UserControl

        {

            protected void Page_Load(object sender, EventArgs e)

            {

                if (!IsPostBack)

                {

                    LoadFeatures();

                }

            }

     

     

            private void LoadFeatures()

            {

                rptFeatures.DataSource = PropertyManager.GetFeatures();

                rptFeatures.DataBind();

            }

     

            protected void btnFilterResults_Click(object sender, EventArgs e)

            {

                string debugInfo = "<br /><br />Repeater Items Checked:<br />";

                for (int i = 0; i < rptFeatures.Items.Count; i++)

                {

                    CheckBox chk = (CheckBox)rptFeatures.Items[i].FindControl("chkFeature");

                    if (chk.Checked)

                    {

                        debugInfo += (chk.Text + "<br />");

                    }

     

     

                }

                litDebug.Text = debugInfo;

     

     

            }

        }

    }

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Oct 24, 2010 @ 09:24
    Richard Soeteman
    0

    Hi Chappers,

    Could you try to move the LoadFeatures to the Page_Init event? Seemed to have the same issue once because of choosing the wrong event.

    Cheers,

    Richard

  • chappers 18 posts 36 karma points
    Oct 24, 2010 @ 12:21
    chappers
    0

    Hi Richard,

    Thanks for the quick reply.  I did try that but without success - it behaves exactly the same. 

    My current thinking is its to do with viewstate and user controls in Umbraco.

     

    If I disable viewstate for the repeater I can replicate the same behaviour when I use the control in a normal aspx page outside of Umbraco which is what is leading me to think its the way umbraco is handling the viewstate.  I might be way off here as I would have thought many other people would have posted similar problems.

     

    Cheers

    Chris

  • Jamie Howarth 306 posts 773 karma points c-trib
    Oct 26, 2010 @ 12:51
    Jamie Howarth
    0

    Hi Chappers,

    My first thought would be to explicitly state EnableViewState both on the usercontrol and on the <asp:Repeater> control itself, as viewstate might be turned off by details.
    As Richard said, I'd also double-check the command pipeline to see whether you should be calling the DataBind() elsewhere.

    Best,

    Benjamin

  • chappers 18 posts 36 karma points
    Oct 26, 2010 @ 13:00
    chappers
    0
    Hi Benjamin,
    Thanks so much for the quick reply. 
    I did try but it still behaves the same....
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FilterResults.ascx.cs" Inherits="PM.usercontrols.FilterResults" EnableViewState="True" %>
        <div class="filters">
          
          <h4>Filter Results</h4>              
       
           <asp:Repeater ID="rptFeatures" runat="server" EnableViewState="True">        
            <HeaderTemplate><ul></HeaderTemplate>
            
            <ItemTemplate>
                <li class="<%# Eval("CssClass") %>"><asp:CheckBox ID="chkFeature" Text='<%# Eval("FeatureName") %>' CssClass="checkbox" runat="server" /> </li>
            </ItemTemplate>
            
            <FooterTemplate></ul></FooterTemplate>        
            </asp:Repeater>     
       
          
          <asp:Button ID="btnFilterResults" CssClass="submit" runat="server" Text="Update Results" onclick="btnFilterResults_Click" /><asp:Literal ID="litDebug" runat="server" />
        </div>
       
    Just for the record I also tried adding the usercontrol to the template directly rather than via a macro to try and rule out that but still behaves the same :-(
  • chappers 18 posts 36 karma points
    Oct 26, 2010 @ 19:26
    chappers
    0

    Still no further with this. If anyone wants to see what I mean just create a user control (with code below), drop into ~/usercontrols and then wrap in a macro to insert into some page or template:

    Outside of umbraco in a normal aspx page this works as you would expect.

     

    ASCX

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="BugTest.ascx.cs" Inherits="BugTest" %>
    <asp:Repeater ID="rptFeatures" runat="server" EnableViewState="True">       
        <HeaderTemplate><ul></HeaderTemplate>
       
        <ItemTemplate>
            <li class="<%# Eval("CssClass") %>"><asp:CheckBox ID="chkFeature" Text='<%# Eval("FeatureName") %>' CssClass="checkbox" runat="server" /> </li>
        </ItemTemplate>
       
        <FooterTemplate></ul></FooterTemplate>       
    </asp:Repeater>

    <asp:Button ID="btnDebug" runat="server" Text="What is checked?"
        onclick="btnDebug_Click" />
    <br /><br />

    <asp:Literal ID="litDebug" runat="server"></asp:Literal>

    CODEBEHIND

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;

    public partial class BugTest : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadFeatures();
            }
        }

        private void LoadFeatures()
        {
            DataTable dtDummyData = new DataTable("Feature");
            dtDummyData.Columns.Add("FeatureId");
            dtDummyData.Columns.Add("FeatureName");
            dtDummyData.Columns.Add("CssClass");

            dtDummyData.Rows.Add("1", "Air Conditioning", "air-conditioning");
            dtDummyData.Rows.Add("2", "Parking", "parking");
            dtDummyData.Rows.Add("3", "Raised Floors", "raised-floors");


            rptFeatures.DataSource = dtDummyData;
            rptFeatures.DataBind();

        }

        protected void btnDebug_Click(object sender, EventArgs e)
        {
            string debugInfo = "<br /><br />Repeater Items Checked:<br />";
            for (int i = 0; i < rptFeatures.Items.Count; i++)
            {
                CheckBox chk = (CheckBox)rptFeatures.Items[i].FindControl("chkFeature");
                if (chk.Checked)
                {
                    debugInfo += (chk.Text + "<br />");
                }
            }

            litDebug.Text = debugInfo;
        }

    }

     

     

     

     

  • chappers 18 posts 36 karma points
    Oct 29, 2010 @ 02:02
    chappers
    0

    UPDATE:

    I think I have tracked down to a piece of code in one of the Master pages in the project.  I didn't do this part of the project so I'm checking with the developer who did in the morning if there is any reason for the following...

    <script runat="server">

    protected override void OnLoad(EventArgs e)

    {

    base.OnLoad(e);

    Page.DataBind();

    }

    </script>

     

    I'm 99% sure that is what has been screwing up the postbacks on databound controls like repeaters!

    I was thrown further off the scent by the tests I did on another umbraco site - it just so happens the same developer did the master pages on that project and I'll bet the same snippet is in that projects masterpage!

    Will keep you posted but I think I'm finally getting somewhere!

    Cheers and a rather large "Doh!"

  • chappers 18 posts 36 karma points
    Oct 29, 2010 @ 10:43
    chappers
    0

    UPDATE:

    Problem solved the code was something to do with they way JavaScript files were being added in at the bottom of the page (keeping scripts down the bottom speeds page loading up slightly).  It was some legacy stuff to help with URL resolving of the JS files and in this project it was safe to remove.

    I believe that's sorted then!  Thanks to those who offered assistance

Please Sign in or register to post replies

Write your reply to:

Draft