The current blog is simple example of using list view control in .Net for binding and showing data to the user.

For this example I have taken class ‘Resume’ that has following fields

Resumeid, name, experience, email, phoneno, expectedsalary, status

  1. /--This is class Resume--/
  2. public class Resume
  3. {
  4. public int resumeid { get; set; }
  5. public string name { get; set; }
  6. public string experience { get; set; }
  7. public string email { get; set; }
  8. public string phoneno { get; set; }
  9. public string expectedsalary { get; set; }
  10. public int status { get; set; }
  11. public Resume(int Resumeid, string Name, string Experience, string
    Email,
    string Phoneno, string Expectedsalary, int Status)
  12. {
  13. resumeid = Resumeid;
  14. name = Name;
  15. experience = Experience;
  16. email = Email;
  17. phoneno = Phoneno;
  18. expectedsalary = Expectedsalary;
  19. status=Status;
  20. }
  21. }
  22. /-- This is bind method--/
  23. private void BindResumes()
  24. {
  25. List<Resume> resumes = new List<Resume>();
  26. Resume r1 = new Resume(1, "Jay Raval", "1.5 years", "[email protected]",
    "123-456-789", "2.5 per annum", 1);
  27. Resume r2 = new Resume(2, "Parth Bhavasar", "1 years", "[email protected]",
    "123-789-456", "2 per annum", 1);
  28. Resume r3 = new Resume(3, "Bhavik Patel", "3.5 years", "[email protected]",
    "456-123-789", "3.5 per annum", 1);
  29. Resume r4 = new Resume(4, "Jatin Saxena", "5 years", "[email protected]",
    "143-456-789", "4.5 per annum", 0);
  30. Resume r5 = new Resume(5, "Nitin Patel", "1.5 years", "[email protected]",
    "153-456-789", "2.5 per annum", 1);
  31. Resume r6 = new Resume(6, "Ravi Patel", "5 months", "[email protected]",
    "163-456-789", "5.5 per annum", 0);
  32. Resume r7 = new Resume(7, "Vinit Sonara", "2 monts", "[email protected]",
    "173-456-789", "1.5 per annum", -1);
  33. Resume r8 = new Resume(8, "Anil Motwani", "2.5 years", "[email protected]",
    "183-456-789", "3.5 per annum", 1);
  34. Resume r9 = new Resume(9, "Hardik Raval", "1.2 years", "[email protected]",
    "193-456-789", "2.5 per annum", 1);
  35. Resume r10 = new Resume(10, "Deep Trivedi", "1 years", "[email protected]",
    "113-456-789", "3.5 per annum", -1);
  36. resumes.Add(r1);
  37. resumes.Add(r2);
  38. resumes.Add(r3);
  39. resumes.Add(r4);
  40. resumes.Add(r5);
  41. resumes.Add(r6);
  42. resumes.Add(r7);
  43. resumes.Add(r8);
  44. resumes.Add(r9);
  45. resumes.Add(r10);
  46. lstViewResumes.DataSource = resumes;
  47. lstViewResumes.DataBind();
  48. }
/-- This is aspx page--/
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title></title>
  5. </head>
  6. <body>
  7. </body>
  8. </html>
  9. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestList.aspx.cs" Inherits="UserPages_TestList" Title="ListView Example" %>
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  11. <html xmlns="http://www.w3.org/1999/xhtml">
  12. <head id="Head1" runat="server">
  13. <title>Untitled Page</title>
  14. </head>
  15. <body>
  16. <form id="form1" runat="server">
  17. <div>
  18. <h1 align="center">List View Example</h1>
  19. <asp:ListView ID="lstViewResumes" runat="server">
  20. <layouttemplate>
  21. <table border=1 align="center" style="background-color:white;" cellpadding="3px" cellspacing="1px">
  22. <thead style="background-color:Silver">
  23. <td>Id</td>
  24. <td>Name</td>
  25. <td>Experience</td>
  26. <td>Email</td>
  27. <td>Phoneno</td>
  28. <td>Expected Salary</td>
  29. <td>Status</td>
  30. </thead>
  31. <tbody style="background-color:#f9f9f9">
  32. <asp:PlaceHolder ID="itemplaceholder" runat="server"></asp:PlaceHolder>
  33. </tbody>
  34. </table>
  35. </layouttemplate>
  36. <itemtemplate>
  37. <tr style="background-color:#99FFCC;padding:5px">
  38. <td><%#Eval("resumeid")%></td>
  39. <td><%#Eval("name")%></td>
  40. <td><%#Eval("experience")%></td>
  41. <td><%#Eval("email")%></td>
  42. <td><%#Eval("phoneno")%></td>
  43. <td><%#Eval("expectedsalary")%></td>
  44. <td>Active</td>
  45. </tr>
  46. </itemtemplate>
  47. </asp:ListView>
  48. </div>
  49. </form>
  50. </body>
  51. </html>
