Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Element 'div' cannot be nested within element 'table'.

Status
Not open for further replies.

mrgulic

Technical User
Sep 18, 2001
248
US
What can i use instead of a <div> to wrap around a <tr> as I get a warning on build. It still works as expected but I don't like warnings.

Code:
<table>
[red]<div id="div_VENDOR" runat="server" visible="false">[/red]
<tr>
	<td class="td_label_grey">
		<label class="label_label">Vendor:</label>
	</td>
	<td>
		<asp:Label CssClass="label_text" runat="server" ID="lbl_vendor"></asp:Label>
		<asp:DropDownList ID="ddl_vendor" CssClass="ddl_style" runat="server" 
			AutoPostBack="True" onselectedindexchanged="ddl_vendor_SelectedIndexChanged">
		</asp:DropDownList>
	</td>
</tr>
</table>

Initially I had the following

Code:
<table>
[red]<tr id="tr_VENDOR" runat="server" visible="false">[/red]
	<td class="td_label_grey">
		<label class="label_label">Vendor:</label>
	</td>
	<td>
		<asp:Label CssClass="label_text" runat="server" ID="lbl_vendor"></asp:Label>
		<asp:DropDownList ID="ddl_vendor" CssClass="ddl_style" runat="server" 
			AutoPostBack="True" onselectedindexchanged="ddl_vendor_SelectedIndexChanged">
		</asp:DropDownList>
	</td>
</tr>
</table>

....but my find control method that dynamically handles the enable and visibility of the controls can not seem to find the DropDownList ddl_vendor. It seems to stem from the runat="server" in the <tr> element. But if I don't have runat="server" in the element then I can't show and then re-hide the row from the code behind.


FindControl method

Code:
foreach (Control ctlMaster in Page.Controls)
{
	if (ctlMaster is MasterPage)
	{
		foreach (Control ctlForm in ctlMaster.Controls)
		{
			if (ctlForm is HtmlForm)
			{
				foreach (Control ctlContent in ctlForm.Controls)
				{
					if (ctlContent is ContentPlaceHolder)
					{
						foreach (Control ctlParent in ctlContent.Controls)   //Find user control
						{
							foreach (Control ctlChild in ctlParent.Controls)
							{
								foreach (Control ctlGrandChild in ctlChild.Controls)
								{
									if (ctlGrandChild is TextBox)
									{
										((TextBox)ctlGrandChild).CssClass = myStyle;
										((TextBox)ctlGrandChild).Enabled = p;
									}
									else if (ctlGrandChild is Label && ((Label)ctlGrandChild).CssClass == "label_text")
									{
										((Label)ctlGrandChild).Visible = !p;
									}
									else if ((ctlGrandChild is DropDownList))
									{
										((DropDownList)ctlGrandChild).Enabled = p;
										((DropDownList)ctlGrandChild).Visible = p;
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

I know there are more efficient and prettier ways to do this, but I am very new to .NET and programming in general so I was never able to get others code to work for my situation so I made my own.

Thanks in advance for you time and helpful comments.
 
First the <div> would have to be in a <td> to be valid. It may "work" as you say but I suspect you are checking in IE. It will probably not work correctly in FF or older versions of IE
Second, please explain what you are tryiing to do. I have no idea why you would write so many nested loops to find a control on the page.
 
Yes, I agree. The nested FindControl methods look a nightmare and will be very difficult to maintain.

Judging from the actual controls you are looping through (e.g. from the Master Page) it would appear that you are simply trying to show/enable certain controls on the Master Page from the Content Page, is that correct? If so, there is a much easier way...

Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
First of all, thanks for your quick reply.

ca8msm: Actually I am showing/enabling certain controls in a user control on a content page that use's a master page.

jbenson001: The reason there are so many nested loops to find a control on the page is, as I said, i am new to .net. I'm sure there is a simpler way, but lack of experience prevents this at the moment.

It is a re-occuring scene for me that months after I write some code I figure out a better way to do the same action and re-wite it. I am just not at that point with this method (yet).

Im trying to show/hide a table row from the code behind. This can only be done (it seems) if the row has the attribute runat="server". Problem is that when the runat="server" is added to the table row the control is not found with my FindControl method.

I also use this elsewhere to clear TextBox controls of values dynamically. I thought it was at least a step forward to do it dynamically instead of staticly. (e.g. textbox1.Text = "";)

 
OK, the principle of what you are trying to achieve is still the same, we're now just looking to do it on a user control.

Rather than trying to find a control within a user control, which as you've found could be located anywhere, it's better to expose a property or function that will let you hide them. For example, in your user control you could have something like:
Code:
    Public Property ShowMyControl() As Boolean
        Get
            Return TextBox1.Visible
        End Get
        Set(ByVal value As Boolean)
            TextBox1.Visible = value
        End Set
    End Property
and then from your content page you just need to say:
Code:
    MyUserControl.ShowMyControl = False


Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top