Reputation: 2861
Developed a slightly dynamic page that builds a Question and Answer page based on information from a database. Everything works great except i cant get the RadioButtonList event to trigger with a minor MsgBox response to validate object sender and selected ListItem information.
If Not Page.IsPostBack Then
With Globals.tst
.GetBrandInformation(Page.RouteData.Values("brand"),
Page.RouteData.Values("year"),
Page.RouteData.Values("month"))
'Load up Question DataSet
For Each quest As Question In .Questions
Dim tr As New HtmlControls.HtmlTableRow
Dim td As New HtmlControls.HtmlTableCell
td.Attributes.Add("class", "tdQ") 'Add class attribute to <td> element, creating 'class="tdQ"'
Dim lbl As New Label
lbl.ID = "lbl" & quest.ID
lbl.Text = quest.Text
td.Controls.Add(lbl)
tr.Cells.Add(td)
tblContent.Rows.Add(tr)
tr = New HtmlControls.HtmlTableRow
td = New HtmlControls.HtmlTableCell
td.Attributes.Add("class", "tdA") 'Add class attribute to <td> element, creating 'class="tdA"'
Dim rbl As New RadioButtonList
rbl.ID = "rblT" & .ID & "_Q" & quest.ID
AddHandler rbl.SelectedIndexChanged, AddressOf rbl_SelectedIndexChanged 'Attach generic event handler to control
Dim li As New ListItem
'Load up Answer Dataset
For Each answ As Answer In quest.Answers
li.Text = answ.Value
li.Value = "T" & .ID & "-Q" & quest.ID & "-A" & answ.ID & "-C" & answ.Correct
'Add built ListItem to RadioButtonList
rbl.Items.Add(li)
li = New ListItem
Next
td.Controls.Add(rbl)
tr.Cells.Add(td)
tblContent.Rows.Add(tr)
Next
End With
'
End If
Below is the Generic Event Handler Logic i am trying to attach to the RadioButtonList(s).
Protected Sub rbl_SelectedIndexChanged(sender As Object, e As EventArgs)
MsgBox(CType(sender, RadioButtonList).ID & " Clicked.")
MsgBox("Radio Button Selected: " & CType(sender, RadioButtonList).ID & " is Correct? " & CType(sender, RadioButtonList).SelectedValue)
End Sub
Anyone see anything wrong with the design, or know why the Event is not being triggered?
Ok changed the rbl
object to do AutoPostBack = true
but that made life even more unbearable ;) Then quickly reverted back as the Q&A list i have is randomly generated on the Questions and Answers so the end-user would be given a new random order of questions upon every click of the ListItem.
Although i am getting some response back from the call backs still not getting the JavaScript response i am expecting by using either a defined JavaScript file or inline Response.Write
/ClientScript.Register
.
Created a debugging method into the Content Page:
Public Shared Sub Show(msg As String, Optional pg As Page = Nothing)
Dim cmsg As String = msg.Replace("'", "\'")
Dim scr As String = "<script type=""text/javascript"">alert('" & cmsg & "');</script>"
If pg Is Nothing Then
pg = CType(HttpContext.Current.CurrentHandler, Page)
End If
If (pg IsNot Nothing) And Not (pg.ClientScript.IsClientScriptBlockRegistered("alert")) Then
pg.ClientScript.RegisterClientScriptBlock(GetType(Alert), "alert", scr)
End If
End Sub
On page Load this method fires and as expected, but when placed inside the SelectedIndexChanged event it never fires off.
Still perplexed on why it works on general execution but not triggered execution.
Userful Method for those that like to create .Net methods that implement Javascript. .Net Slave - Javascript Alert.Show Class
Upvotes: 0
Views: 942
Reputation: 30727
You have to take it OUT of the If Not Page.IsPostBack Then
this is because when you DO postback, the...
AddHandler rbl.SelectedIndexChanged, AddressOf rbl_SelectedIndexChanged
...needs to be rebound before it can fire.
So basically, remove the If Not Page.IsPostBack Then
and End If
.
Oh, and get rid of the MsgBox()
code too - this will kill the page from doing anything. If you want to output an Alert()
then do something like:
Dim script As String = String.Format("alert('{0}');", ""Radio Button Selected: " & CType(sender, RadioButtonList).ID & " is Correct? " & CType(sender, RadioButtonList).SelectedValue")
page.ClientScript.RegisterClientScriptBlock(page.[GetType](), "alert", script, True)
UPDATE:
you also want to set AutoPostback = true;
on your RBL. This tells the page to postback as soon as it's changed. the handler only fires if AutoPostBack
is true.
Upvotes: 1