Output of following code is



In the above output all the records are shown as ‘Active ‘ so easily shown.

But if I some records are ‘Deleted’ and some are ‘Rejected’ and want to show in listview with different style then following changes are made.

If Records are ‘Deleted’ then ‘status’ flag is 0
If Records are ‘Rejected ‘ then ‘status’ flag is -1

Conditioning using PlacHolder

For This type of condition in list view we can use : PlaceHolder
  1. <asp:PlaceHolder runat="server" Visible='Condition'>
  2. --template--
  3. </asp:PlaceHolder>
If I Just want to show ‘Active’ records means ‘status’ is 1

Then following changes are done in <ItemTemplate>
  1. <ItemTemplate>
  2. <asp:PlaceHolder runat="server" Visible='<%#Eval("Status").ToString()=="1" %>'>
  3. <tr style="background-color:#99FFCC;padding:5px">
  4. <td><%#Eval("resumeid")%></td>
  5. <td><%#Eval("name")%></td>
  6. <td><%#Eval("experience")%></td>
  7. <td><%#Eval("email")%></td>
  8. <td><%#Eval("phoneno")%></td>
  9. <td><%#Eval("expectedsalary")%></td>
  10. <td>Active</td>
  11. </tr>
  12. </asp:PlaceHolder>
  13. </ItemTemplate>
Output of following code is



Now we will show Resumes with all ‘status’

So we have to do following changes in <ItemTemplate>
  1. <ItemTemplate>
  2. <asp:PlaceHolder runat="server" Visible='<%#Eval("Status").ToString()=="1" %>'>
  3. <tr style="background-color:#99FFCC;padding:5px">
  4. <td><%#Eval("resumeid")%></td>
  5. <td><%#Eval("name")%></td>
  6. <td><%#Eval("experience")%></td>
  7. <td><%#Eval("email")%></td>
  8. <td><%#Eval("phoneno")%></td>
  9. <td><%#Eval("expectedsalary")%></td>
  10. <td>Active</td>
  11. </tr>
  12. </asp:PlaceHolder>
  13. <asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%#Eval("Status").ToString()=="0" %>'>
  14. <tr style="background-color:#db3000;padding:5px">
  15. <td><%#Eval("resumeid")%></td>
  16. <td><%#Eval("name")%></td>
  17. <td><%#Eval("experience")%></td>
  18. <td><%#Eval("email")%></td>
  19. <td><%#Eval("phoneno")%></td>
  20. <td><%#Eval("expectedsalary")%></td>
  21. <td>Deleted</td>
  22. </tr>
  23. </asp:PlaceHolder>
  24. <asp:PlaceHolder ID="PlaceHolder2" runat="server" Visible='<%#Eval("Status").ToString()=="-1" %>'>
  25. <tr style="background-color:#FFCC00;padding:5px">
  26. <td><%#Eval("resumeid")%></td>
  27. <td><%#Eval("name")%></td>
  28. <td><%#Eval("experience")%></td>
  29. <td><%#Eval("email")%></td>
  30. <td><%#Eval("phoneno")%></td>
  31. <td><%#Eval("expectedsalary")%></td>
  32. <td>Rejected</td>
  33. </tr>
  34. </asp:PlaceHolder>
  35. </ItemTemplate>
Output of following code is