Question

Within codebehind in an ASPX page I get few details about the employees (read from external data source). Finally I would like to show in the below fashion. The display of below may be showin incorrect but is a simplpe table with header/column approach.

___________________________________________________________________________________
|                        DEPT        | HR                                          | |__________________________________________________________________________________|
|     Employee Image                 | Emp Name       | Hire Date                  |
|____________________________________|_____________________________________________|
|        Steve.gif                   | Steve Jobs     |  22/05/1979                |
|____________________________________|_____________________________________________|
|        Mark.gif                    | Mark Miller    |  22/05/1949                |
|____________________________________|_____________________________________________|
|                        DEPT        | Operations                                  | |__________________________________________________________________________________|
|     Employee Image                 | Emp Name       | Hire Date                  |
|____________________________________|_____________________________________________|

The data is collected from various data sources and is finally available within the codebehind. I want to display in the above fashion.

What is the best approach? I thought of creating a htmldivcontrol and adding all these values. Finally I will bind div tag to the page.

Currently I am trying with approach but want to know if there are any better approaches.

Please share your views and exaamples. _

Was it helpful?

Solution

The best approach in my opinion, would be to use a Repeater like this:

<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_RowDataBound">
        <ItemTemplate>
            <table runat="server" style="color: White; background-color: #3A4F63;" visible="false"
                id="headerTable">
                <tr>
                    <td colspan="3" align="center">
                        <asp:Label ID="headerTitle" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td style="width: 200px; background-color: #3A4F63; color: White;">
                        Image 
                    </td>
                    <td style="width: 200px;">
                        Name
                    </td>
                    <td style="width: 200px;">
                        Hire Date
                    </td>
                </tr>
            </table>
            <!-- These are the actual data items -->
            <!-- Bind to your specific properties i.e. Employees. -->
            <table>
                <tr>
                    <td style="width: 200px;">
                        <asp:Image ID="img" runat="server" ImageUrl='<%#Eval("ImageUrl") %>'></asp:Image>
                    </td>
                    <td style="width: 200px;">
                        <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                    </td>
                    <td style="width: 200px;">
                        <asp:Label ID="lblHireDate" runat="server" Text='<%#Eval("HireDate") %>'></asp:Label>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:Repeater>

And then on your code behind, something like:

private string currentDepartment =string.Empty;

protected void rpt_RowDataBound(object sender, RepeaterItemEventArgs e)
{

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        //Binding to Employee object.
        if (currentDepartment!= (e.Item.DataItem as Employee).Department) {
        currentDepartment = (e.Item.DataItem as Employee).Department;
            e.Item.FindControl("headerTable").Visible = true;
            (e.Item.FindControl("headerTitle") as Label).Text = (e.Item.DataItem as Employee).Department;
        }
            else {
            e.Item.FindControl("headerTable").Visible = false;
        }
    }
}

You would need to bind your grid to a List<Employee> where Employee would be defined like so:

public class Employee
{
   public string Department {get;set;}
   public string ImageUrl {get;set;}
   public DateTime HireDate {get;set;}
   public string Name {get;set;}
}

But they must be ordered by Department previously to being bound.

Sample code to Populate the repeater

private void bindGridView()
{
    List<Employee> emps = new List<Employee>();
    for (int i = 0; i < 5; i++)
    {
        Employee e = new Employee();
        if (i % 2 == 0)
        {
            e.Department = "Human resources";
            e.HireDate = DateTime.Now.AddDays(-i);
            e.ImageUrl = @"http://www.freedigitalphotos.net/images/gal_images/av-_146.jpg";
        }
        else
        {
            e.Department = "Information Technology";
            e.HireDate=DateTime.Now.AddMonths(-i);
            e.ImageUrl = "http://www.freedigitalphotos.net/images/gal_images/av-_314.jpg";

        }

        e.Name = "Employee " + i;
        emps.Add(e);

    }
    rpt.DataSource = emps.OrderBy(x=>x.Department);
    rpt.DataBind();
}

Produces

enter image description here

OTHER TIPS

A GridView would probably be best

Are you thinking of doing the table dynamically? You'll have a hard-time editing it in the future. Why not make an ordinary HtmlTable in your markup(.aspx), put some Labels and Images, then populate it in code-behind? Are there any special reasons why?

You have a couple of options.

One way would be to combine all of the disparate data into a single datatable that you create on the fly and bind to your data source.

Another is to simply do what you suggested and emit div's with the relevant info.

A third is to define the table in your aspx file and use asp:label controls. In your code behind populate the label controls from your various data sources.

Given those options I'd probably go with the third one. Sounds easiest to reconfigure / change the display of when you need to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top