SHOW:
|
|
- or go back to the newest paste.
1 | using System; | |
2 | using System.Collections.Generic; | |
3 | using System.Linq; | |
4 | using System.Web; | |
5 | using System.Web.UI; | |
6 | using System.Web.UI.WebControls; | |
7 | ||
8 | namespace redditTest | |
9 | { | |
10 | ||
11 | public partial class Students : System.Web.UI.Page | |
12 | { | |
13 | ||
14 | ||
15 | public List<Student> testList = new List<Student>() | |
16 | { | |
17 | new Student("Steve", 5), | |
18 | new Student("Steve", 2), | |
19 | new Student("Steve", 4), | |
20 | new Student("Steve", 1), | |
21 | new Student("Steve", 5), | |
22 | ||
23 | }; | |
24 | protected void Page_Load(object sender, EventArgs e) | |
25 | { | |
26 | ||
27 | } | |
28 | ||
29 | protected void Button1_Click(object sender, EventArgs e) | |
30 | { | |
31 | GridView1.DataSource = testList; | |
32 | GridView1.DataBind(); | |
33 | } | |
34 | ||
35 | protected void Button2_Click(object sender, EventArgs e) | |
36 | { | |
37 | //LINQ way | |
38 | var goodStudent = from c in testList | |
39 | where c.studentGrade <= 4 | |
40 | select c; | |
41 | goodStudent.ToList(); | |
42 | ||
43 | GridView1.DataSource = goodStudent; | |
44 | //GridView1.DataSource = goodList; | |
45 | GridView1.DataBind(); | |
46 | ||
47 | ||
48 | } | |
49 | ||
50 | protected void Button3_Click(object sender, EventArgs e) | |
51 | { | |
52 | List<Student> badList = new List<Student>(); | |
53 | foreach(Student s in testList) | |
54 | { | |
55 | if (s.studentGrade <= 4) | |
56 | { | |
57 | badList.Add(s); | |
58 | } | |
59 | } | |
60 | GridView1.DataSource = badList; | |
61 | GridView1.DataBind(); | |
62 | } | |
63 | } | |
64 | public class Student | |
65 | { | |
66 | public string studentName { get; set; } | |
67 | public int studentGrade { get; set; } | |
68 | ||
69 | public Student(string name, int grade) | |
70 | { | |
71 | studentName = name; | |
72 | studentGrade = grade; | |
73 | } | |
74 | ||
75 | ||
76 | ||
77 | } | |
78 | - | } |
78 | + | } |
79 | ||
80 | //aspx | |
81 | <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Students.aspx.cs" Inherits="redditTest.Students" %> | |
82 | ||
83 | <!DOCTYPE html> | |
84 | ||
85 | <html xmlns="http://www.w3.org/1999/xhtml"> | |
86 | <head runat="server"> | |
87 | <title></title> | |
88 | </head> | |
89 | <body> | |
90 | <form id="form1" runat="server"> | |
91 | <div> | |
92 | <asp:Button ID="Button1" runat="server" Text="Show All Students" OnClick="Button1_Click" /> | |
93 | ||
94 | <asp:Button ID="Button2" runat="server" Text="Show Top Performers" OnClick="Button2_Click" /> | |
95 | ||
96 | <asp:Button ID="Button3" runat="server" Text="Show Dunces" OnClick="Button3_Click" /> | |
97 | ||
98 | <asp:GridView ID="GridView1" runat="server"> | |
99 | </asp:GridView> | |
100 | </div> | |
101 | ||
102 | </form> | |
103 | </body> | |
104 | </html> |