asp.net - Radiobuttonlist results not working when rbl's dynamically created -
i have 20 radiobuttonlists dynamically created - declared when form submitted.
i have code totals number of answered questions , total value of answered questions. - code used work when radiobuttonlists hard coded page, not. - writing number of questions answered , total value of answers page come 0.
can see why might not work radiobuttonlists dynamically created.?
code behind:
sub page_load(byval sender object, byval e eventargs) integer = 1 20 dim tablerow new tablerow() dim tablerowcell_1 new tablecell() tablerow.cells.add(tablerowcell_1) holidayquestionnairetable.rows.add(tablerow) dim question new radiobuttonlist question.id = "question" & question.items.insert(0, new listitem("", "1")) question.items.insert(1, new listitem("", "2")) tablerowcell_1.controls.add(question) next end sub
...
sub btnsendfeedback_click(sender object, e eventargs) dim question1 radiobuttonlist = directcast(page.findcontrol("question1"), radiobuttonlist) dim question2 radiobuttonlist = directcast(page.findcontrol("question2"), radiobuttonlist) dim question3 ... ... dim question19 radiobuttonlist = directcast(page.findcontrol("question19"), radiobuttonlist) dim question20 radiobuttonlist = directcast(page.findcontrol("question20"), radiobuttonlist) dim rblcount double dim total double dim avg double each ctrl ui.control in me.mypanel.controls if typeof ctrl radiobuttonlist dim rbl radiobuttonlist = directcast(ctrl, radiobuttonlist) if rbl.selectedindex > -1 , not rbl.id = "question18" dim value double = double.parse(rbl.selectedvalue) total += value rblcount += 1 end if end if next response.write(rblcount & " - " & total & " - " & (total / rblcount)) end sub
body:
<asp:placeholder id="mypanel" runat="server"> <asp:table runat="server" cellpadding="0" cellspacing="0" gridlines="none" horizontalalign="center" cssclass="ratingtable" id="holidayquestionnairetable" /> <asp:button onclick="btnsendfeedback_click" runat="server" text="submit..." id="submitbutton" /> </asp:placeholder>
you have changed content of panel , added table instead of using panel add radiobuttonlists directly. findcontrol namingcontainer of panel , not of child controls' namingcontainer. searching through control-collection of panel not work because rbl's inside of table inside of panel. therefore have loop tablerows rbl's. have look:
for each row tablerow in me.holidayquestionnairetable.rows each cell tablecell in row.cells each ctrl control in cell.controls if typeof ctrl radiobuttonlist dim rbl radiobuttonlist = directcast(ctrl, radiobuttonlist) if rbl.selectedindex <> -1 andalso rbl.id <> "question18" dim value int32 = int32.parse(rbl.selectedvalue) total += value rblcount += 1 'count selected radiobuttonlists' end if end if next next next
if want use findcontrol-approach, have use namingcontainer of each radiobuttonlist , tablerow. work, static , error-prone:
dim question1 radiobuttonlist = directcast(me.holidayquestionnairetable.rows(0).findcontrol("question1"), radiobuttonlist) dim question2 radiobuttonlist = directcast(me.holidayquestionnairetable.rows(1).findcontrol("question2"), radiobuttonlist) dim question3 radiobuttonlist = directcast(me.holidayquestionnairetable.rows(2).findcontrol("question3"), radiobuttonlist)
Comments
Post a Comment