Reputation: 304
I have a website that is dynamically creating HiddenFields for each item inside a Listbox.
foreach (Checklist c in check)
{
lbxCheckListLevel3.Items.Add(c.Name);
HiddenField hf = new HiddenField();
hf.ID = c.Name;
hf.Value = c.Status + ":" + c.ResponseLabels + ":" + c.Prompt + ":" + c.Notes + ":" + c.ResponseValues;
prompt.Controls.Add(hf);
}
The value of the HiddenField is being changed inside Javascript
And then when the save button is clicked, the OnClick function runs to go through all the HiddenFields and read the Values back out.
for(int i = 0; lbxCheckListLevel3.Items.Count > i; i++)
{
//Update the main checklist with the new information
HiddenField hidden = (HiddenField)FindControl(lbxCheckListLevel3.Items[i].Text);
int index = check.FindIndex(delegate(Checklist c) { return c.Name == lbxCheckListLevel3.Items[i].Text; });
if (check[index].Status != int.Parse(hidden.Value.Split(':')[0]) ||
check[index].Notes != hidden.Value.Split(':')[3] ||
check[index].ResponseValues != hidden.Value.Split(':')[4])
{
check[index].Status = int.Parse(hidden.Value.Split(':')[0]);
check[index].Notes = hidden.Value.Split(':')[3];
check[index].ResponseValues = hidden.Value.Split(':')[4];
check[index].Changed = true;
}
}
My problem, is that the FindControl Line returns NULL. I have brought up Developer tools in IE9 and made sure that it exists and with the correct ID. But it still returns the NULL
If you need anymore information, please ask, and I will do the best I can.
EDIT
The following line is the culperate, I believe because of the dash. Even though it is shown in ID of the Field
Environmental Health – Does the application involve any of the following:
Javascript is still able to find the control and change the values.
I have found another with a dash, and that one is being found without error. Seems to be this line only...
Upvotes: 1
Views: 1855
Reputation: 44605
not only what others have suggested you is valid and you should be sure your controls are recreated at Page_Init
or Page_Load
, also consider that you are adding your controls to the: prompt.Controls
so make sure you search in the right container, for example try searching like this:
HiddenField hidden = (HiddenField)prompt.FindControl(...);
only in this way you are sure you search in the same container as you added your HiddenField
before.
Upvotes: 1