Access ASP.NET Controls in Code-Behind Classes

Updated March 30, 2010
Iron Speed Designer V6.2 and later

Sometimes, however, you just want to display text on a page, place a label adjacent to a text box or display other data that is not necessarily coming from a database field.  In such cases, you can add standard ASP.NET controls to your pages at locations of your choice via the Layout Editor.

Adding ASP.NET controls to a page

Follow these steps to access ASP.NET controls in code-behind classes created by Iron Speed Designer.

Step 1:  Create an application using a table, such as the Orders table in Northwind.

Step 2:  Use the Application Explorer to open the ShowOrdersTable.aspx page.

Step 3:  In the Layout Editor, drag an ASP.NET TextBox control from the Toolbox onto the page.

<asp:textbox id="myTextbox" runat="server" />

Step 3:  Handle the PreRender event for the ShowOrdersTable.aspx page and set the text of the ASP.NET TtextBox control.  This is described in detail in the following sections.

Accessing ASP.NET controls in a Page class

Add your code in the “Page” class of ShowOrdersTable.aspx.cs, located in:

...\<Application Folder>\Orders\ShowOrdersTable.aspx.cs

C#:

public ShowOrdersTable()

{

     this.PreRender += new EventHandler(Page_PreRender);

}

 

private void Page_PreRender(object sender, EventArgs e)

{

     this.myTextbox.Text = "PAGE CLASS";

}

Visual Basic .NET:

Private Sub ShowOrdersTable_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender

     Me.myTextbox.Text = "PAGE CLASS"

End Sub

Accessing ASP.NET controls in TableControl and RecordControl classes

Add your code in the TableControl class or RecordControl class of ShowOrdersTable.aspx.cs, located in:

...\<Application Folder>\App_Code\Orders\ShowOrdersTable.Controls.cs

C# TableControl class:

public OrdersTableControl()

{

     this.PreRender += new EventHandler(TableControl_PreRender);

}

private void TableControl_PreRender(object sender, EventArgs e)

{

     System.Web.UI.WebControls.TextBox myTextbox =

          (System.Web.UI.WebControls.TextBox)(this.Page.FindControlRecursively("myTextbox"));

     myTextbox.Text = "TABLE CONTROL CLASS";

}

C# RecordControl class:

public OrdersTableControlRecordControl()

{

     this.PreRender += new EventHandler(RecordControl_PreRender);

}

private void RecordControl_PreRender(object sender, EventArgs e)

{

     System.Web.UI.WebControls.TextBox myTextbox =

          (System.Web.UI.WebControls.TextBox)(this.Page.FindControlRecursively("myTextbox"));

     myTextbox.Text = "RECORD CONTROL CLASS";

}

Visual Basic .NET TableControl and RecordControl classes:

Private Sub myOrdersTableControlRecordControl_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)

     Handles Me.PreRender

 

     Dim tempTextBox As System.Web.UI.WebControls.TextBox =

          CType(Me.Page.FindControlRecursively("myTextBox"),

     System.Web.UI.WebControls.TextBox)

     tempTextBox.Text = "TABLE CONTROL AND RECORD CONTROL CLASS"

End Sub

Note that “myTextBox” is the ID of the ASP.NET TextBox control.

See Also

Part V: Customizing Application Code