locked
Custom servercontrol with control-type property. Visibility problem. RRS feed

  • Question

  • User247235378 posted

    Hi,

    I made own control via inheriting it from TextBox-control. I also made new Label-type property.

    Everything seems to go ok but when I try modify referenced Label-control nothing happens.

    Example:

    Imports System.Web
    Imports System.Web.UI
    Imports System.ComponentModel
    Imports System.Web.UI.WebControls
    
    
        <ToolboxData("<{0}:OwnTextbox runat=server></{0}:OwnTextbox>")> _
        <ParseChildren(True)> _
        <PersistChildren(False)> _
        Public Class OwnTextBox
            Inherits TextBox
           
    
            Private _TitleLabel As Label
    
            <Category("OwnPropertys")> _
            <PersistenceMode(PersistenceMode.InnerProperty)> _
            Public Property TitleLabel As Label
                Get
                    Return _TitleLabel 
                End Get
                Set(ByVal value As Label)
                    _TitleLabel = value
                End Set
            End Property
        End Class

    End Class

    I can select Label-type control on OwnTexbox-property dialog. But when I try modify label text like 

    OwnTextBox1.TitleLabel.Text = "yeah"

    nothing happens.

    I can control it like 

    CType(FindControl(OwnTextBox1.TitleLabel.ClientID), Label).Text = "yeah"

    and it works but I think this is not best way.

    Tuesday, July 19, 2011 6:36 AM

All replies

  • User3866881 posted

    Hello OP:)

    In my mind I think because everytime when you click something like submit to render the whole page, the label will be automatically crreated again.

    I think you can have a try like this——

    Imports System.Web
    Imports System.Web.UI
    Imports System.ComponentModel
    Imports System.Web.UI.WebControls


       
    <ToolboxData("<{0}:OwnTextbox runat=server></{0}:OwnTextbox>")> _
       
    <ParseChildren(True)> _
       
    <PersistChildren(False)> _
       
    Public Class OwnTextBox
           
    Inherits TextBox 
           

           
    Private _TitleLabel As Label= New Label

            Public Sub New()

                 _TitleLabel.Text = IIf(ViewState("txt")==Nothing,String.Empty,ViewState["txt"].ToString())

            End Sub

           Public Property LabelText As String
           Get
                 Return ViewState("txt")
           End Get
            Set(ByVal value As String) 
                 ViewState("txt") = value
           End Set

          End Property

                 

           
    <Category("OwnPropertys")> _
           
    <PersistenceMode(PersistenceMode.InnerProperty)> _
           
    Public ReadOnly Property TitleLabel As Label
               
    Get
                   
    Return _TitleLabel 
               
    End Get 
           
    End Property
       
    End Class

    Wednesday, July 20, 2011 10:08 PM
  • User247235378 posted

    I think it same use findcontrol then. Meaning is make property which point to Label-control. When textbox raises some error or something it automatically change label's text.

     

    Thursday, July 21, 2011 7:10 AM
  • User-16411453 posted

    You have to assign a ID value to the control, in order to access it. If you don't assign an id, then asp.net will assign an id for it. What you name the control is only reference inside the server control on the server side. ID's are required for javascript to find the control.

    I use .ID = [ID] & "_label_control" where the [ID] is the name of the control, for multiple control use.

    Then in jquery $('[id*="_label_control"]').text = "Some text";

    Sunday, July 31, 2011 12:32 AM