动态创建 rbl 时,单选按钮列表结果不起作用
我有 20 个动态创建的单选按钮列表 - 然后在提交表单时声明。
我还有一些代码,用于计算已回答问题的数量和已回答问题的总价值。 - 当单选按钮列表被硬编码到页面中时,此代码曾经可以工作,但现在不行了。 - 我正在将已回答的问题数和所有答案的总价值写入页面,但它们返回为 0。
有人能明白为什么现在单选按钮列表是动态创建的,这可能不起作用。?
背后代码:
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
For i As Integer = 1 To 20
Dim TableRow As New TableRow()
Dim TableRowCell_1 As New TableCell()
TableRow.Cells.Add(TableRowCell_1)
holidayQuestionnaireTable.Rows.Add(TableRow)
Dim question As New RadioButtonList
question.ID = "question" & i
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 as Object, e as EventArgs)
Dim question1 As RadioButtonList = DirectCast(Page.FindControl("question1"), RadioButtonList)
Dim question2 As RadioButtonList = DirectCast(Page.FindControl("question2"), RadioButtonList)
Dim question3 ...
...
Dim question19 As RadioButtonList = DirectCast(Page.FindControl("question19"), RadioButtonList)
Dim question20 As RadioButtonList = DirectCast(Page.FindControl("question20"), RadioButtonList)
Dim rblCount As Double
Dim total As Double
Dim avg As Double
For Each ctrl As UI.Control In Me.myPanel.Controls
If TypeOf ctrl Is RadioButtonList Then
Dim rbl As RadioButtonList = DirectCast(ctrl, RadioButtonList)
If rbl.SelectedIndex > -1 And not rbl.ID = "question18" Then
Dim value As Double = Double.Parse(rbl.SelectedValue)
total += value
rblCount += 1
End If
End If
Next
Response.Write(rblCount & " - " & total & " - " & (total / rblCount))
End Sub
正文:
<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>
I have 20 radiobuttonlists which are dynamically created - then declared when a form is submitted.
I also have some code which totals the number of answered questions and the total value of the answered questions. - this code used to work when the radiobuttonlists were hard coded into the page, but it now does not. - I am writing the number of questions answered and the total value of all answers to the page but they come back as 0.
Can anyone see why this might not work now that the radiobuttonlists are dynamically created.?
Code behind:
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
For i As Integer = 1 To 20
Dim TableRow As New TableRow()
Dim TableRowCell_1 As New TableCell()
TableRow.Cells.Add(TableRowCell_1)
holidayQuestionnaireTable.Rows.Add(TableRow)
Dim question As New RadioButtonList
question.ID = "question" & i
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 as Object, e as EventArgs)
Dim question1 As RadioButtonList = DirectCast(Page.FindControl("question1"), RadioButtonList)
Dim question2 As RadioButtonList = DirectCast(Page.FindControl("question2"), RadioButtonList)
Dim question3 ...
...
Dim question19 As RadioButtonList = DirectCast(Page.FindControl("question19"), RadioButtonList)
Dim question20 As RadioButtonList = DirectCast(Page.FindControl("question20"), RadioButtonList)
Dim rblCount As Double
Dim total As Double
Dim avg As Double
For Each ctrl As UI.Control In Me.myPanel.Controls
If TypeOf ctrl Is RadioButtonList Then
Dim rbl As RadioButtonList = DirectCast(ctrl, RadioButtonList)
If rbl.SelectedIndex > -1 And not rbl.ID = "question18" Then
Dim value As 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已更改面板的内容并添加了一个表格,而不是使用面板直接添加RadioButtonLists。 FindControl 将仅查找面板的 NamingContainer,而不查找其子控件的 NamingContainer。搜索面板的控制集合也不起作用,因为 RBL 位于面板内的表内。因此,您必须循环 TableRows 才能获取 RBL。看一下:
如果您想使用 FindControl 方法,则必须使用每个 radioButtonList 的 NamingContainer,即 TableRow。所以这也可以工作,但是非常静态并且容易出错:
You have changed the content of your panel and added a Table instead of using the Panel to add the RadioButtonLists directly. FindControl will only look into the NamingContainer of the Panel and not of its child controls' NamingContainer. Searching through the control-collection of the Panel does also not work because the RBL's are inside of the Table that is inside of the Panel. Therefore you have to loop the TableRows to get the RBL's. Have a look:
If you want to use the FindControl-approach, you have to use the NamingContainer of each radioButtonList and that is the TableRow. So this would also work, but is very static and error-prone: