User control combining TextBox and RadioButtonList

S

Silvia Elena

Hi all!
I've implemented a RadioButtonListOther that looks like:

- red
- blue
- green
- other

and when an user click on "other" a TextBox appears in order to let
him to specifiy the "other" color.
All works fine, but the TextBox, on the PostBack Event, doesn't
maintains the value inserted by the user.

Here is the code I wrote:

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Math


Namespace WebControlsSilvia

Public Class RadioButtonListOther : Inherits RadioButtonList


Private OtherText As TextBox = New TextBox

Public Property Other() As Boolean
Get
Return CType(ViewState("Other"), String)
End Get
Set(ByVal val As Boolean)
ViewState("Other") = val
End Set
End Property

Public Property OtherMessage() As String
Get
Return CType(ViewState("OtherMessage"), String)
End Get
Set(ByVal val As String)
ViewState("OtherMessage") = val
End Set
End Property

Public Property OtherItem() As Integer
Get
Return CType(ViewState("OtherItem"), String)
End Get
Set(ByVal val As Integer)
ViewState("OtherItem") = val
End Set
End Property

Protected Overrides Sub OnInit(ByVal e As EventArgs)
OtherText.ID = Me.ID & "_al"
Dim a As String = ""
If Me.OtherMessage <> "" Then
a = Me.OtherMessage
End If
If ViewState("OtherMessage") Is Nothing Then
ViewState("OtherMessage") = a
End If
OtherText.Text = ViewState("OtherMessage")
OtherText.Visible = False
OtherText.EnableViewState = True
MyBase.SaveViewState()
OtherText.EnableViewState = True
If Me.Other = True Then
Controls.Add(OtherText)
End If
End Sub

Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
If ViewState("OtherItem") = 0 Then Me.OtherItem =
Me.Items.Count - 1
MyBase.AutoPostBack = True
MyBase.Render(writer)
If Me.Other = True Then
If Me.Items(Me.OtherItem - 1).Selected = True Then
OtherText.Visible = True
Else
OtherText.Visible = False
End If
ViewState("OtherMessage") = OtherText.Text
OtherText.RenderControl(writer)
End If
End Sub

Protected Overrides Sub OnSelectedIndexChanged(ByVal e As
System.EventArgs)
If ViewState("OtherItem") = 0 Then Me.OtherItem =
Me.Items.Count - 1
If Me.Other = True Then
ViewState("OtherMessage") = OtherText.Text
If Me.Items(Me.OtherItem - 1).Selected = True Then
OtherText.Visible = True
Else
OtherText.Visible = False
End If
End If
MyBase.OnSelectedIndexChanged(e)
End Sub

End Class

End Namespace

The aspx page looks like:

<WCS:RadioButtonListOther ID="RBLO1" runat="server" OtherItem="6"
Other=True>
<asp:ListItem id="al1" value="1" Text="first"
runat="server"></asp:ListItem>
<asp:ListItem id="al2" value="2" Text="second"
runat="server"></asp:ListItem>
<asp:ListItem id="al3" value="3" Text="third"
runat="server"></asp:ListItem>
<asp:ListItem id="al4" value="4" Text="four"
runat="server"></asp:ListItem>
<asp:ListItem id="al5" value="5" Text="five"
runat="server"></asp:ListItem>
<asp:ListItem id="al6" value="6" Text="Other"
runat="server"></asp:ListItem>
</WCS:RadioButtonListOther>

What is wrong?

Thanks a lot,
Silvia
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top