2016-06-27 82 views
0

我根據數據庫中的條目動態創建表。在創建期間,我將其中一個DB字段分配給Table.ID。如何通過使用FindControl(ID)來更改表格的背景顏色?通過FindControl更改表backcolor

protected void AgentStat_Tick(object sender, EventArgs e) 
{ 
    Dictionary<string, string> activityAgent = new Dictionary<string, string>(); 
    string conString = "Data Source = lewcomp1\\COMPLIANCE; Initial Catalog = 3CXCallStats; Integrated Security = True;"; 
    string query = "SELECT FirstName, LastName, Status FROM Queue_Listing WHERE [ComplianceCC(English)] = 'True'"; 
    SqlConnection myCon = new SqlConnection(conString); 
    SqlCommand cmd = new SqlCommand(query, myCon); 
    SqlDataReader myReader; 

    myCon.Open(); 
    myReader = cmd.ExecuteReader(); 
    while (myReader.Read()) 
    { 
     string name = myReader["FirstName"].ToString(); 
     string surname = myReader["LastName"].ToString(); 
     string status = myReader["Status"].ToString(); 
     activityAgent.Add(name + surname.Substring(0, 1), status); 
    } 


    foreach (KeyValuePair<string, string> item in activityAgent) 
    { 
     if (item.Value == "Connected") 
     { 
      var state = listAgents.FindControl(item.Key); 

     } 
    } 
} 
+0

你能後的HTML代碼' ListAgents'? – SpiderCode

+0

ListAgents是一個ID爲空的div。用作容器 –

+0

請看看我發佈的答案。 – SpiderCode

回答

0

而不是使用var,我創建和實例的表,並指出它在正確的方向。請看下面:

   TableCell tCell = (TableCell)listAgents.FindControl(item.Key); 
       tCell.BackColor = System.Drawing.Color.Red; 
0

如果item.key包含標記爲Runat="Server"那麼你就可以改變你的表格的背景顏色下面所提到的表ID

var myTable = listAgents.FindControl(item.Key) as System.Web.UI.HtmlControls.HtmlTable; 
if(myTable != null) 
{ 
    myTable.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Red"); 
}