1

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.

Mike C
  • 63
  • 1
  • 11
  • You really shouldn't set those in code behind. Use if statements in the markup! – mason Mar 08 '16 at 00:05
  • Understood. However is it possible? I am having struggles, doing it inline at markup. – Mike C Mar 09 '16 at 20:13
  • 1
    Sure. `<% if( ) { %>` – mason Mar 09 '16 at 20:14
  • I get the syntax of the if statement but not able to get it work. this is what I have tried `<%# if (DataBinder.Eval(Container.DataItem, "Online/Offline") == "Online") { Response.Write("led-green"); } else { Response.Write("led-red"); }` And the shortcut version `(DataBinder.Eval(Container.DataItem, "Online/Offline") == "Online" ? Response.Write("led-green") : Response.Write("led-red") );` – Mike C Mar 09 '16 at 20:38
  • 1
    No, don't use Response.Write. `<%# if (DataBinder.Eval(Container.DataItem, "Online/Offline") == "Online") { %>led-green<%# } else { %>led-red<%# } %>`. By the way this syntax is much easier to write if you switch to MVC which can easily use [Razor syntax](http://www.asp.net/web-pages/overview/getting-started/introducing-razor-syntax-c). – mason Mar 09 '16 at 20:41
  • Doesn't work `<%# if(DataBinder.Eval(Container.DataItem,"Online/Offline") == "ONLINE") { %>led-green<%# } else { %>led-red<%# } %>` I get a series of errors that tell me it is not rendering the code as a complete unit of code. IE: missing ")" and ";". This is a one page site so not sure using MVC would be of any benefit would it? – Mike C Mar 09 '16 at 21:13
  • 1
    Sure there would be benefit. Doing conditional logic in Razor is much easier and cleaner than Web Forms. And MVC itself doesn't have all that much overhead to setup. – mason Mar 09 '16 at 21:30
  • I'll have to look into it. I haven't used MVC before so might be for a future alteration. There has to a be a solution here somewhere I'll just have to keep looking. I need to set classes at ItemDatabound and before rendering. It's a simple enough of a page and works in Classic ASP but trying to bring these guys up to more current tech. – Mike C Mar 09 '16 at 21:55
  • If you're trying to bring them up to more current tech, then Web Forms was a terrible choice. It's not included in ASP.NET Core, which is clearly the future of where .NET is heading. Web Forms is based on an old and outdated way of thinking about the web. If you wanted to bring it up to a modern framework, then MVC and/or a client side JavaScript framework (Angular, React, etc) would be much better choices. – mason Mar 09 '16 at 21:59
  • Appreciate the suggestion and I may move to MVC in the future as I am able to become versed in it, however I could use help with this as it is. – Mike C Mar 21 '16 at 15:59

2 Answers2

1

The type of e.Item.DataItem is the type that's bound to the repeater. So if you've bound a list of Foo to the repeater and you want to access the properties of an individual Foo then cast e.Item.DataItem as type Foo.

var myFoo = e.Item.DataItem as Foo
if(myFoo != null && myFoo.Online == "Online")
    //Do something
Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
0
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.

Mike C
  • 63
  • 1
  • 11
  • I found the issue and resolved it. The issue was in the object itself not being found and therefore the assignment was not applying. – Mike C May 12 '16 at 15:43