Navigation

Search

Categories

On this page

User Controls

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 112
This Year: 50
This Month: 0
This Week: 0
Comments: 0

Sign In

 Monday, May 12, 2008
Monday, May 12, 2008 2:01:26 PM (Eastern Standard Time, UTC-05:00) ( )


User controls are somewhat outdated already with the introduction of partial classes in ASP.NET 2.0 but I still find them useful.  I think of them similar to the include pages in classic ASP. This is a simple example of how to get the text value from a user control on the parent page.

<%@ Page Language="VB" %>
<%@ Register Src="header.ascx" TagName="header" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs)
    Dim CompanyName As TextBox = CType(Page.FindControl("Header1$txtCompanyName"), TextBox)
    lblCompanyName.Text = CompanyName.Text   
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <!--This is my user control page header-->
     <uc1:header id="Header1" runat="server"></uc1:header>
     <br />
     <br />
     <asp:Label ID="lblCompanyName" runat="server" Text="Label" />     
     <asp:TextBox ID="TextBox1" runat="server" />    
    </div>
    </form>
</body>
</html>

This is an example of how to set the text value from inside the user control on the parent page.

<%@ Control Language="VB" ClassName="header" EnableViewState="false" %>

<script runat="server">
        
    Private Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs)
        If Not Page.IsPostBack Then                        
            Dim TextBox1 As Textbox =CType(Me.Parent.FindControl("TextBox1"),TextBox)
            TextBox1.Text = "Eat your veggies"            
        End If
    End Sub
</script> <table width="100%" border="0" cellpadding="3" cellspacing="0"> <tr> <td>Company: <asp:Textbox ID="txtCompanyName" runat="server" CssClass="textbox" Text="Company Name" ToolTip="Enter a company name" />
<asp:Button ID="btnCompanySearch" runat="server" Text="go" CssClass="buttons" OnClick="QuickSearch_Companies_Click" /></td> </tr> </table>