Updating a Control when a (different) Composite Control Changes

Discussion in 'ASP .Net' started by Jeff, Mar 21, 2005.

  1. Jeff

    Jeff Guest

    Hi -

    I am developing a composite control using VB.NET.

    In the ASP.NET page using the control, How can I update a label text value
    to reflect the value of a property of the custom control whenever the
    composite control is changed? The label is not a part of the custom
    control.

    I've managed to correctly set the composite control properties whenever it
    is changed, but I don't know how or where to update the label.

    What am I doing wrong?

    My code is below. Thanks for your help.

    - Jeff


    <DefaultProperty("SelectedValue"), ToolboxData("<{0}:MyControl
    runat=server></{0}:MyControl>")> Public Class MyControl
    Inherits System.Web.UI.WebControls.WebControl
    Implements INamingContainer

    Dim WithEvents ddlMyControl as New DropDownList
    Dim sSelectedValue as String

    <Bindable(True), Category("Data"), DefaultValue("A")> Property
    [SelectedValue]() As String
    Get
    Return sSelectedValue
    End Get

    Set(ByVal Value As String)
    EnsureChildControls()
    sSelectedValue = Value
    End Set

    End Property

    Protected Overrides Sub CreateChildControls()
    popItems()
    Controls.Add(ddlMyControl)
    ddlMyControl.AutoPostBack = True
    End Sub

    Private Sub popItems()
    Dim iItem as ListItem

    iItem = New ListItem
    iItem.Value = "A"
    iItem.Text = "A Alpha"
    ddlMyControl.Items.Add(iItem)

    iItem = New ListItem
    iItem.Value = "B"
    iItem.Text = "B Bravo"
    ddlMyControl.Items.Add(iItem)

    iItem = New ListItem
    iItem.Value = "C"
    iItem.Text = "C Charlie"
    ddlMyControl.Items.Add(iItem)

    End Sub

    Public Sub ControlChanged(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles ddlMyControl.SelectedIndexChanged
    SelectedValue = ddlMyControl.SelectedValue
    End Sub

    End Class

    In the aspx page:

    <%@Register TagPrefix="enp" Namespace="eNPControls2" Assembly="eNPControls2"
    %>

    ....

    <enp:eNPMyControl id=ENPMyControl1 runat="server">
    </enp:eNPMyControl>

    <asp:Label id=lblCtrl runat="server" Text="<%=eNPMyControl.SelectedValue%>">
    </asp:Label>
    Jeff, Mar 21, 2005
    #1
    1. Advertising

  2. Jeff

    Jeff Guest

    Additional info:

    I've tried adding the following line to Page_Load event for the page:

    Me.lblCtrl.Text = Mme.ENPState1.SelectedValue

    When stepping through, it seems that the SelectedValue of the composite
    control (ddlMyControl) gets set properly, but the SelectedValue of the
    control on the aspx page (eNPMyControl) never gets set.

    What am I missing?

    - Jeff


    "Jeff" <> wrote in message
    news:QuF%d.385$...
    > Hi -
    >
    > I am developing a composite control using VB.NET.
    >
    > In the ASP.NET page using the control, How can I update a label text value
    > to reflect the value of a property of the custom control whenever the
    > composite control is changed? The label is not a part of the custom
    > control.
    >
    > I've managed to correctly set the composite control properties whenever it
    > is changed, but I don't know how or where to update the label.
    >
    > What am I doing wrong?
    >
    > My code is below. Thanks for your help.
    >
    > - Jeff
    >
    >
    > <DefaultProperty("SelectedValue"), ToolboxData("<{0}:MyControl
    > runat=server></{0}:MyControl>")> Public Class MyControl
    > Inherits System.Web.UI.WebControls.WebControl
    > Implements INamingContainer
    >
    > Dim WithEvents ddlMyControl as New DropDownList
    > Dim sSelectedValue as String
    >
    > <Bindable(True), Category("Data"), DefaultValue("A")> Property
    > [SelectedValue]() As String
    > Get
    > Return sSelectedValue
    > End Get
    >
    > Set(ByVal Value As String)
    > EnsureChildControls()
    > sSelectedValue = Value
    > End Set
    >
    > End Property
    >
    > Protected Overrides Sub CreateChildControls()
    > popItems()
    > Controls.Add(ddlMyControl)
    > ddlMyControl.AutoPostBack = True
    > End Sub
    >
    > Private Sub popItems()
    > Dim iItem as ListItem
    >
    > iItem = New ListItem
    > iItem.Value = "A"
    > iItem.Text = "A Alpha"
    > ddlMyControl.Items.Add(iItem)
    >
    > iItem = New ListItem
    > iItem.Value = "B"
    > iItem.Text = "B Bravo"
    > ddlMyControl.Items.Add(iItem)
    >
    > iItem = New ListItem
    > iItem.Value = "C"
    > iItem.Text = "C Charlie"
    > ddlMyControl.Items.Add(iItem)
    >
    > End Sub
    >
    > Public Sub ControlChanged(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles ddlMyControl.SelectedIndexChanged
    > SelectedValue = ddlMyControl.SelectedValue
    > End Sub
    >
    > End Class
    >
    > In the aspx page:
    >
    > <%@Register TagPrefix="enp" Namespace="eNPControls2"

    Assembly="eNPControls2"
    > %>
    >
    > ...
    >
    > <enp:eNPMyControl id=ENPMyControl1 runat="server">
    > </enp:eNPMyControl>
    >
    > <asp:Label id=lblCtrl runat="server"

    Text="<%=eNPMyControl.SelectedValue%>">
    > </asp:Label>
    >
    >
    >
    Jeff, Mar 21, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sleigh
    Replies:
    1
    Views:
    2,685
    sleigh
    Feb 12, 2004
  2. Mark Olbert
    Replies:
    4
    Views:
    693
    Mark Olbert
    Mar 1, 2006
  3. news.rcn.com
    Replies:
    2
    Views:
    1,113
    Roedy Green
    Dec 10, 2007
  4. Chad
    Replies:
    0
    Views:
    219
  5. Buzz
    Replies:
    4
    Views:
    180
Loading...

Share This Page