Home All Groups Group Topic Archive Search About

Radiobuttonlist (a custom control) selectedindexchanged event not firing in User Control

Author
31 Oct 2006 9:58 PM
Martin Frank
I created a custom control so I could control how my RadioButtonList was
rendered.  Adding an instance of the custom control to a form seems to
work no problem.  However if I add an instance to a User Control the
SelectedIndexChanged event does not seem to fire.


Here is code for the custom control - inheriting a radiobuttonlist:

<ToolboxData("<{0}:MyCustomRbList runat=server></{0}:MyCustomRbList>")>
Public Class MyCustomRbList
     Inherits System.Web.UI.WebControls.RadioButtonList

     Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
         Controls.Clear()

         Dim input As String = "<input id={0}{1}{0} name={0}{2}{0}
type={0}radio{0} value={0}{3}{0}{4} "
         If Me.AutoPostBack Then
             input &= "onclick={0}__doPostBack('{1}',''){0}
language=""javascript"""
         End If
         input &= " />"
         writer.WriteLine()
         writer.WriteLine("<table border='0' width='100%'>")
         For i As Integer = 0 To Items.Count - 1
             writer.WriteLine("<tr>")
             writer.Write("<td width='15' valign='middle'>")
             Dim sbInput As New System.Text.StringBuilder
             sbInput.AppendFormat(input, """", MyBase.ClientID & "_" &
i.ToString, MyBase.ClientID, Items.Item(i).Value,
Microsoft.VisualBasic.IIf(Items.Item(i).Selected, " checked", ""),
i.ToString)
             writer.Write(sbInput.ToString)
             writer.Write("</td>")
             writer.WriteLine()
             writer.Write("<td valign='middle'>")
             Dim sbLabel As New System.Text.StringBuilder
             sbLabel.Append(Items.Item(i).Text)
             writer.Write(sbLabel.ToString)
             writer.Write("</td>")
             writer.WriteLine()
             writer.WriteLine("</tr>")
         Next
         writer.WriteLine("</table>")
         writer.WriteLine()

     End Sub
End Class


Here is code from the aspx page (also the code on the ascx is basically
the same):

Public Class zTest
     Inherits System.Web.UI.Page

     Protected WithEvents myRbList2 As BosonRadioButton
     Private Sub myInit()
         Me.myRbList2 = New BosonRadioButton
         Me.myRbList2.EnableViewState = True
         Me.myRbList2.AutoPostBack = True
         Me.myRbList2.Width = New Unit(100.0, UnitType.Percentage)
         Me.Panel1.Controls.Add(Me.myRbList2)

         If Not (Me.Page.IsPostBack) Then
             Me.myRbList2.Items.Clear()
             Me.myRbList2.Items.Add(New ListItem("Blah20", "a"))
             Me.myRbList2.Items.Add(New ListItem("Blah21", "b"))
             Me.myRbList2.Items.Add(New ListItem("Blah22", "c"))
             Me.myRbList2.Items.Add(New ListItem("Blah23", "d"))
         End If
     End Sub

     Private Sub listSingleSelect_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles myRbList2.SelectedIndexChanged
         Dim s As String = ""
         s = Me.myRbList2.SelectedItem.Value
     End Sub
End Class


I spent the good part of a day try to figure it out...no luck so far.


Any help is appreciated.


A few related links I found:
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.aspnet.webcontrols/browse_thread/thread/c35b3a3dc652a9d1/fba6137a7db09de9?lnk=gst&q=SelectedIndexChanged&rnum=2&hl=en#fba6137a7db09de9
After reading this I made sure to a unique value for each item...no effect.

http://groups-beta.google.com/group/microsoft.public.dotnet.framework.aspnet.webcontrols/browse_thread/thread/6df409cc1efe7c93/34cbb139c0264c52?lnk=gst&q=SelectedIndexChanged+radiobuttonlist&rnum=6&hl=en#34cbb139c0264c52
Maybe has something to do with how I am adding the controls.?.

Thank you,
Martin Frank

Author
31 Oct 2006 10:04 PM
Martin Frank
If anyone is wondering...
As BosonRadioButton should say As MyCustomRbList

Thanks.

