Reputation: 977
SO i have a dynamically built table that is created by a method and loaded into a panel that acts like a place holder for it and when loading it into the page, it works fine when i upload the page. The table comes up and loads exactly how i want it to. My only issue is that i have two dropboxes that have options the user selects to update those columns of the table with. The Problem is that when i go and click my update button, it searches the page for the corresponding control (i.e. the values in the dropboxes) that is getting the proper value to update the table with which will be explained below.
This is what i am doing with my update button:
protected void Update_Click(object sender, EventArgs e)
{
UpdateFail.Visible = false;
UpdateSucceed.Visible = false;
string MachineTypeID;
string MachineModelID;
string machinetypeid;
string machinemodelid;
int i = 0;
machine = inputsService.GetMachineSiteDetails(SiteID);
foreach (Machine Machine in machine)
{
try
{
machinetypeid = Machine.ID.ToString() + "type";
machinemodelid = Machine.ID.ToString() + "model";
Control type = MyExtensions.FindControlRecursive(this, machinetypeid);
Control model = MyExtensions.FindControlRecursive(this, machinemodelid);
RadComboBox machinetype = (RadComboBox) type;
RadComboBox machinemodel = (RadComboBox) model;
MachineTypeID = machinetype.SelectedValue;
MachineModelID = machinemodel.SelectedValue;
if (MachineTypeID != "")
{
inputsService.UpdateMachineModels(Machine.ID, MachineModelID);
i++;
}
if (MachineModelID != "")
{
inputsService.UpdateMachineTypes(Machine.ID, MachineTypeID);
i++;
}
}
catch (Exception ex)
{
{
logger.ErrorFormat(
"Update_Click exception occurred when attempting to update the database {0}", ex);
}
}
}
if (i != 0)
{
UpdateFail.Visible = false;
UpdateSucceed.Visible = true;
}
else
{
UpdateSucceed.Visible = false;
UpdateFail.Visible = true;
}
//clear out the old table and replace with the newly revized table.
AddTable();
}
Now my problem has to do with the two lines of code below
Control type = MyExtensions.FindControlRecursive(this, machinetypeid);
Control model = MyExtensions.FindControlRecursive(this, machinemodelid);
The method MyExtensions.FindControlRecursive() is this:
public static Control FindControlRecursive( Control root, string id )
{
System.Web.UI.Control controlFound;
if ( root != null )
{
controlFound = root.FindControl( id );
if ( controlFound != null )
return controlFound;
foreach ( Control c in root.Controls )
{
controlFound = FindControlRecursive( c, id );
if ( controlFound != null )
return controlFound;
}
}
return null;
}
Update1 What it is doing is when it gets to the findcontrol method, it can't find the table within the pannel so it returns null and fails to update the table.
What i would like to know is, what am i doing wrong here and how can i fix this issue?
Any help or suggestions are greatly appreciated.
Thank you
Upvotes: 0
Views: 136
Reputation: 11945
Why don't you try this recursive find control:
public static Control FindControlRecursive(this Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
What Shai said too is important - is your table being recreated/initialized with data and user input before you access it in your Update method?
Upvotes: 1
Reputation: 6249
When are you creating the table? If you are creating a table dynamically, it MUST be re-created no later than OnInit for the viewstate to be able to be restored.
Upvotes: 1