0

I have an UpdatePanel and in my updatepanel_Load I have some code which looks like this:

if (!IsPostBack || triggeredRefresh.Value == "1") 
{
create hidden fields and add to list using
itemFields.Add(newField);
} 
else if ( triggeredCheck.Value == "1" )
{
lookup field values
}

The list is declared at class level using: List itemFields = new List();

The problem is that whenever I want to lookup values in the hidden fields the list is empty. Why is it empty at this point and how can I fix it?

Thanks

Bala R
  • 107,317
  • 23
  • 199
  • 210
Jimmy
  • 349
  • 1
  • 9
  • 14

1 Answers1

0

Class level fields are not persisted between postbacks. Use the Session state Collection to persist values. For persisting controls, you can use <asp:PlaceHolder />.

EDIT:

If you are using the HiddenField to just store a single value and access from server side and if it's not being accessed from client script, you can do something like this.

Remove your class level list.

if (!IsPostBack || triggeredRefresh.Value == "1") 
{
    Session["someValueKey"] = 0;
} 
else if ( triggeredCheck.Value == "1" )
{
    var x = Convert.ToInt32(Session["someValueKey"]);
}

if you do need a list of values, then you can do

if (!IsPostBack || triggeredRefresh.Value == "1") 
{
    Session["someValueKey"] = new List<int>{100,200};
} 
else if ( triggeredCheck.Value == "1" )
{
    var x = Session["someValueKey"] as List<int>();
}

if you do need it to be a control (to access from client script) you can do

if (!IsPostBack || triggeredRefresh.Value == "1") 
{
   HiddenField hiddenField = new HiddenField();
   hiddenField.ID ="hiddenField1";
   hiddenField.Value = "0";
   placeHolder1.Controls.Add(hiddenField);
} 
else if ( triggeredCheck.Value == "1" )
{
    HiddenField hiddenField = placeHolder1.FindControl("hiddenField1") as HiddenField;
    var x = Convert.ToInt32(hiddenField.Value);
}
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • so can I create the list at class level still but make it maintain it's state between postbacks? like List itemFields = (List)Session["itemFields"]; or something? I tried using a placeholder with id="hiddenfields" but then I got argumentoutofrange when trying to access hiddenfields.Controls[0] even though I had added controls to the placeholder before the postback took place – Jimmy Jun 16 '11 at 18:34
  • @Jimmy can you post the code you have for `create hidden fields and add to list using` and `lookup field values`. That'll give us a better idea. – Bala R Jun 16 '11 at 18:35
  • also I declared the placeholder like this in the xaml: – Jimmy Jun 16 '11 at 18:42
  • Sorry for the late reply, and thanks for the detailed suggestion. I tried your second method (as I need to be able to access the hiddenField value from client script) .. but unfortunately it didn't work, I got a NullReferenceException saying "Object reference not set to instance of an object" on the int x = Convert.ToInt32(hiddenField.Value); line. So.. FindControl returned null. :/ any idea why? thanks – Jimmy Jun 16 '11 at 22:19
  • I checked the placeholder's Controls.Count value and it is 0 at the point when I want to check the value. so it seems it isn't holding the hiddenfields consistently over the postback? I am definitely adding a lot of controls to the placeholder before the postback... – Jimmy Jun 16 '11 at 22:29
  • could this be related to trying to check the placeholder in an updatepanel_Load event? – Jimmy Jun 16 '11 at 22:30
  • well apparently the placeholder controls get killed in the page lifecycle. i've decided to hardcode my hiddenfields for now. it's a bit messy as they are supposed to be dynamically generated (from database records) but it will do for now! thanks for the help – Jimmy Jun 17 '11 at 00:06