通过FindControl更改表格背景色

时间:2016-06-27 06:49:44

标签: c# asp.net

我根据数据库中的条目动态创建表。在此创建过程中,我将其中一个数据库字段分配给Table.ID。如何使用FindControl(ID)

更改表格的背景颜色
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);

        }
    }
}

2 个答案:

答案 0 :(得分:0)

我没有使用var,而是创建表的实例并将其指向正确的方向。请参阅以下内容:

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

答案 1 :(得分:0)

如果item.key包含标记为ID的表格的Runat="Server",那么您可以更改表格的背景颜色,如下所述:

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