当rbl动态创建时,Radiobuttonlist结果不起作用

时间:2011-02-24 09:59:38

标签: asp.net vb.net radiobuttonlist

我有20个动态创建的radiobuttonlists - 然后在提交表单时声明。

我还有一些代码,总计已回答问题的数量和已回答问题的总价值。 - 这个代码用于将radiobuttonlists硬编码到页面中,但现在却没有。 - 我正在写回答问题的数量和页面所有答案的总值,但它们会回来为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>

1 个答案:

答案 0 :(得分:1)

您更改了面板的内容并添加了一个表,而不是使用Panel直接添加RadioButtonLists。 FindControl只会查看Panel的NamingContainer而不是其子控件的NamingContainer。搜索Panel的控件集合也不起作用,因为RBL位于Panel内部的Table内部。因此,您必须循环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)