2015-05-01 69 views
0

我HTML表我隱藏其將結合在頁面加載數據。按鈕綁定HTML表再次單擊

現在我有一個按鈕,將數據插入數據庫。我的問題是當我插入按鈕點擊數據時,我的頁面得到回傳。我的HTML表格數據將只顯示舊數據不是我已經添加的新數據。因爲html表格首先在pageload中綁定並且在單詞之後,我的控件將轉到button_click事件。

我試圖把對的IsPostBack的Page_Load但隨後第一次,當頁面加載得到HTML表計量不顯示任何數據。

我怎樣才能解決這個問題。

protected void Page_Load(object sender, EventArgs e) 
     { 
      using (SqlCommand cmd = new SqlCommand()) 
      { 
       cmd.Connection = conn; 
       cmd.CommandType = CommandType.Text; 
       cmd.CommandText = "Select * from Ticket"; 
       conn.Open(); 

       SqlDataReader dr = cmd.ExecuteReader(); 

       // //Building an HTML string. 
       StringBuilder html = new StringBuilder(); 

       //Table start. 

       html.Append("<table id='tbldata' style='table-layout:fixed;' border='0'>"); 
       html.Append("<THEAD>"); 
       html.Append("<TR>"); 
       html.Append("<TH>Ticket</TH>"); 
       html.Append("<TH>Priority</TH>"); 
       html.Append("<TH>Status<div class='fildownarrow'></div></TH>"); 
       html.Append("<TH>Practice Name<div class='fildownarrow'></div></TH>"); 
       html.Append("<TH>Patient Name</TH>"); 
       html.Append("<TH>Assigned</TH>"); 
       html.Append("<TH>Subject</TH>"); 
       html.Append("<TH style='width:100px;><a href='#'><div class='filtericon'></div></a></TH>"); 
       html.Append("</TR>"); 
       html.Append("</THEAD>"); 

       if (dr.HasRows) 
       { 
        while (dr.Read()) 
        { 
         html.Append("<tbody>"); 
         html.Append("<tr>"); 
         html.Append("<td>" + dr["Ticket_no"] + "</td>"); 
         html.Append("<td>" + dr["Priority"] + "</td>"); 
         html.Append("<td>" + dr["Status"] + "</td>"); 
         html.Append("<td>" + dr["Practice_Name"] + "</td>"); 
         html.Append("<td>" + dr["Patient_Name"] + "</td>"); 
         html.Append("<td>" + dr["Assign_User_Name"] + "</td>"); 
         html.Append("<td>" + dr["Msg_Subject"] + "</td>"); 
         html.Append("</tr>"); 
         html.Append("</tbody>"); 
        } 
       } 

       //Table end. 
       html.Append("</table>"); 

       //Append the HTML string to Placeholder. 
       PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() }); 

       dr.Close(); 
       dr.Dispose(); 
      } 
+0

不要做這種方式,檢索數據到一個列表並將其綁定到ListView控件。將爲您節省大量的麻煩。 – Banana

+0

使用SqlDataAdapter來檢索數據,並使用Repeater,DataList或GridView來顯示數據 – Sandeep

回答

2

你應該背後組織你的代碼是這樣的:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     LoadData(); 
    } 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    //Write data to database 
    LoadData(); 
} 

private void LoadData() 
{ 
    using (SqlCommand cmd = new SqlCommand()) 
    { 
     //Here goes your sql code that reads the database 
    } 
} 

當然更好的解決辦法是使用像一個GridView控件和數據直接綁定到它。

+0

感謝它現在的工作。 –

0

如果您在檢查IsPostBack屬性時是否在頁面加載時收到空表,我認爲您正在檢查它的值是否爲true。所以表格只會在回發時加載。您應該檢查IsPostBack屬性值是否爲僅顯示頁面的第一個負載的內容。

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!Page.IsPostBack) 
    { 
     using (SqlCommand cmd = new SqlCommand()) 
     { 
      cmd.Connection = conn; 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = "Select * from Ticket"; 
      conn.Open(); 

      SqlDataReader dr = cmd.ExecuteReader(); 

      // //Building an HTML string. 
      StringBuilder html = new StringBuilder(); 

      //Table start. 

      html.Append("<table id='tbldata' style='table-layout:fixed;' border='0'>"); 
      html.Append("<THEAD>"); 
      html.Append("<TR>"); 
      html.Append("<TH>Ticket</TH>"); 
      html.Append("<TH>Priority</TH>"); 
      html.Append("<TH>Status<div class='fildownarrow'></div></TH>"); 
      html.Append("<TH>Practice Name<div class='fildownarrow'></div></TH>"); 
      html.Append("<TH>Patient Name</TH>"); 
      html.Append("<TH>Assigned</TH>"); 
      html.Append("<TH>Subject</TH>"); 
      html.Append("<TH style='width:100px;><a href='#'><div class='filtericon'></div></a></TH>"); 
      html.Append("</TR>"); 
      html.Append("</THEAD>"); 

      if (dr.HasRows) 
      { 
       while (dr.Read()) 
       { 
        html.Append("<tbody>"); 
        html.Append("<tr>"); 
        html.Append("<td>" + dr["Ticket_no"] + "</td>"); 
        html.Append("<td>" + dr["Priority"] + "</td>"); 
        html.Append("<td>" + dr["Status"] + "</td>"); 
        html.Append("<td>" + dr["Practice_Name"] + "</td>"); 
        html.Append("<td>" + dr["Patient_Name"] + "</td>"); 
        html.Append("<td>" + dr["Assign_User_Name"] + "</td>"); 
        html.Append("<td>" + dr["Msg_Subject"] + "</td>"); 
        html.Append("</tr>"); 
        html.Append("</tbody>"); 
       } 
      } 

      //Table end. 
      html.Append("</table>"); 

      //Append the HTML string to Placeholder. 
      PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() }); 

      dr.Close(); 
      dr.Dispose(); 
     } 
    } 
} 
+0

這不會工作。他的桌子是在這裏創建的,這意味着它不會根據回傳進行更新。 – Banana

+0

@Banana哦,你說得對。似乎我誤解了這個問題。 –