locked
Urgent help needed - Custom RadioButtonList - not giving selected value on postback RRS feed

  • Question

  • User698394135 posted

    Hi, I am creating a custom radio button list. In my case i have to group multiple radio button lists. In other words i've different group of options which will behave as one group. So, i decided to extend radio button list. I've added one new property GroupName in my ExtendedRadioButtonList which i am using in RenderItem method. So, i am explicitly assiging user specified group name to all items in the list. Its working fine as i need. Its grouping multiple Radio Button Lists. But now its not giving me selected value on post back. neither its firing selectedIndexChanged event. I've tried by overriding LoadPostData function. But pointer never goes in that method as far as my radio button group name is not equal to radio button list id. If i use radio button id as group name which is microsoft using by default then it works. So, i hope i am missing some thing or may be i am doing wrong way. Please help.

    Imports System

    Imports System.ComponentModel

    Imports System.Security.Permissions

    Imports System.Globalization

    Imports System.Web.UI

    Imports System.Web.UI.WebControls

    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _

    Public Class ExtendedRadioButtonList Inherits RadioButtonList

    #Region "Properties"

    <Bindable(True), _

    Category("Appearance"), _

    DefaultValue("Group1"), _

    Description("The group name for radio button list."), _

    Localizable(True)> _

    Public Overridable Property GroupName() As String

    Get

    Dim s As String = CStr(ViewState("GroupName"))

    If s Is Nothing Then s = String.Empty

    Return s

    End Get

    Set(ByVal value As String) ViewState("GroupName") = value

    End Set

    End Property

    #End Region

    ''' <summary>

    ''' Overriding to keep the same group name for each radio button in list.

    ''' </summary>

    ''' <param name="itemType"></param>

    ''' <param name="repeatIndex"></param>

    ''' <param name="repeatInfo"></param>

    ''' <param name="writer"></param>

    ''' <remarks></remarks>

    Protected Overrides Sub RenderItem(ByVal itemType As ListItemType, ByVal repeatIndex As Integer, ByVal repeatInfo As RepeatInfo, ByVal writer As HtmlTextWriter)

    writer.AddAttribute("name", GroupName)

    MyBase.RenderItem(itemType, repeatIndex, repeatInfo, writer) End Sub

    End Class

    Wednesday, July 2, 2008 5:16 AM

