使用 Code-behind 中的 DataItem 在 Nested Repeater 中设置 class 或样式

Use DataItem in Code-behind to set class or style in Nested Repeater

我已经在这方面四处寻找了一段时间,但我无法弄清楚。我有一个 onItemDataBound 事件的嵌套中继器,我想为某些 <DIV>.

设置 class 和样式

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)>*);
        }
    }
}

我卡住的地方是 code-behind 我想在某些表达式中使用数据项的其中一列的值,例如在上面的 rpDB_item_ItemDataBound 事件中。

IE:

if (e.Item.DataItem("Online") == "Online")
{
   ((HtmlGenericControl)e.Item.FindControl("label2")).Attributes.Add("title", * e.Item.DataItem("sFile").ToString()*);
} 

显然出了点问题,我只是确定从这里去哪里。理想情况下,我要么根据数据项值或值本身设置 class 或标签标题。

也许有更好的方法,例如在代码后面创建 <div>,也不确定该怎么做?任何帮助或建议将不胜感激 (NOVICE C#)

编辑: 我加了这个功能我觉得是对的

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"); }
}

和下面的标签

<div class='anj red glyphicon glyphicon-file <%= %> id="dvFile" runat="server" title=<%# DataBinder.Eval(Container.DataItem,"FileName") %>></div>

我将如何调用该函数?我试过了

<%# FileExists(DataBinder.Eval(Container.DataItem,"FileName")) %> 

在 class 中,但它没有将结果字符串发送到函数。

e.Item.DataItem的类型是转发器绑定的类型。 因此,如果您已将 Foo 的列表绑定到中继器并且您想要访问单个 Foo 的属性,则将 e.Item.DataItem 转换为类型 Foo.

var myFoo = e.Item.DataItem as Foo
if(myFoo != null && myFoo.Online == "Online")
    //Do something
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");
            }           
        }
    }

我添加了这个并浏览了它。在 Attributes.Add 部分之前似乎正在做预期的事情。它没有分配关联的属性。再次注意,如果有区别的话,这是在嵌套中继器中。