HIDDENFIELD.VALUE IS AN EMPTY STRING UPON POSTBACK INSIDE MY CUSTOM CONTROL

Samuel Fernandes

Total Post:159

Points:1117
Posted by  Samuel Fernandes
 1088  View(s)
Ratings:
Rate this:

I have a custom control that has a hidden field. Upon postback I want to obtain the value stored in it, but it's always an empty string. any thoughts?

I am performing client-side manipulation of the hidden field values and have verified in firebug that the fields are correct before issue a post back

Here is the setup:

public class DualListPanel : SWebControl, INamingContainer

    {

        protected IList<DlpItem> UnassignedList { get; set; }

        protected IList<DlpItem> AssignedList { get; set; }

        private HiddenField assignedItemsField, unassignedItemsField;

        public DualListPanel()

        {

            CssClass = "DualListPanel";

            EnableViewState = true;

        }

        #region ViewState

        protected override void LoadViewState(object savedState)

        {

            var state = savedState as object[];

            UnassignedList =   state[0] as List<DlpItem>;

            AssignedList = state[1] as List<DlpItem>;

            base.LoadViewState(state[2]);

        }

        protected override object SaveViewState()

        {

            object[] state = new object[3];

            state[0] = UnassignedList;

            state[1] = AssignedList;

            state[2] = base.SaveViewState();

            return state;

        }

        #endregion

        #region WebControl Overrides

        protected override void OnInit(EventArgs e)

        {

            EnsureChildControls();

            GetUnassignedList(); //omitted method

            GetAssignedList(); //omitted method

            base.OnInit(e);

        }

        protected override void CreateChildControls()

        {         

            assignedItemsField = new HiddenField();

            assignedItemsField.ID = "HiddenAssignedItems";

            assignedItemsField.EnableViewState = true;

            unassignedItemsField = new HiddenField();

            unassignedItemsField.ID = "HiddenUnassignedItems";

            unassignedItemsField.EnableViewState = true;

            Controls.Add(assignedItemsField);

            Controls.Add(unassignedItemsField);

            base.CreateChildControls();

        }

        #endregion

        #region Item Lists Retrieval

        public string GetCommaDelimUnassignedItems()

        {

            return unassignedItemsField.Value;

        }

        public string GetCommaDelimAssignedItems()

        {

            return assignedItemsField.Value;

        }

        #endregion

    }

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: HiddenField.Value is an empty string upon postback inside my custom control

    Hi Samuel, 


    try this:

    protected override void CreateChildControls()        

    {                     

     if(!ispostback){

     assignedItemsField = new HiddenField();    

     assignedItemsField.ID = "HiddenAssignedItems";

     assignedItemsField.EnableViewState = true;         

     unassignedItemsField = new HiddenField();          

     unassignedItemsField.ID = "HiddenUnassignedItems";

     unassignedItemsField.EnableViewState = true;     

     Controls.Add(assignedItemsField);            

     Controls.Add(unassignedItemsField);         

     base.CreateChildControls();     

    }

     }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!