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

Mike C

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
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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get Footer Item value on code behind with nested Repeater?

From Dev

Set StaticResource style of a control in code behind

From Dev

How to set a nested datasource from code behind?

From Dev

Nested Class In ASP Repeater

From Dev

CSS Style not being set Dynamically in ASP.NET Code behind

From Dev

How to set Style property in Code Behind for Specific DataRow

From Dev

How to Fill Repeater ItemTemplate from Code Behind?

From Dev

Get a value from Repeater in code-behind

From Dev

WPF. Use button style which is in xaml in code behind

From Dev

Calling a div class that's set by code behind function in jquery

From Dev

Change Style from code behind

From Dev

Use code behind in CSS

From Dev

How to get Reference to the label in repeater item in code behind

From Dev

Give asp:hyperlink in a repeater a url from code behind

From Dev

How to add Repeater control dynamically from code behind?

From Dev

How to call javascript from code behind for Repeater control

From Dev

Add extra repeater item or loop from the code behind

From Dev

How to set a:before css style from code behind aspx.cs page

From Dev

Dynamically change the class of a div nested in a gridview item template from code behind c#

From Dev

React native code behind class

From Dev

define a style for a button in code behind UWP app

From Dev

Change list style attribute from code behind

From Dev

How to add a Style to a ResourceDictionary in code-behind

From Dev

Change list style attribute from code behind

From Dev

Create and style a ListView all in code behind

From Dev

Can't use injected class in Razor Pages code-behind class

From Dev

Can't use injected class in Razor Pages code-behind class

From Dev

How to use SqlDataAdapter in code behind?

From Dev

How to use SqlDataAdapter in code behind?

Related Related

  1. 1

    How to get Footer Item value on code behind with nested Repeater?

  2. 2

    Set StaticResource style of a control in code behind

  3. 3

    How to set a nested datasource from code behind?

  4. 4

    Nested Class In ASP Repeater

  5. 5

    CSS Style not being set Dynamically in ASP.NET Code behind

  6. 6

    How to set Style property in Code Behind for Specific DataRow

  7. 7

    How to Fill Repeater ItemTemplate from Code Behind?

  8. 8

    Get a value from Repeater in code-behind

  9. 9

    WPF. Use button style which is in xaml in code behind

  10. 10

    Calling a div class that's set by code behind function in jquery

  11. 11

    Change Style from code behind

  12. 12

    Use code behind in CSS

  13. 13

    How to get Reference to the label in repeater item in code behind

  14. 14

    Give asp:hyperlink in a repeater a url from code behind

  15. 15

    How to add Repeater control dynamically from code behind?

  16. 16

    How to call javascript from code behind for Repeater control

  17. 17

    Add extra repeater item or loop from the code behind

  18. 18

    How to set a:before css style from code behind aspx.cs page

  19. 19

    Dynamically change the class of a div nested in a gridview item template from code behind c#

  20. 20

    React native code behind class

  21. 21

    define a style for a button in code behind UWP app

  22. 22

    Change list style attribute from code behind

  23. 23

    How to add a Style to a ResourceDictionary in code-behind

  24. 24

    Change list style attribute from code behind

  25. 25

    Create and style a ListView all in code behind

  26. 26

    Can't use injected class in Razor Pages code-behind class

  27. 27

    Can't use injected class in Razor Pages code-behind class

  28. 28

    How to use SqlDataAdapter in code behind?

  29. 29

    How to use SqlDataAdapter in code behind?

HotTag

Archive