【问题标题】:Use DataItem in Code-behind to set class or style in Nested Repeater在代码隐藏中使用 DataItem 在嵌套中继器中设置类或样式
【发布时间】:2016-06-21 18:06:35
【问题描述】:

我已经环顾了一段时间,但我无法弄清楚这一点。我有一个嵌套的转发器 onItemDataBound 事件我想为一些 <DIV> 设置类和样式。

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

我卡在代码隐藏中我想在某些表达式中使用数据项的列之一的值,例如在上面的 rpDB_item_ItemDataBound 事件中。

IE:

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

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

也许有更好的方法可以做到这一点,例如在后面的代码中创建&lt;div&gt;,但也不确定该怎么做?任何帮助或建议将不胜感激(新手 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")) %> 

在类内部,但它没有将结果字符串发送到函数。

【问题讨论】:

  • 你真的不应该在后面的代码中设置这些。在标记中使用 if 语句!
  • 明白。然而这可能吗?我遇到了困难,在标记处内联。
  • 当然。 &lt;% if( &lt;some logic here&gt; ) { %&gt;
  • 不,不要使用 Response.Write。 &lt;%# if (DataBinder.Eval(Container.DataItem, "Online/Offline") == "Online") { %&gt;led-green&lt;%# } else { %&gt;led-red&lt;%# } %&gt;。顺便说一句,如果您切换到可以轻松使用 Razor syntax 的 MVC,则此语法更容易编写。
  • 当然会有好处。在 Razor 中执行条件逻辑比 Web 窗体更容易和更清晰。而且 MVC 本身并没有太多的设置开销。

标签: c# html asp.net code-behind nested-repeater


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    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 部分之前似乎正在做预期的事情。它没有分配关联的属性。再次注意,如果这有所不同,这是在嵌套中继器中。

    【讨论】:

    • 我发现了问题并解决了。问题在于未找到对象本身,因此未应用分配。
    猜你喜欢
    • 2010-11-25
    • 2011-10-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多