Copied to clipboard

Flag this post as spam?

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


  • grazer 10 posts 31 karma points
    May 26, 2011 @ 01:46
    grazer
    0

    umbraco usercontrol wrapper control not showing current value

    I've created an umbrac usercontrol wrapper Data Type which wraps a DropDownList populated with data from a database.  I have the Database datatype for the Umbraco Data Type set to "Integer", which is correct to the Value property on the item in the DropDownList (an int primary key).

    I can select the value from the data type within the document and save correctly.  This is verified by rending the value of the control on the page.

    <umbraco:Item field="circuitId" runat="server"></umbraco:Item>

    However, when I move to a different document and come back to the original, the value displayed in the drop down list list is the first item in the list.

    My Usercontrol codebehind is this:

        public partial class CircuitsDataType : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
        {
            private ICircuitRepository _circuitRepository;
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) {
    
                    var circuits = _circuitRepository.GetAll();
    
                    CircuitList.DataSource = circuits;
                    CircuitList.DataValueField = "Id";
                    CircuitList.DataTextField = "Name";
                    CircuitList.DataBind();
    
                }
            }
    
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
    
                _circuitRepository = new CircuitRepository();
            }
    
            public object value
            {
                get { return CircuitList.SelectedValue; }
                set
                {                
                    CircuitList.SelectedIndex = -1;
                    var item = CircuitList.Items.FindByValue(value.ToString());
                    if (item != null) item.Selected = true;
    
                }
            }        
        }


    It appears as if the underlying circuitId on the Document is not getting passed into the value property from IUsercontrolDataEditor.

    Also, for extra points ( :p ) can someone remind me the correct way to do this:

    <umbraco:Macro CircuitId='<umbraco:Item field="circuitId" runat="server"></umbraco:Item>' Alias="F1CircuitDetail" runat="server"></umbraco:Macro>
  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 26, 2011 @ 04:37
    Tom Fulton
    0

    Hi,

    I think you'll want to set check and set the item to selected in the Page_Load event (if !Page.IsPostBack) rather than from the value property.  See this blog post for a quick example (uses a textbox but should be the same).

    For your bonus question, use CircuitId="[#circuitId]" - see Advanced Macro Parameter Syntax for more info

    Hope this helps,
    Tom

  • grazer 10 posts 31 karma points
    May 26, 2011 @ 05:17
    grazer
    0

    Thanks Tom. 

    That makes sense re in Page_Load.  Also I think perhaps the repository isn't set. I've also created a ascx that displays the Circuit in the template, based on the value selected in the above Data Type and I noticed last night that OnInit wasn't being fired before the property to set CircuitId was being called, resulting in a fail to call the repository. I suspect that may also be hapenning here. Will investigate.

     

    I tried {$circuitId}, {@circuitId} and the variations of those with square brackets. I remembered doing it a while back but couldn't find where, to refer back :)

Please Sign in or register to post replies

Write your reply to:

Draft