2016-07-27 63 views
-1

在我的程序中,我有幾個加載動態字段的單選按鈕。當我選擇第一個單選按鈕時,所有的字段都被加載。但是,當我選擇另一個單選按鈕時,字段不會出現,並且出現錯誤「未將對象引用設置爲對象的實例」。我試圖清除佔位符,但它沒有奏效。我認爲這個問題與單選按鈕有關,因爲當我發表評論時,一切都很完美。但遺憾的是我需要這些按鈕。'對象引用未設置爲對象的實例'動態控件

我知道這個問題已經被問了很多次,我也試圖解決我的問題與不同的主題,但我不知道現在該做什麼...

下面是代碼

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 

末級

+1

而該線路上的,什麼是'Nothing' - 'PlaceHolder1','PlaceHolder1.Controls'或'DXTextbox'? – GSerg

+0

PlaceHolder1是PlaceHolders名稱,Placeholder1.Controls.Add允許我在佔位符上添加控件,而DXTextbox是文本框的名稱。基本上,這一行允許我添加文本框(爲每個文本框添加一個新的控件)。你在哪裏看到「沒有」? – georges619

+0

您可以告訴我們PlaceHolder1的模型嗎?當您嘗試向其添加對象時發生錯誤,但不能將對象添加到** Nothing **。 – PSo

回答

0

試試這個,我外面聲明新的實例,並嘗試你提到它的工作在第一時間重新使用該變量。

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) 
    Dim DXTextbox As ASPxTextBox 
    Dim DXLabel As ASPxLabel 
    DXTextbox = New ASPxTextBox 
    DXLabel = New ASPxLabel 
    Dim BXRadiobutton As ASPxRadioButtonList 
    Dim BXLabel As ASPxLabel 
    BXRadiobutton = New ASPxRadioButtonList 
    BXLabel = New ASPxLabel 
    Dim MXMemobox As ASPxMemo 
    Dim MXLabel As ASPxLabel 
    MXMemobox = New ASPxMemo 
    MXLabel = New ASPxLabel 

    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"      
       DXTextbox.ID = NewField.FieldName & "_field" 
       DXTextbox.Width = NewField.Width 
       DXTextbox.Height = NewField.Height 
       DXTextbox.TabIndex = NewField.TabIndex 
       DXTextbox.CssClass = "Field" 
       PlaceHolder1.Controls.Add(DXTextbox) 
       DXLabel.ID = NewField.FieldName.ToString & "_lbl" 
       DXLabel.Text = NewField.lblText 
       DXLabel.CssClass = "TextBox" 
       lblText.Controls.Add(DXLabel) 
       DXTextbox.Controls.Add(DXLabel) 

      Case "RadioButton"      
       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.ID = NewField.FieldName.ToString & "_lbl" 
       BXLabel.Text = NewField.lblText 
       BXLabel.CssClass = "RadioButton" 
       lblText.Controls.Add(BXLabel) 
       BXRadiobutton.Controls.Add(BXLabel) 

      Case "MemoBox"     
       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.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 

末次

+0

它不起作用:/。它幾乎是做同樣的事情(當我點擊第一個單選按鈕時),但有幾個領域缺失。當我選擇另一個按鈕,錯誤再次出現... – georges619

+0

到現在爲止,說實話我沒有線索你的問題在哪裏,錯誤味精告訴我們,在你的行** PlaceHolder1.Controls.Add(DXTextbox) **,有一件事尚未初始化,這是**沒有** – PSo

相關問題