Working with ListView

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

 

Ebook Download
View all
Learn
View all