我试图更改formview中标签中的文字。理论上,当用户单击按钮时,这应该转到SQL表,提取数据,并将新文本输入到名为Description的标签中。这是数据绑定所以我怀疑这是我的问题的一部分,因为我收到错误消息"对象引用未设置为对象的实例。"
下面是asp部分(对不起,但它不会像在asp中输入的那样完全显示):
td class =" style29"列跨度=" 4" style =" border:1px solid#000000"> strong>说明:/ strong>
asp:标签ID =" DescriptionLabel" RUNAT ="服务器" 文字='<%#Bind("说明")%>'宽度=" 1000像素"
下面是VB代码:
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim conCString1 As String = ConfigurationManager.ConnectionStrings("conCString1").ConnectionString
Dim sqlConnection1 As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conCString1").ConnectionString)
Dim cmd1 As New SqlCommand
cmd1.CommandType = CommandType.Text
cmd1.Connection = sqlConnection1
Dim querystring1 As String = "SELECT [Rule] FROM [BuildRules] WHERE ([Table] = N'Capacitors') AND (Field = N'Description')"
sqlConnection1.Open()
Dim command2 As New SqlCommand(querystring1, sqlConnection1)
Dim reader2 As SqlDataReader = command2.ExecuteReader()
Dim lblD As Label = FormView1.FindControl("DescriptionLabel")
While reader2.Read()
'below is test line only
lblD.Text = reader2(0) 'Example: 'CAP', Value, Dielectric Type, Package Size, Rated Voltage, Tolerance, Temperature Coefficient
End While
sqlConnection1.Close()
End Sub
答案 0 :(得分:1)
我不认为你的'FindControl'工作正常。试试这个:
Dim lblD As Label = DirectCast(FindName("DescriptionLabel"), Label)
看看是否有效。