I have looked around for awhile on this and I am not able to figure this out. I have a nested repeater that onItemDataBound
event I would like to set class and style for some <DIV>
.
HTML:
<%# DataBinder.Eval(Container.DataItem,"sServer") %>
>
CODE-BEHIND
protected void rpDB_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string _sql = "";
using(SqlConnection _conn = new SqlConnection(_sql))
{
_conn.Open();
DataTable _dt = new DataTable();
// Get repeater controls
Repeater rpDB_item = (Repeater)(e.Item.FindControl("rpDB_item"));
SqlCommand _cmd = new SqlCommand("", _conn);
SqlDataAdapter _da = new SqlDataAdapter(_cmd);
_da.Fill(_dt);
rpDB_item.DataSource = _dt;
rpDB_item.DataBind();
}
}
}
protected void rpDB_item_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (<value of dataitem("online")> == "Online")
{
((HtmlGenericControl)e.Item.FindControl("label2")).Attributes.Add("class", "glyphicon glyphicon-file");
((HtmlGenericControl)e.Item.FindControl("label2")).Attributes.Add("style", "color: green;");
((HtmlGenericControl)e.Item.FindControl("label2")).Attributes.Add("title", *<value of dataitem(sFile)>*);
}
}
}
Where I am stuck is in the code-behind I would like to use the value of one of the columns of the dataitem in some expressions, such as in the rpDB_item_ItemDataBound event above.
IE:
if (e.Item.DataItem("Online") == "Online")
{
((HtmlGenericControl)e.Item.FindControl("label2")).Attributes.Add("title", * e.Item.DataItem("sFile").ToString()*);
}
Obviously something is wrong I am just sure where to go from here. Ideally I am either setting a class or a title of a label based on the dataitem value or the value itself.
Maybe there is a better way of doing this, such as creating the <div>
in code behind, not really sure how to do that either? Any help or suggestions would be appreciated (NOVICE C#)
EDIT: I have added this function I think it is right
protected void FileExists(string url, RepeaterItemEventArgs e)
{
Label myLabel = (Label)(e.Item.FindControl("divfile"));
url = "@" + url;
if (File.Exists(url))
{
myLabel.Attributes.Add("class", "green");
}
else { myLabel.Attributes.Add("class", "red"); }
}
and the following label
<div class='anj red glyphicon glyphicon-file <%= %> id="dvFile" runat="server" title=<%# DataBinder.Eval(Container.DataItem,"FileName") %>></div>
How would I call the function? I tried
<%# FileExists(DataBinder.Eval(Container.DataItem,"FileName")) %>
inside the class but it is not sending the resulting string to the function.
protected void rpDB_item_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
HtmlGenericControl dbOnline = ((HtmlGenericControl)e.Item.FindControl("dbOnline"));
HtmlGenericControl sfile = ((HtmlGenericControl)e.Item.FindControl("lblfile"));
//HtmlGenericControl online = ((HtmlGenericControl)e.Item.FindControl("dbOnline"));
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string sonline = (string)(DataBinder.Eval(e.Item.DataItem, "Online/Offline").ToString());
string myfile = (string)(DataBinder.Eval(e.Item.DataItem,"FileName"));
if (sonline == "Online")
{
sfile.Attributes.Add("class", "green");
dbOnline.Attributes.Add("class", "led-green");
}
}
}
I added this and walked through it. Seems to be doing what is expected until the Attributes.Add
section. It is not assigning the associated attributes. Again note that this is in a nested repeater if that makes a difference.