动态创建 rbl 时,单选按钮列表结果不起作用

发布于 2024-10-19 00:44:38 字数 2341 浏览 5 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妳是的陽光 2024-10-26 00:44:38

您已更改面板的内容并添加了一个表格,而不是使用面板直接添加RadioButtonLists。 FindControl 将仅查找面板的 NamingContainer,而不查找其子控件的 NamingContainer。搜索面板的控制集合也不起作用,因为 RBL 位于面板内的表内。因此,您必须循环 TableRows 才能获取 RBL。看一下:

For Each row As TableRow In Me.holidayQuestionnaireTable.Rows
     For Each cell As TableCell In row.Cells
         For Each ctrl As Control In cell.Controls
             If TypeOf ctrl Is RadioButtonList Then
                Dim rbl As RadioButtonList = DirectCast(ctrl, RadioButtonList)
                If rbl.SelectedIndex <> -1 AndAlso rbl.ID <> "question18" Then
                   Dim value As Int32 = Int32.Parse(rbl.SelectedValue)
                   total += value
                   rblCount += 1 'count only the selected RadiobuttonLists'
                End If
             End If
         Next
    Next
Next

如果您想使用 FindControl 方法,则必须使用每个 radioButtonList 的 NamingContainer,即 TableRow。所以这也可以工作,但是非常静态并且容易出错:

Dim question1 As RadioButtonList = DirectCast(Me.holidayQuestionnaireTable.Rows(0).FindControl("question1"), RadioButtonList)
Dim question2 As RadioButtonList = DirectCast(Me.holidayQuestionnaireTable.Rows(1).FindControl("question2"), RadioButtonList)
Dim question3 As RadioButtonList = DirectCast(Me.holidayQuestionnaireTable.Rows(2).FindControl("question3"), RadioButtonList)

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:

For Each row As TableRow In Me.holidayQuestionnaireTable.Rows
     For Each cell As TableCell In row.Cells
         For Each ctrl As Control In cell.Controls
             If TypeOf ctrl Is RadioButtonList Then
                Dim rbl As RadioButtonList = DirectCast(ctrl, RadioButtonList)
                If rbl.SelectedIndex <> -1 AndAlso rbl.ID <> "question18" Then
                   Dim value As Int32 = Int32.Parse(rbl.SelectedValue)
                   total += value
                   rblCount += 1 'count only the selected RadiobuttonLists'
                End If
             End If
         Next
    Next
Next

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:

Dim question1 As RadioButtonList = DirectCast(Me.holidayQuestionnaireTable.Rows(0).FindControl("question1"), RadioButtonList)
Dim question2 As RadioButtonList = DirectCast(Me.holidayQuestionnaireTable.Rows(1).FindControl("question2"), RadioButtonList)
Dim question3 As RadioButtonList = DirectCast(Me.holidayQuestionnaireTable.Rows(2).FindControl("question3"), RadioButtonList)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文