Mega Code Archive

 
Categories / ASP.Net Tutorial / Custom Controls
 

Populating user control properties from the host Web page (C#)

<%@ Page Language="C#" %> <%@ Register Src="Control.ascx" TagName="WebUserControl" 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">     protected void Page_Load(object sender, EventArgs e)     {         this.WebUserControl1.Text = "The quick brown fox jumped over the lazy dog";     } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>Untitled Page</title> </head> <body>     <form id="form1" runat="server">     <div>         <uc1:WebUserControl id="WebUserControl1" runat="server">         </uc1:WebUserControl></div>     </form> </body> </html> File: Control.ascx <%@ Control Language="C#" ClassName="WebUserControl" %> <script runat="server">     private string _text;          public string Text     {         get {             return _text;         }         set {             _text = value;         }     }          protected void Page_Load(object sender, EventArgs e)     {         this.Label1.Text = this.Text;     }     protected void Button1_Click(object sender, EventArgs e)     {         this.Label1.Text = "The quick brown fox clicked the button on the page";     } </script> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />