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 dKaul,
The ContentPane (base class of ContentTabItem) has a method
public Control FindChildControl(string id)
That method searches all children of tab items recursively until it finds a Control which has ID equals to the value of "id".Similar search can be implemented by application as well.
In order to use that method the content of tab item should already exist and children should be instantiated. If application fills Controls of tab dynamically, then that method should be used after all content was created.If value returned by that method is not null, then application may typecast it to a specific/expected class like TextBox, WebDatePicker, etc. Though, it is better to use explicit check for type. For example,Control child = tabItem.FindChildControl("TextBox1");if (child is TextBox){ TextBox tb = (TextBox)child; tb.Text = "ok";}
That method will not work if ContentUrl is used, because it is not possible to get reference to a child control located in IFRAME. References to those controls (better to say, their html elements) can be obtained only on client. On client, tab item has similar method: findChild(id), which besides explicit aspx children can get references to html elements located in IFRAME (though, ContentUrl should point to a local aspx, but not to a website).
Can you elaborate your answer. How did you find control from panel?
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
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";
I actually found the solution to this.
Little did I know that controls on the first tab are found in control "tmpl0", those in the 2nd tab are found in "tmpl1", etc.
Ultimately, the control was found using the following:
mnuiQuery.FindControl(
"tmpl1").FindControl("pnlParametersRight").FindControl(rpi.Name);
Thanks for your help,