2013-12-22 33 views
3

這是一個初學者問題。我想從數據庫動態創建div(MSSQL)。例如,我想在條目下方顯示評論。 評論表連接條目條目ID。我的aspx代碼是這樣的:在ASP.NET中動態創建DIV數據庫

<div class="commentBody" runat="server"> 
     <asp:Label ID="commentSender" runat="server" Text=""></asp:Label> 
     <asp:Label ID="commentDate" runat="server" Text=""></asp:Label> 
     <asp:Label ID="commentText" runat="server" Text=""></asp:Label> 
</div> 

這將重複所有評論。我正在工作所有代碼隱藏(沒有evals)。我的c#代碼:

protected void YorumlariGetir() 
{ 
    string selectComments = "SELECT * FROM Comment WHERE Comment.EntryID = @EntryID"; 

    SqlConnection conn = new SqlConnection(constr); 
    SqlCommand cmd = new SqlCommand(selectComments, conn); 

    cmd.Parameters.AddWithValue("@EntryID", Session["EntryID"].ToString()); 

    try 
    { 
     conn.Open(); 
     // HERE I WANT TO CALL A LOOP FOR COMMENTS 

    } 
    catch (Exception ex) 
    { 
     Response.Write("Hata: " + ex.Message); 
    } 
    finally 
    { 
     conn.Close(); 
    } 
} 

我可以使用中繼器或foreach循環。但我不知道如何,需要一個例子在這一點上。

感謝您的幫助。

+0

你有沒有使用「repeater C#asp.net」? –

回答

7

編輯:回答全面修訂。

在你的問題中,你要求一種方法來做循環和每個循環迭代添加註釋。你可以做到這一點,但有更好的方式使用內置的ASP.NET控件。我將首先向您展示一個簡單的迭代SqlDataReader對象並手動創建HTML的示例。然後我會展示一個更好的解決方案。如果您能夠實施第二種方案,我不建議採用第一種方案。

在這兩種解決方案上,我強烈建議在select查詢中專門命名您的字段,而不是使用星號來選擇所有字段。如果表結構更改,則使用SELECT *可能會導致問題。另外,您可能正在選擇不需要浪費資源的數據列。

首先,這裏是使用SqlDataReader類的非常簡單的例子。這會起作用,但請記住有一個更好的方法。

try 
    { 
     conn.Open(); 
     // HERE I WANT TO CALL A LOOP FOR COMMENTS 
     SqlDataReader reader = cmd.ExecuteReader(); 
     while (reader.Read()) 
     { 
      // create the div to wrap around the comment 
      HtmlGenericControl div = new HtmlGenericControl("div"); 
      div.Attributes.Add("style", "commentBody"); 

      // create three labels and add them to the div 
      // 1,2,3 are the ordinal positions of the column names, this may need corrected since I have no idea what your table looks like. 
      div.Controls.Add(new Label() { Text = reader.GetString(1) }); 
      div.Controls.Add(new Label() { Text = reader.GetString(2) }); 
      div.Controls.Add(new Label() { Text = reader.GetString(3) }); 

      // add the div to the page somehow, these can be added to any HTML control that can act as a container. I would suggest a plain old div. 
      MyMainDiv.Controls.Add(div); 

     } 
    } 

現在,上面的方法可以工作,但它是一種笨拙的,老式的處理顯示數據的方式。現代.NET應用程序應該使用更好的解決方案。更好的解決方案是使用Data Binding。在互聯網上有很多關於這方面的文章和教程,所以如果這是一個新想法,你可以做一些教程來學習數據綁定的更好的點。

要使用中繼器類,第一Repeater控件添加到您的ASPX頁面:

<asp:Repeater id="Repeater1" runat="server"> 
    <ItemTemplate> 
     <div class="commentBody"> 
      <span class="commentSender"><%# DataBinder.Eval(Container.DataItem,"aucommentSenderid") %></span> 
      <span class="commentDate"><%# DataBinder.Eval(Container.DataItem,"aucommentDateid") %></span> 
      <span class="commentText"><%# DataBinder.Eval(Container.DataItem,"aucommentTextid") %></span> 
     </div> 
    </ItemTemplate> 
</asp:Repeater> 

接下來,添加一些代碼隱藏創建數據源和連接這Repeater控件:

SqlDataAdapter da = new SqlDataAdapter(cmd);  // use your existing SqlCommand here (don't use select *) 
DataSet ds = new DataSet();      // create a DataSet object to hold you table(s)... this can contain more than 1 table 
da.Fill(ds, "Comment");       // fill this dataset with everything from the Comment Table 
Repeater1.DataSource = ds.Tables["Comment"];  // attach the data table to the control 
Repeater1.DataBind();        // This causes the HTML to be automatically rendered when the page loads. 
+0

我真的很抱歉,但這與問題無關。我並不是說你錯了,但是你沒有背景。 – danish

+1

@丹納,問題**非常具體**「我想從數據庫動態創建div」,我認爲這是正確的。 – laylarenee

+0

OP在這裏看起來很困惑。他並不需要多個div。他可以使用任何支持IListSource進行數據綁定的工具。我可能在這裏錯了。 – danish