I have this function (as well as another that is almost just like it). This function draws a table with some info, then I add a button. It worked while I actually had a table on the webpage, but when I try to add the table dynamically, it quits working:
Public Sub DrawSingleSystemWithButton(ByVal Position As Integer, ByVal Systems1 As MainSystems, ByVal NestedSystems1 As NestedSystems, ByVal ButtonText As String)
Dim SystemTable As Web.UI.WebControls.Table = New Web.UI.WebControls.Table
Dim ButtonRow As System.Web.UI.WebControls.TableRow = New System.Web.UI.WebControls.TableRow
Dim ButtonCell As System.Web.UI.WebControls.TableCell = New System.Web.UI.WebControls.TableCell
Dim MyButton As System.Web.UI.WebControls.Button = New System.Web.UI.WebControls.Button
DrawHeaderRow(SystemTable)
DrawSystem(Position, Systems1, NestedSystems1, SystemTable)
MyButton.Text = ButtonText
MyButton.Width = Unit.Percentage(100)
ButtonCell.ColumnSpan = 2
ButtonCell.Controls.Add(MyButton)
ButtonRow.Cells.Add(ButtonCell)
SystemTable.Rows.Add(ButtonRow)
AddHandler MyButton.Command, AddressOf OnSelect
Me.Page.Controls.Add(SystemTable)
End Sub
It used to be this (I just put a blank table on my asp.net page and passed it in, rather than declaring a table in the function and then doing Page.Controls.Add()). It worked fine like this:
Public Sub DrawSingleSystemWithButton(ByVal Position As Integer, ByVal Systems1 As MainSystems, ByVal NestedSystems1 As NestedSystems, ByVal ButtonText As String, ByRef SystemTable As System.Web.UI.WebControls.Table)
Dim ButtonRow As System.Web.UI.WebControls.TableRow = New System.Web.UI.WebControls.TableRow
Dim ButtonCell As System.Web.UI.WebControls.TableCell = New System.Web.UI.WebControls.TableCell
Dim MyButton As System.Web.UI.WebControls.Button = New System.Web.UI.WebControls.Button
DrawHeaderRow(SystemTable)
DrawSystem(Position, Systems1, NestedSystems1, SystemTable)
MyButton.Text = ButtonText
MyButton.Width = Unit.Percentage(100)
ButtonCell.ColumnSpan = 2
ButtonCell.Controls.Add(MyButton)
ButtonRow.Cells.Add(ButtonCell)
SystemTable.Rows.Add(ButtonRow)
AddHandler MyButton.Command, AddressOf OnSelect
End Sub
So I changed very little. Yet after the changes, it no longer works, and I get this error message:
Quote:Control '_ctl4' of type 'Button' must be placed inside a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Control '_ctl4' of type 'Button' must be placed inside a form tag with runat=server.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[HttpException (0x80004005): Control '_ctl4' of type 'Button' must be placed inside a form tag with runat=server.]
System.Web.UI.Page.VerifyRenderingInServerForm(Control control)
System.Web.UI.WebControls.Button.AddAttributesToRender(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
System.Web.UI.Control.Render(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)
System.Web.UI.WebControls.TableCell.RenderContents(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
System.Web.UI.Control.Render(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
System.Web.UI.WebControls.Table.RenderContents(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
System.Web.UI.Control.Render(HtmlTextWriter writer)
System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
System.Web.UI.Page.ProcessRequestMain()
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032
Does anybody know what the hell might be going on here?