'对象引用未设置为对象的实例'动态控件

时间:2016-07-27 08:01:33

标签: vb.net

在我的程序中,我有几个加载动态字段的单选按钮。当我选择第一个radiobutton时,所有字段都被加载。但是当我选择另一个单选按钮时,字段不会出现,我收到错误“对象引用未设置为对象的实例”。我试图清除占位符,但它没有用。我认为这个问题与radiobuttons有关,因为当我评论它时,一切都很完美。但遗憾的是我需要这些按钮。

我知道这个问题已被多次询问,我也尝试用不同的主题解决我的问题,但我现在不知道该做什么......

以下是代码

 Protected Sub ServiceTypeList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ServiceTypeList.SelectedIndexChanged
        Dim tmpservicetype As New servicetype(ServiceTypeList.SelectedItem.Value)
        Dim tmpFieldName As String

    Try
        Dim cmdstr As String
        PlaceHolder1.Controls.Clear()
        lblText.Controls.Clear()

        cmdstr = "SELECT servicetype, FieldName, FieldType, FieldLength, FieldLabelEmail, ToolboxType, Height, Width, TabIndex FROM tblDynamicControl WHERE (servicetype = '" & tmpservicetype.servicetype & "') ORDER BY TabIndex"
        Dim cmd As New SqlCommand(cmdstr)

        Dim dt As DataTable = EMGEHelpdesk.SharedCode.ExecuteCMD(cmd).Tables(0)
        For i = 0 To dt.Rows.Count - 1
            Dim dr As DataRow = dt.Rows(i)
            tmpFieldName = SetDataVar(dr, "FieldName")
            Dim NewField As New DynamicControl(tmpservicetype.servicetype, tmpFieldName, "GB")

            Select Case NewField.ToolboxType
                Case "TextBox"
                    Dim DXTextbox As ASPxTextBox
                    Dim DXLabel As ASPxLabel
                    DXTextbox = New ASPxTextBox
                    DXTextbox.ID = NewField.FieldName & "_field"
                    DXTextbox.Width = NewField.Width
                    DXTextbox.Height = NewField.Height
                    DXTextbox.TabIndex = NewField.TabIndex
                    DXTextbox.CssClass = "Field"
                    PlaceHolder1.Controls.Add(DXTextbox)        --> **The error appears here**
                    DXLabel = New ASPxLabel
                    DXLabel.ID = NewField.FieldName.ToString & "_lbl"
                    DXLabel.Text = NewField.lblText
                    DXLabel.CssClass = "TextBox"
                    lblText.Controls.Add(DXLabel)
                    DXTextbox.Controls.Add(DXLabel)

                Case "RadioButton"
                    Dim BXRadiobutton As ASPxRadioButtonList
                    Dim BXLabel As ASPxLabel
                    BXRadiobutton = New ASPxRadioButtonList
                    BXRadiobutton.DataSource = Split(NewField.FieldName)
                    BXRadiobutton.DataBind()
                    BXRadiobutton.RepeatDirection = RepeatDirection.Horizontal
                    BXRadiobutton.ID = NewField.FieldName & "_field"
                    BXRadiobutton.TabIndex = NewField.TabIndex
                    BXRadiobutton.CssClass = "Button"
                    PlaceHolder1.Controls.Add(BXRadiobutton)
                    BXLabel = New ASPxLabel
                    BXLabel.ID = NewField.FieldName.ToString & "_lbl"
                    BXLabel.Text = NewField.lblText
                    BXLabel.CssClass = "RadioButton"
                    lblText.Controls.Add(BXLabel)
                    BXRadiobutton.Controls.Add(BXLabel)

                Case "MemoBox"
                    Dim MXMemobox As ASPxMemo
                    Dim MXLabel As ASPxLabel
                    MXMemobox = New ASPxMemo
                    MXMemobox.ID = NewField.FieldName & "_field"
                    MXMemobox.Width = NewField.Width
                    MXMemobox.Height = NewField.Height
                    MXMemobox.TabIndex = NewField.TabIndex
                    MXMemobox.CssClass = "Field"
                    PlaceHolder1.Controls.Add(MXMemobox)
                    MXLabel = New ASPxLabel
                    MXLabel.ID = NewField.FieldName.ToString & "_lbl"
                    MXLabel.Text = NewField.lblText
                    MXLabel.CssClass = "MemoBox"
                    lblText.Controls.Add(MXLabel)
                    MXMemobox.Controls.Add(MXLabel)
            End Select

        Next


    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub
End Class

1 个答案:

答案 0 :(得分:0)

试试这个。我在外部声明了新实例,并尝试重用您提到的变量。它第一次起作用。

WebBrowser