asp.net技巧点滴(一)
2006-04-17 22:40 Clingingboy 阅读(1713) 评论(0) 编辑 收藏 举报1.使用快捷键
1
<p>
2
<asp:Label ID="Label1" Runat="server" AccessKey="N"
3
AssociatedControlID="Textbox1">User<u>n</u>ame</asp:Label>
4
<asp:Textbox ID="TextBox1" Runat="server"></asp:Textbox></p>
5
<p>
6
<asp:Label ID="Label2" Runat="server" AccessKey="P"
7
AssociatedControlID="Textbox2"><u>P</u>assword</asp:Label>
8
<asp:Textbox ID="TextBox2" Runat="server"></asp:Textbox></p>
9
<p>
10
<asp:Button ID="Button1" Runat="server" Text="确定" />
11
</p>

2

3

4

5

6

7

8

9

10

11

使用 AssociatedControlID 属性将 Web 窗体上的一个 Label 控件与另一个服务器控件关联。当一个 Label 控件与另一个服务器控件相关联时,可以使用其属性扩展关联控件的功能。可以使用 Label 控件作为另一个控件的标题,或者可以设置关联控件的 Tab 键索引或热键
2.TextChanged 事件 当用户更改 TextBox 的文本时发生。
1
protected void TextBox1_TextChanged(object sender, EventArgs e)
2
{
3
Response.Write("更改");
4
}
5
6
protected void Button1_Click(object sender, EventArgs e)
7
{
8
Response.Write("没有更改");
9
}

2

3

4

5

6

7

8

9

3.使用Command触发事件
1
<%@ Page Language="C#" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<script runat="server">
6
protected void Button_Command(Object sender,
7
System.Web.UI.WebControls.CommandEventArgs e)
8
{
9
switch (e.CommandName)
10
{
11
case ("DoSomething1"):
12
Response.Write("Button 1 was selected");
13
break;
14
case ("DoSomething2"):
15
Response.Write("Button 2 was selected");
16
break;
17
}
18
}
19
</script>
20
21
<html xmlns="http://www.w3.org/1999/xhtml" >
22
<head id="Head1" runat="server">
23
<title>Buttons</title>
24
</head>
25
<body>
26
<form id="form1" runat="server">
27
<div>
28
<asp:Button ID="Button1" Runat="server" Text="Button 1"
29
OnCommand="Button_Command" CommandName="DoSomething1" />
30
<asp:Button ID="Button2" Runat="server" Text="Button 2"
31
OnCommand="Button_Command" CommandName="DoSomething2" />
32
</div>
33
</form>
34
</body>
35
</html>

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

4.使用OnClientClick事件,返回客户端脚本
1
<%@ Page Language="C#" %>
2
3
<script runat="server">
4
protected void Button1_Click(object sender, EventArgs e)
5
{
6
Response.Write("Postback!");
7
}
8
</script>
9
10
<script language="javascript" type="text/javascript">
11
function AlertHello()
12
{
13
alert('Hello ASP.NET');
14
}
15
</script>
16
17
<html xmlns="http://www.w3.org/1999/xhtml" >
18
<head id="Head1" runat="server">
19
<title>Button Server Control</title>
20
</head>
21
<body>
22
<form id="form1" runat="server">
23
<asp:Button ID="Button1" Runat="server" Text="Button"
24
OnClientClick="AlertHello()" OnClick="Button1_Click" />
25
</form>
26
</body>
27
</html>
28

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

5.使用控件数据绑定数组
1
<%@ Page Language="C#" %>
2
3
<script runat="server">
4
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
5
{
6
string[] CarArray = new string[4] {"Ford", "Honda", "BMW", "Dodge"};
7
string[] AirplaneArray = new string[3] {"Boeing 777", "Boeing 747",
8
"Boeing 737"};
9
string[] TrainArray = new string[3] {"Bullet Train", "Amtrack", "Tram"};
10
11
if (DropDownList1.SelectedValue == "Car") {
12
DropDownList2.DataSource = CarArray; }
13
else if (DropDownList1.SelectedValue == "Airplane") {
14
DropDownList2.DataSource = AirplaneArray; }
15
else {
16
DropDownList2.DataSource = TrainArray;
17
}
18
19
DropDownList2.DataBind();
20
DropDownList2.Visible = true;
21
}
22
23
protected void Button1_Click(object sender, EventArgs e)
24
{
25
Response.Write("You selected <b>" +
26
DropDownList1.SelectedValue.ToString() + ": " +
27
DropDownList2.SelectedValue.ToString() + "</b>");
28
}
29
</script>
30
31
32
<html xmlns="http://www.w3.org/1999/xhtml" >
33
<head id="Head1" runat="server">
34
<title>DropDownList Page</title>
35
</head>
36
<body>
37
<form id="form1" runat="server">
38
<div>
39
Select transportation type:<br />
40
<asp:DropDownList ID="DropDownList1" Runat="server"
41
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
42
AutoPostBack="true">
43
<asp:ListItem>Select an Item</asp:ListItem>
44
<asp:ListItem>Car</asp:ListItem>
45
<asp:ListItem>Airplane</asp:ListItem>
46
<asp:ListItem>Train</asp:ListItem>
47
</asp:DropDownList>
48
<asp:DropDownList ID="DropDownList2" Runat="server" Visible="false">
49
</asp:DropDownList>
50
<asp:Button ID="Button1" Runat="server" Text="Select Options"
51
OnClick="Button1_Click" />
52
</div>
53
</form>
54
</body>
55
</html>
56

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

6.使用ListItem.Enabled 禁用列表项
1
<%@ Page Language="C#" %>
2
3
<script runat="server">
4
5
protected void Dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
6
{
7
Response.Write("You selected item number " +
8
DropDownList1.SelectedValue + "<br>");
9
Response.Write("You didn't select item number " +
10
DropDownList1.Items[1].Value);
11
}
12
</script>
13
14
<html>
15
<head id="Head1" runat="server">
16
<title>DropDownList Server Control</title>
17
</head>
18
<body>
19
<form id="form1" runat="server">
20
<asp:DropDownList ID="DropDownList1" Runat="server" AutoPostBack="True" OnSelectedIndexChanged="Dropdownlist1_SelectedIndexChanged">
21
<asp:ListItem Value="1">First Choice</asp:ListItem>
22
<asp:ListItem Value="2" Enabled="False">Second Choice</asp:ListItem>
23
<asp:ListItem Value="3">Third Choice</asp:ListItem>
24
</asp:DropDownList>
25
</form>
26
</body>
27
</html>
28

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

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现