I working an ASP.Net 4 project in Visual Studio 2010 that allows users to select and run SQL Server 2008 Reporting Services reports.
When the user selects a report, any controls needed to select parameters are programmatically generated within an ASP.NET Panel on a different Tab (i.e., Reports are selected on Tab[0], Parameters are specified on Tab[1], Results (ReportViewer) are seen on Tab[2]).
Parameters are being added to an ASP.Net Panel using the following assignment (sample):
pnlParametersRight.Controls.Add(ctlParam);
After the panel's closing tag (</Panel>), I've added a "Generate Results" button.
Interestingly, when I go to Tab[1], the controls are (correctly added, including valid values in dropdownboxes and listboxes) below the button, instead of above it.
At any rate, once the user selects the parameters values, he or she clicks the "Generate Results" button.
I need to pass the parmeter values from the controls in Tab[1] to Reporting Services.
I'm running into a problem finding the parameter controls.
In order to find the correct names, I built a small method that shows me the names as I step through the code:
WebTab
menu = (WebTab)FindControl("mnuiQuery");
string foo = "";
int c = 0;
// menu.Tabs[1].FindControl("tmpl1").Controls.Count;
string subctl = "";
foreach (Control ctl in menu.Tabs[1].Controls)
{ foo = ctl.ID.ToString(); // foo shows ctl.ID as "tmp1"
foreach (Control ctl2 in ctl.FindControl("tmpl1").Controls) *** here the method blows up with a "Object reference not set to an instance of an object." error
{
c = ctl.FindControl(
"tmpl1").Controls.Count;
if (ctl2.ID != null)
subctl = ctl2.ID.ToString();
}
I don't understand how one line can find the control with ID "tmpl1", but when I try to access it, it blows up. Am I missing something really simple here?
TIA,
badger98023
Hi Michell,
If you added child controls to a tab item, then you may use a helper method of tab item FindChildControl(id), which was designed for that situation. That also can be used if a child control is located inside UserControl or has several nested containers which automatic/dynamic IDs can be unknown.
Control tbox = this.WebTab1.Tabs[1].FindChildControl("TextBox1");if(tbox != null) ((TextBox)tbox).Text = "ok";
Thanks for you input, Viktor.
Part of the problem was that the control was inside a panel in the WebTab.Tab. Made it more difficult to find.
Michell