Please refer this code. I have used temp data in this example. You can use your own SQL table.
If you want to apply the style to div tag then first give runat = server to div tag. Use HtmlControl for setting the the div tag style inside the OnItemDataBound event and set the style using hidden field value.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList_OnItemDataBound">
<ItemTemplate>
<asp:HiddenField ID="hfFontSize" runat="server" Value='<%# Eval("FontSize") %>' />
<table cellpadding="2" cellspacing="0" class="Item">
<tr>
<td class="header">
<b>
<asp:Label ID="lblName" Text='<%# Eval("Name") %>' runat="server" />
</b>
</td>
</tr>
<tr>
<td class="body">
<b>Id: </b>
<%# Eval("Id") %><br />
<b>Country: </b>
<br />
<asp:Label ID="lblCountry" Text='<%# Eval("Country") %>' runat="server" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
Namespace
using System.Data;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)),
new DataColumn("FontSize",typeof(string))});
dt.Rows.Add(1, "John Hammond", "United States", "10px");
dt.Rows.Add(2, "Mudassar Khan", "India", "20px");
dt.Rows.Add(3, "Suzanne Mathews", "France", "25px");
dt.Rows.Add(4, "Robert Schidner", "Russia", "30px");
this.DataList1.DataSource = dt;
this.DataList1.DataBind();
}
}
protected void DataList_OnItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Label lblName = e.Item.FindControl("lblName") as Label;
lblName.Style.Add("background-color", "green");
Label lblCountry = e.Item.FindControl("lblCountry") as Label;
lblName.Style.Add("font-size", (e.Item.FindControl("hfFontSize") as HiddenField).Value);
}
}
Screenshot
