2013-08-07 45 views
2

我需要將邊框顏色和邊框樣式設置爲動態創建的表格行。我怎樣才能做到這一點?如何設置動態創建的表格行的屬性?

if (cmd.Connection.State == ConnectionState.Closed) 
       cmd.Connection.Open(); 
      using (SqlDataReader reader = cmd.ExecuteReader()) 

      { 

       if (reader.HasRows) 
       { 
        while (reader.Read()) 
        { 

         JobDesignation = reader.GetString(0); 
         JobDescription = reader.GetString(1); 
         NoOfVacancies = Convert.ToString(reader.GetInt32(2)); 
         DatePosted = Convert.ToString(reader.GetDateTime(3)).Replace("00:00:00", ""); 
         jobId = reader.GetString(4); 
         int tblRows = 1; 
         int tblCols = 1; 

         Table tbl = new Table(); 
         PlaceHolder1.Controls.Add(tbl); 
         for (int i = 0; i < tblRows; i++) 
         { 
          TableRow tr = new TableRow(); 

          for (int j = 0; j < tblCols; j++) 
          { 
           TableCell tc = new TableCell(); 
           System.Web.UI.WebControls.Label lblBox = new System.Web.UI.WebControls.Label(); 
           lblBox .Text = "Job ID:" + jobId + Environment.NewLine + "Job Designation:" + JobDesignation + Environment.NewLine + "Job Description:" + JobDescription + Environment.NewLine + "Vacancies:" + NoOfVacancies + Environment.NewLine + "Ad Posted On:" + DatePosted + ""; 
           tc.Controls.Add(lblBox); 
           tr.Cells.Add(tc); 
          } 

          tr.Width = new Unit("700px"); 
          tr.Height = new Unit("200px"); 
          tr.BorderColor = System.Drawing.Color.Black; 
          tr.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid; 
          tbl.Rows.Add(tr); 

         } 
         ViewState["dynamictable"] = true; 
         } reader.NextResult(); 

       } 

      } 

我還希望在單獨的行中顯示職位ID,職位描述,職位名稱,職位空缺編號。我怎麼能做到這一點?

請幫幫我。

+0

: -

關於第二個問題: 要顯示在單獨一行,用於顯示CSS前塊,使每個項目將出現在新行? – Arshad

回答

1

可按照下述動態設置屬性創建表:

TableRow row1 = new TableRow(); 
    row1.CssClass = "rowStyle1"; 

    TableCell cell1 = new TableCell(); 
    cell1.CssClass = "cellStyle1"; 


//create css class as below in your css file : 

.rowStyle{ 
    border:1px solid red; 
} 
.cellStyle1{ 
    background-color:blue; 
} 
1

嘗試LiteralControl

for (int j = 0; j < tblCols; j++) 
{ 
    TableCell tc = new TableCell(); 
    tc.Controls.Add(new LiteralControl("Job ID:" + jobId + "<br>" + "Job Designation:" + JobDesignation + "<br>" + "Job Description:" + JobDescription + "<br>" + "Vacancies:" + NoOfVacancies + "<br>" + "Ad Posted On:" + DatePosted + "")); 
    tr.Cells.Add(tc); 
} 
0

好吧,如果你正在談論的風格,我強烈建議你使用CSS。請嘗試在CSS類中包含相應的行。在一個單獨的CSS文件中,您可以根據需要設置該類的屬性。

關於你的第二個問題,我建議你用一個單元格創建一個新行並將它的colspan設置爲tblCols。你可以這樣做的權利後:

tbl.Rows.Add(tr); 

希望幫助,

0

因爲這是動態的,我要使用的CSS類定義的樣式典型的建議不會在這裏做的工作承擔 - 否則簡單的解決方案是設置你的css類(總是推薦的方法)

如果它們不能預定義並且可以在樣式上有所不同,那麼Table繼承WebControl,以便你有Table.Style您可以將所有樣式定義添加到 - 例如:http://msdn.microsoft.com/en-us/library/system.web.ui.cssstylecollection.aspx

儘管上面的代碼會發生什麼?你的標記是什麼樣的? TD可以覆蓋TR邊框樣式(例如row border color),但是您沒有設置上述任何一種。你爲什麼不出去中繼How make 2 lines in <TD>