Home > DeveloperSection > Forums > HiddenField.Value is an empty string upon postback inside my custom control
Samuel Fernandes
Samuel Fernandes

Total Post:156

Points:1096
Posted on    August-23-2014 8:33 AM

 ASP.Net ASP.Net 
Ratings:


 1 Reply(s)
 798  View(s)
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

    }



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    August-23-2014 8:35 AM

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();     

}

 }


Don't want to miss updates? Please click the below button!

Follow MindStick