簡體   English   中英

更改formview數據綁定標簽文本

[英]change formview databound label text

我正在嘗試更改formview內標簽中的文本。 理論上,當用戶單擊按鈕時,這應該轉到SQL表,提取數據,然后將新文本輸入到名為Description的標簽中。 這是數據綁定的,因此當我收到錯誤消息“對象引用未設置為對象的實例”時,我懷疑這是問題的一部分。

下面是asp部分(很抱歉,但不會完全顯示在asp中輸入的內容):

td class =“ style29” colspan =“ 4” style =“ border:1px solid#000000”> strong>說明:/ strong>

asp:標簽ID =“ DescriptionLabel” runat =“服務器” Text ='<%#Bind(“ Description”)%>'Width =“ 1000px”

以下是背后的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

我認為您的“ FindControl”工作不正常。 嘗試這個:

  Dim lblD As Label = DirectCast(FindName("DescriptionLabel"), Label)

看看是否可行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM