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
- /--This is class Resume--/
- public class Resume
- {
- public int resumeid { get; set; }
- public string name { get; set; }
- public string experience { get; set; }
- public string email { get; set; }
- public string phoneno { get; set; }
- public string expectedsalary { get; set; }
- public int status { get; set; }
-
- public Resume(int Resumeid, string Name, string Experience, string
Email, string Phoneno, string Expectedsalary, int Status) - {
- resumeid = Resumeid;
- name = Name;
- experience = Experience;
- email = Email;
- phoneno = Phoneno;
- expectedsalary = Expectedsalary;
- status=Status;
- }
- }
-
- /-- This is bind method--/
- private void BindResumes()
- {
-
- List<Resume> resumes = new List<Resume>();
- Resume r1 = new Resume(1, "Jay Raval", "1.5 years", "[email protected]",
"123-456-789", "2.5 per annum", 1); - Resume r2 = new Resume(2, "Parth Bhavasar", "1 years", "[email protected]",
"123-789-456", "2 per annum", 1); - Resume r3 = new Resume(3, "Bhavik Patel", "3.5 years", "[email protected]",
"456-123-789", "3.5 per annum", 1); - Resume r4 = new Resume(4, "Jatin Saxena", "5 years", "[email protected]",
"143-456-789", "4.5 per annum", 0); - Resume r5 = new Resume(5, "Nitin Patel", "1.5 years", "[email protected]",
"153-456-789", "2.5 per annum", 1); - Resume r6 = new Resume(6, "Ravi Patel", "5 months", "[email protected]",
"163-456-789", "5.5 per annum", 0); - Resume r7 = new Resume(7, "Vinit Sonara", "2 monts", "[email protected]",
"173-456-789", "1.5 per annum", -1); - Resume r8 = new Resume(8, "Anil Motwani", "2.5 years", "[email protected]",
"183-456-789", "3.5 per annum", 1); - Resume r9 = new Resume(9, "Hardik Raval", "1.2 years", "[email protected]",
"193-456-789", "2.5 per annum", 1); - Resume r10 = new Resume(10, "Deep Trivedi", "1 years", "[email protected]",
"113-456-789", "3.5 per annum", -1); -
- resumes.Add(r1);
- resumes.Add(r2);
- resumes.Add(r3);
- resumes.Add(r4);
- resumes.Add(r5);
- resumes.Add(r6);
- resumes.Add(r7);
- resumes.Add(r8);
- resumes.Add(r9);
- resumes.Add(r10);
- lstViewResumes.DataSource = resumes;
- lstViewResumes.DataBind();
- }
/-- This is aspx page--/
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title></title>
- </head>
- <body>
-
- </body>
- </html>
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestList.aspx.cs" Inherits="UserPages_TestList" Title="ListView Example" %>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h1 align="center">List View Example</h1>
- <asp:ListView ID="lstViewResumes" runat="server">
- <layouttemplate>
- <table border=1 align="center" style="background-color:white;" cellpadding="3px" cellspacing="1px">
- <thead style="background-color:Silver">
- <td>Id</td>
- <td>Name</td>
- <td>Experience</td>
- <td>Email</td>
- <td>Phoneno</td>
- <td>Expected Salary</td>
- <td>Status</td>
- </thead>
- <tbody style="background-color:#f9f9f9">
- <asp:PlaceHolder ID="itemplaceholder" runat="server"></asp:PlaceHolder>
- </tbody>
- </table>
- </layouttemplate>
- <itemtemplate>
- <tr style="background-color:#99FFCC;padding:5px">
- <td><%#Eval("resumeid")%></td>
- <td><%#Eval("name")%></td>
- <td><%#Eval("experience")%></td>
- <td><%#Eval("email")%></td>
- <td><%#Eval("phoneno")%></td>
- <td><%#Eval("expectedsalary")%></td>
- <td>Active</td>
- </tr>
- </itemtemplate>
- </asp:ListView>
- </div>
- </form>
- </body>
- </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
- <asp:PlaceHolder runat="server" Visible='Condition'>
- --template--
- </asp:PlaceHolder>
If I Just want to show ‘Active’ records means ‘status’ is 1
Then following changes are done in <ItemTemplate>
- <ItemTemplate>
- <asp:PlaceHolder runat="server" Visible='<%#Eval("Status").ToString()=="1" %>'>
- <tr style="background-color:#99FFCC;padding:5px">
- <td><%#Eval("resumeid")%></td>
- <td><%#Eval("name")%></td>
- <td><%#Eval("experience")%></td>
- <td><%#Eval("email")%></td>
- <td><%#Eval("phoneno")%></td>
- <td><%#Eval("expectedsalary")%></td>
- <td>Active</td>
- </tr>
- </asp:PlaceHolder>
- </ItemTemplate>
Output of following code is
Now we will show Resumes with all ‘status’
So we have to do following changes in <ItemTemplate>
- <ItemTemplate>
- <asp:PlaceHolder runat="server" Visible='<%#Eval("Status").ToString()=="1" %>'>
- <tr style="background-color:#99FFCC;padding:5px">
- <td><%#Eval("resumeid")%></td>
- <td><%#Eval("name")%></td>
- <td><%#Eval("experience")%></td>
- <td><%#Eval("email")%></td>
- <td><%#Eval("phoneno")%></td>
- <td><%#Eval("expectedsalary")%></td>
- <td>Active</td>
- </tr>
- </asp:PlaceHolder>
- <asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%#Eval("Status").ToString()=="0" %>'>
- <tr style="background-color:#db3000;padding:5px">
- <td><%#Eval("resumeid")%></td>
- <td><%#Eval("name")%></td>
- <td><%#Eval("experience")%></td>
- <td><%#Eval("email")%></td>
- <td><%#Eval("phoneno")%></td>
- <td><%#Eval("expectedsalary")%></td>
- <td>Deleted</td>
- </tr>
- </asp:PlaceHolder>
- <asp:PlaceHolder ID="PlaceHolder2" runat="server" Visible='<%#Eval("Status").ToString()=="-1" %>'>
- <tr style="background-color:#FFCC00;padding:5px">
- <td><%#Eval("resumeid")%></td>
- <td><%#Eval("name")%></td>
- <td><%#Eval("experience")%></td>
- <td><%#Eval("email")%></td>
- <td><%#Eval("phoneno")%></td>
- <td><%#Eval("expectedsalary")%></td>
- <td>Rejected</td>
- </tr>
- </asp:PlaceHolder>
- </ItemTemplate>
Output of following code is