0

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Anirudh
  • 581
  • 5
  • 14
  • 32

4 Answers4

6

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

Icarus
  • 63,293
  • 14
  • 100
  • 115
1

A GridView would probably be best

novacara
  • 2,207
  • 4
  • 24
  • 34
  • I generate or gather the data in codebehind from varoius tables. For example there is department, employee, employee profile etc. How do I bind the resultant values to gridview? Also I have multiple headers (like Dept and then emp-image, empname etc on another line). Can you share any examples? – Anirudh Nov 08 '11 at 21:56
  • How are you gathering this data? You say you are getting it from various tables? What format is it in? – novacara Nov 08 '11 at 22:09
  • You put the gridview on the page, give it an ID and then write this: MyGridView.DataSource = ... ; MyGridView.DataBind(); On the aspx page, you simply create a template that displays your data. I'd put the data in an object that's coming from a class, like mentioned above. – frenchie Nov 09 '11 at 04:25
  • @novacara: sorry for late reply. I am reading using SQL and as I get the desired data, I add them to htmlcontrol. When I go through the data and collect required data, I immediately add to an htnmlcontrol. This control has , and
    .
    – Anirudh Nov 09 '11 at 14:49
0

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?

Alvin
  • 985
  • 5
  • 13
  • @whinn: I am actually doing the same logic as you mentioned. I am adding to htmlcontrol in codebehind but need to define everything (such as stylesheet classes, tr, td etc). Was looking if there is any better approach. – Anirudh Nov 09 '11 at 14:54
  • go for it then, I think thats the best approach you can do. :) – Alvin Nov 10 '11 at 00:54
0

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.

NotMe
  • 87,343
  • 27
  • 171
  • 245
  • :third option->How to manage with table in aspx considering my scenario where I donot know how many rows will be generated.? – Anirudh Nov 09 '11 at 15:16