1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Collections;
11
using System.Data.SqlClient;
12
using System.IO;
13
14
public partial class _Default : System.Web.UI.Page
15
{
16
protected void Page_Load(object sender, EventArgs e)
17
{
18
//构造数据源
19
//字符串数组
20
string[] DataSource1 = new string[] {"项目1","项目2","项目3" };
21
//哈西表
22
Hashtable DataSource2 = new Hashtable(3);
23
DataSource2.Add("项目1", "值1");
24
DataSource2.Add("项目2", "值2");
25
DataSource2.Add("项目3", "值3");
26
//数据集
27
DataSet DataSource3 = new DataSet();
28
string connString="server=.\\sqlexpress;database=Clients;uid=sa;pwd=123456;";
29
using (SqlConnection conn=new SqlConnection(connString))
30
{
31
SqlDataAdapter sda = new SqlDataAdapter("select * from OrderClient",conn);
32
sda.Fill(DataSource3);
33
}
34
//目录集合
35
DirectoryInfo di = new DirectoryInfo("D:\\");
36
DirectoryInfo[] DataSource4 = di.GetDirectories();
37
//动态数组
38
ArrayList DataSource5 = new ArrayList();
39
DataSource5.Add(new Link("谷歌","http://www.google.com"));
40
DataSource5.Add(new Link("百度","http://www.baidu.com"));
41
42
if (!IsPostBack)
43
{
44
//第一次请求
45
//绑定数据
46
DropDownList1.DataSource = DataSource1;
47
DropDownList1.DataBind();
48
DropDownList1.Items.Insert(0, "请选择");//放在DataBind方法之后,不然会被数据绑定覆盖
49
50
ListBox1.DataSource = DataSource2;
51
ListBox1.DataTextField = "key";
52
ListBox1.DataValueField = "value";
53
ListBox1.DataBind();
54
55
56
CheckBoxList1.DataSource = DataSource3;
57
CheckBoxList1.DataTextField = "ClientName";
58
CheckBoxList1.DataTextFormatString = "分类;{0}";
59
CheckBoxList1.DataValueField = "ClientID";
60
CheckBoxList1.DataBind();
61
62
RadioButtonList1.DataSource = DataSource4;
63
RadioButtonList1.DataTextField = "Name";
64
RadioButtonList1.DataValueField = "FullName";
65
RadioButtonList1.DataBind();
66
67
68
BulletedList1.DataSource = DataSource5;
69
BulletedList1.DataTextField = "Name";
70
BulletedList1.DataValueField = "Url";
71
BulletedList1.DataBind();
72
}
73
74
}
75
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
76
{
77
Response.Write(DropDownList1.SelectedValue);
78
}
79
protected void Button1_Click(object sender, EventArgs e)
80
{
81
foreach (ListItem li in ListBox1.Items)
82
{
83
if (li.Selected)
84
{
85
Response.Write(string.Format("{0}:{1}<br>",li.Text,li.Value));
86
}
87
}
88
}
89
protected void Button2_Click(object sender, EventArgs e)
90
{
91
foreach (ListItem li in CheckBoxList1.Items)
92
{
93
li.Selected = true;
94
}
95
}
96
protected void Button3_Click(object sender, EventArgs e)
97
{
98
foreach (ListItem li in CheckBoxList1.Items)
99
{
100
li.Selected = false;
101
}
102
}
103
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
104
{
105
Response.Write(RadioButtonList1.SelectedValue);
106
}
107
}
108
109
110
111
112
113
114
115
116
using System;
117
using System.Data;
118
using System.Configuration;
119
using System.Web;
120
using System.Web.Security;
121
using System.Web.UI;
122
using System.Web.UI.WebControls;
123
using System.Web.UI.WebControls.WebParts;
124
using System.Web.UI.HtmlControls;
125
126
/// <summary>
127
/// Link 的摘要说明
128
/// </summary>
129
public class Link
130
{
131
public Link(string Name,string Url)
132
{
133
this.Name = Name;
134
this.Url = Url;
135
}
136
137
private string name;
138
139
public string Name
140
{
141
get { return name; }
142
set { name = value; }
143
}
144
145
private string url;
146
147
public string Url
148
{
149
get { return url; }
150
set { url = value; }
151
}
152
}
153

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153
