[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.buildingcontrols

User control combining TextBox and RadioButtonList

Silvia Elena

2/23/2007 2:22:00 PM

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