Radiobuttonlist (a custom control) selectedindexchanged event notfiring in User Control

M

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...tedIndexChanged&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...radiobuttonlist&rnum=6&hl=en#34cbb139c0264c52
Maybe has something to do with how I am adding the controls.?.

Thank you,
Martin Frank
 
M

Martin Frank

If anyone is wondering...
As BosonRadioButton should say As MyCustomRbList

Thanks.
 
M

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";
 
M

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
 

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,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top