Answers

  • User698394135 posted

    thanks for reply. i think i could not explain my issue correctly. Actulay my requirements was to group multiple radio button lists on one page. For example i have four columns in one row in a repeater. So, in my case i want to group all the radio button lists in one row. So each row will behave like one group. the reason why i have to use multiple radiobuttonlists because data source are different for each list. i've done some work around and found the solution. All i have to do is to register my control for postback and then overrides LoadPostData. have a look on this and is this make any sence.

    cheers

     

    Imports System
    Imports System.ComponentModel
    Imports System.Security.Permissions
    Imports System.Globalization
    Imports System.Web.UI
    Imports System.Web.UI.WebControls

    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Public Class ExtendedRadioButtonList
        Inherits RadioButtonList

        <Bindable(True), _
            Category("Appearance"), _
            DefaultValue("Group1"), _
            Description("The group name for radio button list."), _
            Localizable(True)> _
            Public Overridable Property GroupName() As String
            Get
                Dim s As String = CStr(ViewState("GroupName"))
                If s Is Nothing Then s = String.Empty
                Return s
            End Get
            Set(ByVal value As String)
                ViewState("GroupName") = value
            End Set
        End Property

        Protected Overrides Sub RenderItem(ByVal itemType As ListItemType, ByVal repeatIndex As Integer, ByVal repeatInfo As RepeatInfo, ByVal writer As HtmlTextWriter)
            writer.AddAttribute("name", GroupName)
            MyBase.RenderItem(itemType, repeatIndex, repeatInfo, writer)
        End Sub

        Protected Overrides Function LoadPostData(ByVal postDataKey As String, _
                                                  ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean

            If postCollection(GroupName) IsNot Nothing Then
                Dim postedValue As String = postCollection(GroupName)
                If Me.Items.FindByValue(postedValue) IsNot Nothing Then
                    If SelectedValue <> postedValue Then
                        Me.SelectedValue = postedValue
                        Return True
                    Else
                        Return False
                    End If
                End If
            Else
                Return False
            End If
        End Function

        Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
            MyBase.OnPreRender(e)
            Page.RegisterRequiresPostBack(Me)
        End Sub

    End Class

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 9, 2008 7:00 AM

All replies

  • User-16411453 posted
    You forgot to put the [] brackers in GroupName when you referenced it. Cleaned up your Property for you as well.  I wanted to run the program, but have no clue how to use this code. I can't see it working the way it is written.

      

    Imports System
    Imports System.ComponentModel
    Imports System.Security.Permissions
    Imports System.Globalization
    Imports System.Web.UI
    Imports System.Web.UI.WebControls

    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Public Class ExtendedRadioButtonList
    Inherits RadioButtonList

    Protected Overrides Sub RenderItem(ByVal itemType As ListItemType, ByVal repeatIndex As Integer, ByVal repeatInfo As RepeatInfo, ByVal writer As HtmlTextWriter)

    MyBase.RenderItem(itemType, repeatIndex, repeatInfo, writer)
    writer.AddAttribute("name", [GroupName])

    End Sub

    #Region "Properties"

    <Bindable(True)> _
    <Category("Appearance")> _
    <DefaultValue("Group1")> _
    <Description("The group name for radio button list.")> _
    <Localizable(True)> Property GroupName() As String

    Get
    Dim
    _GroupName As String = CStr(ViewState("GroupName"))
    If _GroupName Is Nothing Then
    Return String
    .Empty
    Else
    Return
    _GroupName
    End If
    End Get

    Set
    (ByVal Value As String)
    ViewState("GroupName") = Value
    End Set

    End Property

    #End Region

    End Class
     
    My Version of the code. I can see this working correctly
     
     
    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    
    
    <DefaultProperty("Text"), ToolboxData("<{0}:ExtendedRadioButtonList1 runat=server></{0}:ExtendedRadioButtonList1>")> _
    Public Class ExtendedRadioButtonList1
    	Inherits WebControl
    
    	Private RadioButtonList As RadioButtonList
    	Private itemType As ListItemType
    	Private repeatIndex As Integer
    	Private repeatInfo As RepeatInfo
    
    	Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
    
    		If Not Me.Context Is Nothing Then
    			RenderContents(writer)
    		Else
    			RenderDesignMode(writer)
    		End If
    
    	End Sub
    	Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    		MyBase.OnInit(e)
    
    		Controls.Clear()
    
    		Dim cs As ClientScriptManager = Me.Page.ClientScript
    		Dim rsType As Type = Me.GetType()
    
    		Dim Comment As LiteralControl
    		Comment = New LiteralControl
    		Comment.Text = "<!-- ExtendedRadioButtonList1 Control V1.3 for ASP.NET -->" & vbCrLf
    		Controls.Add(Comment)
    
    		
    		RadioButtonList = New RadioButtonList
    		With RadioButtonList
    			.ID = [ID] & "_RadioButtonList"
    			.Attributes.Add("name", [GroupName])
    			.Text = [GroupName]
    			.Controls.Add(RadioButtonList)
    		End With
    
    	End Sub
    	Private Sub RenderDesignMode(ByVal writer As HtmlTextWriter)
    
    		Controls.Clear()
    
    		Dim cs As ClientScriptManager = Me.Page.ClientScript
    		Dim rsType As Type = Me.GetType()
    
    		Dim Comment As LiteralControl
    		Comment = New LiteralControl
    		Comment.Text = "<!-- ExtendedRadioButtonList1 Control V1.3 for ASP.NET -->" & vbCrLf
    		Controls.Add(Comment)
    
    		RadioButtonList = New RadioButtonList
    		With RadioButtonList
    			.ID = [ID] & "_RadioButtonList"
    			.Attributes.Add("name", [GroupName])
    			.Text = [GroupName]
    			.Controls.Add(RadioButtonList)
    		End With
    
    		RenderControl(writer)
    
    	End Sub
    	Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    		MyBase.OnLoad(e)
    
    		itemType = ListItemType.Header
    		repeatIndex = 1
    		repeatInfo.Caption = "Hey"
    
    	End Sub
    
    	<Bindable(True)> _
    	<Category("Appearance")> _
    	<DefaultValue("Group1")> _
    	<Description("The group name for radio button list.")> _
    	<Localizable(True)> Property GroupName() As String
    
    		Get
    			Dim _GroupName As String = CStr(ViewState("GroupName"))
    			If _GroupName Is Nothing Then
    				Return String.Empty
    			Else
    				Return _GroupName
    			End If
    		End Get
    
    		Set(ByVal Value As String)
    			ViewState("GroupName") = Value
    		End Set
    
    	End Property
    
    End Class
    
     
    Thursday, July 3, 2008 3:51 PM
  • User698394135 posted

    thanks for reply. i think i could not explain my issue correctly. Actulay my requirements was to group multiple radio button lists on one page. For example i have four columns in one row in a repeater. So, in my case i want to group all the radio button lists in one row. So each row will behave like one group. the reason why i have to use multiple radiobuttonlists because data source are different for each list. i've done some work around and found the solution. All i have to do is to register my control for postback and then overrides LoadPostData. have a look on this and is this make any sence.

    cheers

     

    Imports System
    Imports System.ComponentModel
    Imports System.Security.Permissions
    Imports System.Globalization
    Imports System.Web.UI
    Imports System.Web.UI.WebControls

    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Public Class ExtendedRadioButtonList
        Inherits RadioButtonList

        <Bindable(True), _
            Category("Appearance"), _
            DefaultValue("Group1"), _
            Description("The group name for radio button list."), _
            Localizable(True)> _
            Public Overridable Property GroupName() As String
            Get
                Dim s As String = CStr(ViewState("GroupName"))
                If s Is Nothing Then s = String.Empty
                Return s
            End Get
            Set(ByVal value As String)
                ViewState("GroupName") = value
            End Set
        End Property

        Protected Overrides Sub RenderItem(ByVal itemType As ListItemType, ByVal repeatIndex As Integer, ByVal repeatInfo As RepeatInfo, ByVal writer As HtmlTextWriter)
            writer.AddAttribute("name", GroupName)
            MyBase.RenderItem(itemType, repeatIndex, repeatInfo, writer)
        End Sub

        Protected Overrides Function LoadPostData(ByVal postDataKey As String, _
                                                  ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean

            If postCollection(GroupName) IsNot Nothing Then
                Dim postedValue As String = postCollection(GroupName)
                If Me.Items.FindByValue(postedValue) IsNot Nothing Then
                    If SelectedValue <> postedValue Then
                        Me.SelectedValue = postedValue
                        Return True
                    Else
                        Return False
                    End If
                End If
            Else
                Return False
            End If
        End Function

        Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
            MyBase.OnPreRender(e)
            Page.RegisterRequiresPostBack(Me)
        End Sub

    End Class

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 9, 2008 7:00 AM
  • User-16411453 posted

    It makes sense and I'm glad it works!.

    But later on, if you want to make a compiled server control dll, you can merge your code with my example.

    Wednesday, July 9, 2008 12:37 PM