How to expose server control properties from a Web User Control?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to expose server control properties from a Web User Control?

  1. #1
    Todd W Guest

    How to expose server control properties from a Web User Control?


    I have a dilemma. I created an ASP.Net Web User Control with an asp:label
    on it. Then I dragged that control onto an .aspx web form. What I need
    to do is to write code in my ASPX code behind that allows me to SET the label's
    text property at runtime. In all the examples I am finding, it appears that
    the server control's within a User Control can only be set from code WITHIN
    the user control itself.

    Does anyone know how to expose the text property of the label in the User
    Control in such a way that the ASPX page it resides on can set the text property?
    A simple example would be that when the client types in a value in a textbox
    on the web page and then clicks a button, the Label on the User Control has
    its text property set to the text in the text box.

    Thanks in advance,

    Todd

  2. #2
    Constance J. Petersen Guest

    Re: How to expose server control properties from a Web User Control?

    Hi Todd,

    "Todd W" <gladiatori@gladiatori.com> wrote in message
    news:3ca4d47c$1@10.1.10.29...
    > Does anyone know how to expose the text property of the label in the User
    > Control in such a way that the ASPX page it resides on can set the text

    property?
    > A simple example would be that when the client types in a value in a textbox
    > on the web page and then clicks a button, the Label on the User Control has
    > its text property set to the text in the text box.


    To expose the text property, put the following code (VB) in the user control:

    Public MustInherit Class UserLabel
    Inherits System.Web.UI.UserControl
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    Label1.Text = _Text
    End Sub

    Private _Text As String = "user control label"

    Public Property Text() As String
    Get
    Return _Text
    End Get

    Set(ByVal Value As String)
    _Text = Value
    End Set
    End Property

    Then, assuming you register the user control on the Web page as follows:

    <%@ Register tagprefix="sma" tagname="userlabel" Src="UserLabel.ascx"%>

    In the ASPX page load, put the following code:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    Dim oUserLabel As UserLabel = Me.FindControl("smaUserLabel")
    oUserLabel.Text = TextBox1.Text
    End Sub

    --
    Constance Petersen, DevX newsgroup section leader
    SoftMedia Artisans, Inc.
    http://www.smartisans.com
    For useful, usable software and Web sites
    Featured Web design: http://www.keweenawnow.com/
    --
    Please reply in the newsgroup so everyone can benefit



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center