2017-02-22 113 views
0

我可以使用MsgBox顯示數據,但是我希望它將它放在div內。顯示來自數據庫內部div的數據

有人可以向我解釋我該如何做到這一點?

代碼:

Dim connetionString As String 
Dim connection As SqlConnection 
Dim adapter As SqlDataAdapter 
Dim ds As New DataSet 
Dim i As Integer 
connetionString = "Data Source=Gadasini-aldigim\SQLEXPRESS;Initial Catalog=allgiz;Trusted_Connection=True" 
connection = New SqlConnection(connetionString) 

connection.Open() 
adapter = New SqlDataAdapter("SELECT * FROM gadalar", connection) 
adapter.Fill(ds) 
connection.Close() 
For i = 0 To ds.Tables(0).Rows.Count - 1 
    MsgBox(ds.Tables(0).Rows(i).Item(1)) 
Next 
+2

爲什麼div的?如果你想顯示錶格數據,你應該使用像GridView,ListView,Repeater,DataList或像ListBox或DropDownList這樣簡單的控件的數據綁定web控件。 –

回答

1

添加格在.aspx文件並設置runat="server"。現在您可以從代碼隱藏中訪問它。然後使用div的InnerHTML屬性更新內容。

注意:正如@Tim在評論中所說,我強烈建議您使用數據視圖控件來顯示數據集。

ASPX:

<div runat="server" id="div1"></div> 

VB.NET:

Dim connetionString As String 
    Dim connection As SqlConnection 
    Dim adapter As SqlDataAdapter 
    Dim ds As New DataSet 
    Dim i As Integer 
    Dim str As String 
    connetionString = "Data Source=Gadasini-aldigim\SQLEXPRESS;Initial Catalog=allgiz;Trusted_Connection=True" 
    connection = New SqlConnection(connetionString) 

    connection.Open() 
    adapter = New SqlDataAdapter("SELECT * FROM gadalar", connection) 
    adapter.Fill(ds) 
    connection.Close() 
    For i = 0 To ds.Tables(0).Rows.Count - 1 
     str= str & ds.Tables(0).Rows(i).Item(1) & "<br />" 
    Next 
    div1.InnerHTML = str; 
1

你應該喜歡設計視圖中添加一個PlaceHolder

<asp:PlaceHolder ID = "PlaceHolder1" runat="server" /> 

而且後面添加以下代碼:

Dim connetionString As String 
Dim connection As SqlConnection 
Dim adapter As SqlDataAdapter 
Dim ds As New DataSet 
Dim i As Integer 
connetionString = "Data Source=Gadasini-aldigim\SQLEXPRESS;Initial Catalog=allgiz;Trusted_Connection=True" 
connection = New SqlConnection(connetionString) 

connection.Open() 
adapter = New SqlDataAdapter("SELECT * FROM gadalar", connection) 
adapter.Fill(ds) 
connection.Close() 
Dim html As New StringBuilder() 
html.Append("<div>") 
For i = 0 To ds.Tables(0).Rows.Count - 1 

    html.Append(+ ds.Tables(0).Rows(i).Item(1) +) 

Next 
html.Append("</div>") 
PlaceHolder1.Controls.Add(New Literal() With {.Text = html.ToString()}) 
相關問題