Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在代码隐藏中使用DataItem设置嵌套中继器中的类或样式_C#_Html_Asp.net_Code Behind_Nested Repeater - Fatal编程技术网

C# 在代码隐藏中使用DataItem设置嵌套中继器中的类或样式

C# 在代码隐藏中使用DataItem设置嵌套中继器中的类或样式,c#,html,asp.net,code-behind,nested-repeater,C#,Html,Asp.net,Code Behind,Nested Repeater,我环顾四周已经有一段时间了,但我无法弄明白这一点。我有一个嵌套的中继器,onItemDataBound事件我想为一些设置类和样式 HTML: > 代码隐藏 protected void rpDB_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingI

我环顾四周已经有一段时间了,但我无法弄明白这一点。我有一个嵌套的中继器,
onItemDataBound
事件我想为一些
设置类和样式

HTML: >

代码隐藏

 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)>*);
        }
    }
}
很明显,有些地方出了问题,我只知道接下来该怎么办。理想情况下,我可以根据dataitem值或值本身设置标签的类或标题

也许有更好的方法可以做到这一点,比如在code behind中创建
,但也不确定如何做到这一点?任何帮助或建议都将不胜感激(新手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>

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

var myFoo = e.Item.DataItem as Foo
if(myFoo != null && myFoo.Online == "Online")
    //Do something

我添加了这个,然后浏览了一遍。似乎正在执行预期的操作,直到
属性。添加
部分。它没有分配关联的属性。再次请注意,如果有区别的话,这是在嵌套的中继器中。

您真的不应该在代码隐藏中设置它们。在标记中使用if语句!理解。然而,这是可能的吗?我有困难,在markup.Sure在线完成<代码>
不,不要使用Response.Write<代码>led绿色led红色
。顺便说一句,如果你切换到MVC,这个语法会更容易编写,因为MVC可以很容易地使用。当然这会有好处。在Razor中执行条件逻辑比Web表单更简单、更干净。MVC本身没有那么多的设置开销,我发现了问题并解决了它。问题在于找不到对象本身,因此分配不适用。
<%# FileExists(DataBinder.Eval(Container.DataItem,"FileName")) %> 
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");
            }           
        }
    }