2013-04-02 25 views
-3

我想顯示在一個div並根據該訊息的答覆後,並連續要顯示另一個帖子和回覆... 我做了這麼多但這並不表明,所需的輸出如何使用asp.net進行評論回覆系統。?

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using System.Data.SqlClient; 

    public partial class Default3 : System.Web.UI.Page 
    { 
     int i = 0; 
     SqlConnection con = new SqlConnection("server=LOD-PC;uid=sa;pwd=1234;database=db2"); 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      con.Open(); 
      SqlCommand cmd = new SqlCommand("select * from t3 ORDER by id DESC", con); 
      SqlDataReader dr = cmd.ExecuteReader(); 
      while (dr.Read()) 
      { 
       i++; 
       string name = dr["name"].ToString(); 
       string image1 = dr["img"].ToString(); 
       string image2 = image1.Substring(2).ToString(); 
       string id = dr["id"].ToString(); 
       string status = dr["status"].ToString(); 
       string buttonid = i.ToString(); 

       Response.Write("<div id='myDiv' class='myDivClass'> <img src='" + image2 + "' style='width:50px; height:50px; float:left; margin-right:6px;' /> <br/> <br/> <span class='name'> <input id='texta' class='texta' type='text' value=" + name.ToString() + " /> <span/> <span class='id'> <input id='textb' class='textb' type='text' value=" + id.ToString() + " /> <span/> <div id='g'><br/><br/><br/><span class='status'> " + status + " </span> <span class='combutton'> <input id='"+buttonid+"' class='button' type='button' value='Comment'/> </span> </div> </div>"); 
      } 
      con.Close(); 
     } 
    } 
+0

**還有當我使用Response.Write(「任何asp標籤」); **沒有在頁面中顯示 –

+0

如果你不讓我們猜測它會做什麼*顯示,輸出有什麼問題。 –

+0

作爲一般規則,**不要在'Page' **中使用'Response.Write()'。改用網頁控制。 – jrummell

回答

0

當基於您的SQL查詢你是不是引用該值的文本輸出HTML:

value=" + id.ToString() + " /> 

這應該是

值= ' 「+ id.ToString()+」'/>

除此之外,您將不得不提供更多關於您所期望的內容沒有出現的信息。不過,我可以解決以下幾點:

的Response.Write( 「任何ASP標籤」)

ASP標籤處理服務器端,而不是客戶端。將它們寫入響應不會導致它們被執行。如果您在瀏覽器的HTML中「查看源代碼」,您會在HTML源代碼中看到這些標籤,但它們對瀏覽器沒有任何特殊意義。

請處理實現IDisposable的事情,如SqlConnection,SqlCommand和SqlDataReader。 使用關鍵字是去那裏的方法。例如用

SqlConnection con = new SqlConnection("server=LOD-PC;uid=sa;pwd=1234;database=db2"); 

的行,如果將引發異常訪問數據庫

con.Close(); 

將不被執行。

相反,使用

using (SqlConnection con = 
    new SqlConnection("server=LOD-PC;uid=sa;pwd=1234;database=db2")) 
{ 
    // Do stuff with the connection 
} 

當使用塊超出範圍,它會自動調用con.Dispose()(一個例外是否被塊內拋出)的形式,其將依次調用con.Close()

服務器= LOD-PC; UID = SA

請不要與sa用戶進行連接。這是要求安全馬褲。