2013-03-21 65 views
0

我到處搜索瞭解這個過程,但沒有任何工作到目前爲止。如何鏈接下拉列表到各種文本框vb.net

基本上我想要的是將我的下拉列表ddlreportid鏈接到各種文本框。

目前,下拉列表連接,數據源在那裏使用的報告表顯示reportid的如1,2,3列表等

我希望發生的是,如果用戶從ddlreportid中點擊,例如1。我希望將reportname放入txtreportname.text文本框中,將reportaddress放置到txtreport地址文本框中,並將reportpostcode放入txtreportpostcodec文本框中。

我從哪裏開始?如果soemone可以指導我,那會很棒。

我以前使用過這個代碼,但它不工作。

Protected Sub ddlreportid_SelectedIndexChanged1(sender As Object, e As System.EventArgs) Handles ddlreportid.SelectedIndexChanged 

    Dim myConn As New SqlConnection 
    Dim myCmd As New SqlCommand 


    myConn.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString 

    myCmd = myConn.CreateCommand 

    myCmd.CommandText = "SELECT ReportName, ReportAddress, ReportPostcode WHERE ReportID = @ ReportID" 

    myCmd.Parameters.Add(New SqlParameter("@ReportID", (ddlreportid.Text))) 

    Dim reader As SqlDataReader = myCmd.ExecuteReader() 

    If (reader.Read()) Then 
     txtreportname.Text = reader(0) 
     txtreportaddress.Text = reader(1) 
     txtreportpostcode.Text = reader(2) 

    End If 

    myCmd.Dispose() 
    myConn.Close() 
    myConn.Dispose() 

End Sub 

謝謝

回答

0

嘗試,而不是...

myCmd.Parameters.Add(New SqlParameter("@ReportID", (ddlreportid.Text))) 

...這樣做:

myCmd.Parameters.Add(New SqlParameter("@ReportID", (ddlreportid.SelectedItem.Value))) 

還要確保您的下拉列表具有AutoPostBack = "true",如果你想您的網頁立即作出迴應。

+0

是AutoPostBack設置爲true。我沒有在頁面上發生任何錯誤,並且運行正常,直到我從下拉列表中選擇了一些內容並出現此錯誤「Microsoft JScript運行時錯誤:找不到成員」。 – 2013-03-21 15:43:29

+0

我必須在頁面加載下關於autoPostBack放置if語句嗎? – 2013-03-21 15:45:06

+0

請編輯您的問題以包含定義DropDownList的HTML以及任何可能引用它的驗證器或Javascript。你的錯誤聽起來像是發生在客戶端,所以我們需要看看你有什麼。 – 2013-03-21 16:05:56

相關問題