Martin Frank wrote:
Show quoteHide quote
> I created a custom control so I could control how my RadioButtonList was
> rendered.  Adding an instance of the custom control to a form seems to
> work no problem.  However if I add an instance to a User Control the
> SelectedIndexChanged event does not seem to fire.
>
>
> Here is code for the custom control - inheriting a radiobuttonlist:
>
> <ToolboxData("<{0}:MyCustomRbList runat=server></{0}:MyCustomRbList>")>
> Public Class MyCustomRbList
>     Inherits System.Web.UI.WebControls.RadioButtonList
>
>     Protected Overrides Sub Render(ByVal writer As
> System.Web.UI.HtmlTextWriter)
>         Controls.Clear()
>
>         Dim input As String = "<input id={0}{1}{0} name={0}{2}{0}
> type={0}radio{0} value={0}{3}{0}{4} "
>         If Me.AutoPostBack Then
>             input &= "onclick={0}__doPostBack('{1}',''){0}
> language=""javascript"""
>         End If
>         input &= " />"
>         writer.WriteLine()
>         writer.WriteLine("<table border='0' width='100%'>")
>         For i As Integer = 0 To Items.Count - 1
>             writer.WriteLine("<tr>")
>             writer.Write("<td width='15' valign='middle'>")
>             Dim sbInput As New System.Text.StringBuilder
>             sbInput.AppendFormat(input, """", MyBase.ClientID & "_" &
> i.ToString, MyBase.ClientID, Items.Item(i).Value,
> Microsoft.VisualBasic.IIf(Items.Item(i).Selected, " checked", ""),
> i.ToString)
>             writer.Write(sbInput.ToString)
>             writer.Write("</td>")
>             writer.WriteLine()
>             writer.Write("<td valign='middle'>")
>             Dim sbLabel As New System.Text.StringBuilder
>             sbLabel.Append(Items.Item(i).Text)
>             writer.Write(sbLabel.ToString)
>             writer.Write("</td>")
>             writer.WriteLine()
>             writer.WriteLine("</tr>")
>         Next
>         writer.WriteLine("</table>")
>         writer.WriteLine()
>
>     End Sub
> End Class
>
>
> Here is code from the aspx page (also the code on the ascx is basically
> the same):
>
> Public Class zTest
>     Inherits System.Web.UI.Page
>
>     Protected WithEvents myRbList2 As BosonRadioButton
>     Private Sub myInit()
>         Me.myRbList2 = New BosonRadioButton
>         Me.myRbList2.EnableViewState = True
>         Me.myRbList2.AutoPostBack = True
>         Me.myRbList2.Width = New Unit(100.0, UnitType.Percentage)
>         Me.Panel1.Controls.Add(Me.myRbList2)
>
>         If Not (Me.Page.IsPostBack) Then
>             Me.myRbList2.Items.Clear()
>             Me.myRbList2.Items.Add(New ListItem("Blah20", "a"))
>             Me.myRbList2.Items.Add(New ListItem("Blah21", "b"))
>             Me.myRbList2.Items.Add(New ListItem("Blah22", "c"))
>             Me.myRbList2.Items.Add(New ListItem("Blah23", "d"))
>         End If
>     End Sub
>
>     Private Sub listSingleSelect_SelectedIndexChanged(ByVal sender As
> Object, ByVal e As System.EventArgs) Handles myRbList2.SelectedIndexChanged
>         Dim s As String = ""
>         s = Me.myRbList2.SelectedItem.Value
>     End Sub
> End Class
>
>
> I spent the good part of a day try to figure it out...no luck so far.
>
>
> Any help is appreciated.
>
>
> A few related links I found:
> http://groups-beta.google.com/group/microsoft.public.dotnet.framework.aspnet.webcontrols/browse_thread/thread/c35b3a3dc652a9d1/fba6137a7db09de9?lnk=gst&q=SelectedIndexChanged&rnum=2&hl=en#fba6137a7db09de9
>
> After reading this I made sure to a unique value for each item...no effect.
>
> http://groups-beta.google.com/group/microsoft.public.dotnet.framework.aspnet.webcontrols/browse_thread/thread/6df409cc1efe7c93/34cbb139c0264c52?lnk=gst&q=SelectedIndexChanged+radiobuttonlist&rnum=6&hl=en#34cbb139c0264c52
>
> Maybe has something to do with how I am adding the controls.?.
>
> Thank you,
> Martin Frank
Author
1 Nov 2006 8:58 AM
marss
Martin Frank написав:
>      Protected WithEvents myRbList2 As BosonRadioButton
>      Private Sub myInit()
>          Me.myRbList2 = New BosonRadioButton
>          Me.myRbList2.EnableViewState = True
>          Me.myRbList2.AutoPostBack = True
>          Me.myRbList2.Width = New Unit(100.0, UnitType.Percentage)
>          Me.Panel1.Controls.Add(Me.myRbList2)
>

You have to set ID for a dinamically added control to identify it
between postbacks.

Me.myRbList2.ID = "myRbList2";
Author
1 Nov 2006 2:29 PM
Martin Frank
Yes, I do have that bit of code in my ascx.  Sorry for not including it.

What did you mean "to identify it between postbacks"?
As of now, my code adds a new RadioButtonList in myInit() on every
postback.

Maybe this is a problem?
However it works just fine, using the same code, on an aspx page.

Whoa!  I just tried it with the default RadioButtonList control and the
same issue happens.  Is it the way I am adding the controls?

Thank you,
Martin Frank

marss wrote:
Show quoteHide quote
> Martin Frank написав:
>>      Protected WithEvents myRbList2 As BosonRadioButton
>>      Private Sub myInit()
>>          Me.myRbList2 = New BosonRadioButton
>>          Me.myRbList2.EnableViewState = True
>>          Me.myRbList2.AutoPostBack = True
>>          Me.myRbList2.Width = New Unit(100.0, UnitType.Percentage)
>>          Me.Panel1.Controls.Add(Me.myRbList2)
>>
>
> You have to set ID for a dinamically added control to identify it
> between postbacks.

> Me.myRbList2.ID = "myRbList2";
>