2014-10-03 44 views
0

我試圖改變formview中標籤中的文本。從理論上講,當用戶點擊按鈕時,這應該轉到SQL表中,提取數據,並將新文本輸入到名爲Description的標籤中。這是數據綁定,所以我懷疑這是我的問題的一部分,因爲我收到錯誤消息「對象引用未設置爲對象的實例」。更改formview數據綁定標籤文本

下面是ASP部分(抱歉,但因爲它是在ASP中輸入將不完全顯示):

TD類= 「style29」 合併單元格= 「4」 風格=「邊界:1px的固體#000000 「>強>描述:/強>

ASP:標籤ID =」 DescriptionLabel」 RUNAT = 「服務器」 文本= '<%#綁定( 「描述」)%>' 寬度= 「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

我不認爲你的'FindControl'工作正常。試試這個:

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

然後看看是否有效。

+0

事實證明,我是在formview「編輯」模式,並一直在尋找改變一個文本框,而不是一個標籤(新手哎呀)。我會繼續,並將其標記爲已解決,謝謝。 – 2014-10-04 15:18:54