Updating a Control when a (different) Composite Control Changes

J

Jeff

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>
 
J

Jeff

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 said:
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"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top