当我在 VB.NET 中执行我的代码时,我得到'对象引用未设置为对象的实例。vb.net'。
我有一个包含许多项目的下拉列表。在一项上,代码执行没有任何问题。但是,在任何其他项目上,我都会收到错误消息。
例如,对于 ddlItemCat.SelectedValue = "Mobile/Smart Phone & Accessories",代码正确执行,但对于 ddlItemCat.SelectedValue = "Mobile Computing & Accessories",您将收到错误消息。
Function validateInput() As Boolean
Dim validInput As Boolean = True
Dim backColour As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#F078A0")
Dim backColourReset As System.Drawing.Color = Drawing.Color.White
Dim errorList As String = "<span id=""errlstHdr"" style=""font-size: medium; font-weight: bold; text-align: left"">The following items must be corrected before the IT Shop Form data can be saved<span/><br /><br /><span id=""errlstDtl"" style=""font-size: small; font-weight: normal;text-align: left"">"
Dim i As Integer
Dim fieldName As String
Dim tb As TextBox
tbSurname.BackColor = backColourReset
tbFirstname.BackColor = backColourReset
tbPosition.BackColor = backColourReset
tbDept.BackColor = backColourReset
ddlCampus.BackColor = backColourReset
tbBuilding.BackColor = backColourReset
tbFlr.BackColor = backColourReset
tbTelPager.BackColor = backColourReset
ddlItemCat.BackColor = backColourReset
msgText.Text = ""
If chkTxt(tbSurname.Text) = False Then
tbSurname.BackColor = backColour
errorList += Chr(149) + "Contact Surname not entered<br />"
validInput = False
End If
If chkTxt(tbFirstname.Text) = False Then
tbFirstname.BackColor = backColour
errorList += Chr(149) + "Contact First Name not entered<br />"
validInput = False
End If
If chkTxt(tbPosition.Text) = False Then
tbPosition.BackColor = backColour
errorList += Chr(149) + "Contact Position not entered<br />"
validInput = False
End If
If chkTxt(tbDept.Text) = False Then
tbDept.BackColor = backColour
errorList += Chr(149) + "Contact Department not entered<br />"
validInput = False
End If
If chkTxt(ddlCampus.SelectedValue) = False Then
ddlCampus.BackColor = backColour
errorList += Chr(149) + "Contact Campus not entered<br />"
validInput = False
End If
If chkTxt(tbBuilding.Text) = False Then
tbBuilding.BackColor = backColour
errorList += Chr(149) + "Contact Building not entered<br />"
validInput = False
End If
If chkTxt(tbFlr.Text) = False Then
tbFlr.BackColor = backColour
errorList += Chr(149) + "Contact Floor not entered<br />"
validInput = False
End If
If chkTxt(tbTelPager.Text) = False Then
tbTelPager.BackColor = backColour
errorList += Chr(149) + "Contact Phone or Pager not entered<br />"
validInput = False
End If
If chkTxt(ddlItemCat.SelectedValue) = False Then
ddlItemCat.BackColor = backColour
errorList += Chr(149) + "Item Category not selected<br />"
validInput = False
End If
If ddlItemCat.SelectedValue = "Mobile/Smart Phone & Accessories" Then
If chkTxt(ddlNewService3.SelectedValue) = False Then
ddlNewService3.BackColor = backColour
errorList += Chr(149) + "New Service not selected<br />"
validInput = False
End If
For i = 1 To ddlQty3.SelectedValue
If ddlNewService3.SelectedValue = "No" Then
fieldName = "tbMobNo3" + i.ToString
tb = TryCast(FindControl(fieldName), TextBox)
If Not IsNothing(tb) And chkTxt(tb.Text) = False Then
tb.BackColor = backColour
errorList += Chr(149) + "Service Mobile Number not entered<br />"
validInput = False
End If
End If
fieldName = "tbSurname3" + i.ToString
tb = TryCast(FindControl(fieldName), TextBox)
If Not IsNothing(tb) And chkTxt(tb.Text) = False Then
tb.BackColor = backColour
errorList += Chr(149) + "User Surname not entered<br />"
validInput = False
End If
fieldName = "tbFirstname3" + i.ToString
tb = TryCast(FindControl(fieldName), TextBox)
If Not IsNothing(tb) And chkTxt(tb.Text) = False Then
tb.BackColor = backColour
errorList += Chr(149) + "User First Name not entered<br />"
validInput = False
End If
Next i
End If
If ddlItemCat.SelectedValue = "Mobile Computing & Accessories" Then
If chkTxt(ddlOldDevice4.SelectedValue) = False Then
ddlOldDevice4.BackColor = backColour
errorList += Chr(149) + "New Service not selected<br />"
validInput = False
End If
For i = 1 To tbQty4.SelectedValue
If ddlOldDevice4.SelectedValue = "Yes" Then
fieldName = "tbTelPager4" + i.ToString
tb = TryCast(FindControl(fieldName), TextBox)
‘THIS IS WHERE THE ERROR OCCURS ON NEXT LINE
If Not IsNothing(tb) And chkTxt(tb.Text) = False Then
tb.BackColor = backColour
errorList += Chr(149) + "Service Mobile Number not entered<br />"
validInput = False
End If
End If
fieldName = "tbFirstname4" + i.ToString
tb = TryCast(FindControl(fieldName), TextBox)
If Not IsNothing(tb) And chkTxt(tb.Text) = False Then
tb.BackColor = backColour
errorList += Chr(149) + "User First Name not entered<br />"
validInput = False
End If
fieldName = "tbSurname4" + i.ToString
tb = TryCast(FindControl(fieldName), TextBox)
If Not IsNothing(tb) And chkTxt(tb.Text) = False Then
tb.BackColor = backColour
errorList += Chr(149) + "User Surname not entered<br />"
validInput = False
End If
Next i
End If
msgText.Text = errorList + "<span/>"
Return validInput
End Function
请帮忙
这是为字段生成单元格的代码:
Sub addRowsToMobComputingTable()
Dim tbChk As TextBox
Dim fieldName As String
Dim rowCount As Integer = tbQty4.SelectedValue
If rowCount = 1 Then ' additional rows are not required
Return
End If
For i = 2 To rowCount
' check to ensure field doesn't exist
fieldName = "tbSurname4" + i.ToString
tbChk = TryCast(FindControl(fieldName), TextBox)
If IsNothing(tbChk) Then
For j = 0 To 10
Dim tempRow As New TableRow
Dim tempCell1 As New TableCell
Dim tempCell2 As New TableCell
Dim tempCell3 As New TableCell
Dim tb As New TextBox
Dim lbl As New Label
Try
Select Case j
Case 0
tempCell1.Text = ""
tempCell1.CssClass = "style8"
tempRow.Cells.Add(tempCell1)
tempCell2.Text = "-----------------"
tempCell2.CssClass = "style8"
tempRow.Cells.Add(tempCell2)
tempCell3.Text = ""
tempCell3.CssClass = "style8"
tempRow.Cells.Add(tempCell3)
tblMobComp.Rows.Add(tempRow)
Case 1
tempCell1.Text = ""
tempRow.Cells.Add(tempCell1)
tempCell2.Text = "Surname"
tempCell2.CssClass = "style8"
tempRow.Cells.Add(tempCell2)
tb.Width = 144
tb.ID = "tbSurname4" + i.ToString
tempCell3.Controls.Add(tb)
tempRow.Cells.Add(tempCell3)
tblMobComp.Rows.Add(tempRow)
Case 2
tempCell1.Text = ""
tempRow.Cells.Add(tempCell1)
tempCell2.Text = "First Name"
tempCell2.CssClass = "style8"
tempRow.Cells.Add(tempCell2)
tb.Width = 142
tb.ID = "tbFirstname4" + i.ToString
tempCell3.Controls.Add(tb)
tempRow.Cells.Add(tempCell3)
tblMobComp.Rows.Add(tempRow)
Case 3
tempCell1.Text = ""
tempRow.Cells.Add(tempCell1)
tempCell2.Text = "Contact No."
tempCell2.CssClass = "style8"
tempRow.Cells.Add(tempCell2)
tb.Width = 186
tb.ID = "tbTelPager4" + i.ToString
tempCell3.Controls.Add(tb)
tempRow.Cells.Add(tempCell3)
tblMobComp.Rows.Add(tempRow)
Case 4
tempCell1.Text = ""
tempCell1.CssClass = "style8"
tempRow.Cells.Add(tempCell1)
tempCell2.Text = "Position"
tempCell2.CssClass = "style8"
tempRow.Cells.Add(tempCell2)
tb.Width = 295
tb.ID = "tbPosition4" + i.ToString
tempCell3.Controls.Add(tb)
tempRow.Cells.Add(tempCell3)
tblMobComp.Rows.Add(tempRow)
Case 5
tempCell1.Text = ""
tempRow.Cells.Add(tempCell1)
tempCell2.Text = "Department"
tempCell2.CssClass = "style8"
tempRow.Cells.Add(tempCell2)
tb.Width = 252
tb.ID = "tbDept4" + i.ToString
tempCell3.Controls.Add(tb)
tempRow.Cells.Add(tempCell3)
tblMobComp.Rows.Add(tempRow)
Case 6
tempCell1.Text = ""
tempRow.Cells.Add(tempCell1)
tempCell2.Text = "Campus"
tempCell2.CssClass = "style8"
tempRow.Cells.Add(tempCell2)
tb.Width = 252
tb.ID = "ddlCampus4" + i.ToString
tempCell3.Controls.Add(tb)
tempRow.Cells.Add(tempCell3)
tblMobComp.Rows.Add(tempRow)
Case 7
tempCell1.Text = ""
tempRow.Cells.Add(tempCell1)
tempCell2.Text = "Building"
tempCell2.CssClass = "style8"
tempRow.Cells.Add(tempCell2)
tb.Width = 252
tb.ID = "tbBuilding4" + i.ToString
tempCell3.Controls.Add(tb)
tempRow.Cells.Add(tempCell3)
tblMobComp.Rows.Add(tempRow)
Case 8
tempCell1.Text = ""
tempRow.Cells.Add(tempCell1)
tempCell2.Text = "Floor"
tempCell2.CssClass = "style8"
tempRow.Cells.Add(tempCell2)
tb.Width = 252
tb.ID = "tbFlr4" + i.ToString
tempCell3.Controls.Add(tb)
tempRow.Cells.Add(tempCell3)
tblMobComp.Rows.Add(tempRow)
Case 9
tempCell1.Text = ""
tempRow.Cells.Add(tempCell1)
lbl.ID = "lblAsset4" + i.ToString
lbl.Text = "Asset No. of old device"
If ddlOldDevice4.SelectedValue = "Yes" Then
lbl.Visible = True
Else
lbl.Visible = False
End If
tempCell2.CssClass = "style8"
tempCell2.Controls.Add(lbl)
tempRow.Cells.Add(tempCell2)
tb.Width = 144
If ddlOldDevice4.SelectedValue = "Yes" Then
tb.Visible = True
Else
tb.Visible = False
End If
tb.ID = "tbAsset4" + i.ToString
tempCell3.Controls.Add(tb)
tempRow.Cells.Add(tempCell3)
tblMobComp.Rows.Add(tempRow)
Case 10
tempCell1.Text = ""
tempRow.Cells.Add(tempCell1)
tempCell2.Text = "Additional comments"
tempCell2.CssClass = "style8"
tempRow.Cells.Add(tempCell2)
tb.Width = 340
tb.Rows = 4
tb.TextMode = TextBoxMode.MultiLine
tb.ID = "tbComments4" + i.ToString
tempCell3.Controls.Add(tb)
tempRow.Cells.Add(tempCell3)
tblMobComp.Rows.Add(tempRow)
End Select
Catch
End Try
Next j
End If
Next i
End Sub
上面的代码通过“tbQty4”下拉列表控件通过 ASP 表执行:
<div id="Mobile Computing">
<asp:Panel ID="pnlMobComp" runat="server" Visible="False">
<span class="style5">Mobile Computing</span><br />
<br />
<asp:Table ID="tblMobComp" runat="server"
Style="width: 46%;" ViewStateMode="Enabled">
<asp:TableRow>
<asp:TableCell CssClass="style8">
</asp:TableCell>
<asp:TableCell CssClass="style4">
Quantity required?</asp:TableCell>
<asp:TableCell>
<asp:DropDownList ID="tbQty4" runat="server" OnSelectedIndexChanged="addRowsToMobComputingTable"
AutoPostBack="True">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>13</asp:ListItem>
<asp:ListItem>14</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
<asp:ListItem>16</asp:ListItem>
<asp:ListItem>17</asp:ListItem>
<asp:ListItem>18</asp:ListItem>
<asp:ListItem>19</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="style8">
</asp:TableCell>
<asp:TableCell CssClass="style4">
Replacing an old device?</asp:TableCell>
<asp:TableCell>
<asp:DropDownList ID="ddlOldDevice4" runat="server" AutoPostBack="True" CssClass="style2"
OnSelectedIndexChanged="ddlOldDevice4_SelectedIndexChanged">
<asp:ListItem><--- Select ---></asp:ListItem>
<asp:ListItem>No</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="style8">
</asp:TableCell>
<asp:TableCell CssClass="style4">
Who will be using this device?</asp:TableCell>
<asp:TableCell>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="style8">
</asp:TableCell>
<asp:TableCell CssClass="style4">
Surname</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="tbSurname4" runat="server" Width="144px"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="style8">
</asp:TableCell>
<asp:TableCell CssClass="style4">
First Name</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="tbFirstname4" runat="server" Width="142px"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="style8">
</asp:TableCell>
<asp:TableCell CssClass="style4">
Position</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="tbPosition4" runat="server" Width="295px"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="style8">
</asp:TableCell>
<asp:TableCell CssClass="style4">
Department</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="tbDept4" runat="server" CssClass="style2" Width="252px"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="style8">
</asp:TableCell>
<asp:TableCell CssClass="style4">
Campus</asp:TableCell>
<asp:TableCell>
<asp:DropDownList ID="ddlCampus4" runat="server" CssClass="style2">
<asp:ListItem>Austin</asp:ListItem>
<asp:ListItem>Repat</asp:ListItem>
<asp:ListItem>Talbot</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="style8">
</asp:TableCell>
<asp:TableCell CssClass="style4">
Building</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="tbBuilding4" runat="server" CssClass="style2" Width="307px"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="style8">
</asp:TableCell>
<asp:TableCell CssClass="style4">
Floor</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="tbFlr4" runat="server" CssClass="style2"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="style8">
</asp:TableCell>
<asp:TableCell CssClass="style4">
Contact No.</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="tbTelPager4" runat="server" CssClass="style2" Width="186px"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="style8">
</asp:TableCell>
<asp:TableCell CssClass="style4">
<asp:Label ID="lblAsset4" runat="server" Text="Asset No. of old device"></asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="tbAsset4" runat="server" Width="144px"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="style9">
</asp:TableCell>
<asp:TableCell CssClass="style12">
Additional comments</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="tbComments4" runat="server" Width="340px" Rows="4" TextMode="MultiLine"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<table style="width: 46%;">
<tr>
<td>
</td>
</tr>
</table>
</asp:Panel>
</div>