1 sender 指的是触发事件的控件
example:
Button btn=(Button)sender;
btn.Content="你点我了!";
//button2.Content="你点我了";
法一能让两个个按钮出现事件的反应,而法二只能让一个出现
2 常用控件的通用属性
!Visibility控件是否可见:枚举类型:Visible表示可见,Collapsed表示不可见
@IsEnabled:控件是否可用:bool类型;
#Background背景色;
$FontSize字体大小
3Textbox的属性:
1 IsReadOnly="true";只读
2TextWrapping="Wrap";可多行输入
3MaxLength="5";限制输入长度
4 基本控件及其运用代码
1 控件的ID应该遵循良好的命名规范,以便维护
2 超链接控件(HeperLink)
代码如下:
<asp:HyperLink ID="HyperLink1" runat="server" ImageUrl=http://www.shangducms.com/images/cms.jpg //ImageUrl Navlgate 属性
NavigateUrl="http://shangducms.com">
HyperLink</asp:HyperLink>
3 DropDownList控件
代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class kongjiandama : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
bind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void bind()
{
ListItem li = new ListItem();
li.Text = "qq";
li.Value = "qq";
li.Enabled = true;
DropDownList1.Items.Add(li);
ListItem l2=new ListItem();
l2.Text="sohu";
l2.Value="sohu";
li.Enabled=true;
DropDownList1.Items.Add(l2);
}
}方法一
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="qq" Text="qq" Enabled="true"></asp:ListItem>
<asp:ListItem Value="sohu" Text="sohu" Enabled="true"></asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
方法二
可是使用DropDownList控件实现当用户选择不同的值的时候实现超链接怎么办 ??????
4 图像控件(image)
Alternate Text:备用文本
Image Align:图像对齐
ImageUrl:显示图像URl //Image 不支持事件
代码如下:
<asp:Image ID="Image1" runat="server"
AlternateText="图片连接无效" ImageAlign="Left" ImageUrl="~/kongjiandama.aspx"/>
5 文本框控件(TextBox)
1 AutoPostBack(自动回传)属性
2 EnableViewState(控件状态)属性
3 MaxLength(限制字符串长度)
4 ReadOnly(只读)
5 TextMode(单行,多行,密文)
6 按钮控件(Button,LinkButton,ImageButton)
7单选组控件
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="choose"
Text="Choose1" OnCheckedChanged="RadioButton1_CheckedChanged" AutoPostBack="true" />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="choose"
Text="Choose1" OnCheckedChanged="RadioButton1_CheckedChanged" AutoPostBack="true"/>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _520 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void RadioButton2_CheckedChanged1(object sender, EventArgs e)
{
Label1.Text = "第二个被选中";
}
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
Label1.Text = "第一个被选中";
}
}
8 DropDownList 列表控件
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
</asp:DropDownList>
</div>
<asp:Label ID="Label1" runat="server" Text="Label" ></asp:Label>
</form>
</body>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text="你选择了第"+DropDownList1.Text+"项";
}
}
9 ListBox 列表控件
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" SelectionMode="Multiple">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:ListBox>
</form>
</body>
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = "你选择了第" + ListBox1.Text+"项";
}
如果需要实现让用户选择多个 L istBox
项,只需要设置 Select ionMod e 属性为“Mu lt ip le”即可
10BulletedList 列表控件
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:BulletedList ID="BulletedList1" runat="server" OnClick="BulletedList1_Click">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:BulletedList>
</form>
</body>
protected void BulletedList1_Click(object sender, BulletedListEventArgs e)
{
Label1.Text = "你选择了第" + BulletedList1.Text + "项";
}
11 日历控件(Calendar)
<body>
<form id="form1" runat="server">
<asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="#999999" CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px" OnSelectionChanged="Calendar1_SelectionChanged" Width="200px">
<DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
<NextPrevStyle VerticalAlign="Bottom" />
<OtherMonthDayStyle ForeColor="#808080" />
<SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
<SelectorStyle BackColor="#CCCCCC" />
<TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
<TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
<WeekendDayStyle BackColor="#FFFFCC" />
</asp:Calendar>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Label1.Text=
"现在的时间是:"+Calendar1.SelectedDate.Year.ToString()+"年"
+Calendar1.SelectedDate.Month.ToString()+"月"
+Calendar1.SelectedDate.Day.ToString()+"号"
+Calendar1.SelectedDate.Hour.ToString()+"点";
}
12 表单验证控件(RequiredFieldValidator)
<body>
<form id="form1" runat="server">
姓名:<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate=" TextBox1" ErrorMessage="必填字段不能为空">
</asp:RequiredFieldValidator>
<br/>
密码:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<br />
</form>
</body>
13 比较验证控件(CompareValidator)
<body>
<form id="form1" runat="server">
<div>
请输入生日:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
毕业日期:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="TextBox2" ControlToValidate="TextBox1"
CultureInvariantValues="True" ErrorMessage="输入格式错误!请改正!"
Operator="GreaterThan"
Type="Date">
</asp:CompareValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<br />
</div>
</form>
</body>
14 范围验证控件(RangeValidator)
<html>
<head>
<title></title>
</head>
<body>
请输入生日:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="超出规定范围,请重新填写"
MaximumValue="2009/1/1" MinimumValue="1990/1/1" Type="Date">
</asp:RangeValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</body>
</html>
15 验证组控件(ValidationSummary)
<body>
<form id="form1" runat="server">
姓名:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="姓名为必填项">
</asp:RequiredFieldValidator>
<br />
身份证:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="身份证号码错误"
ValidationExpression="\d{17}[\d|X]|\d{15}"></asp:RegularExpressionValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
</form>
</body>
16 导航控件
<html>
<head>
<title></title>
</head>
<body>
<asp:Menu ID="Menu1" runat="server">
<Items>
<asp:MenuItem Text="新建项" Value="新建项"></asp:MenuItem>
<asp:MenuItem Text="新建项" Value="新建项">
<asp:MenuItem Text="新建项" Value="新建项"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="新建项" Value="新建项">
<asp:MenuItem Text="新建项" Value="新建项"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="新建项" Value="新建项">
<asp:MenuItem Text="新建项" Value="新建项">
<asp:MenuItem Text="新建项" Value="新建项"></asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="新建项" Value="新建项"></asp:MenuItem>
</Items>
</asp:Menu>
</body>
</html>
17 登录控件(Login)
<body>
<form id="form1" runat="server">
<div>
<asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="10pt">
<LayoutTemplate>
<table cellpadding="1" cellspacing="0" style="border-collapse:collapse;">
<tr>
<td>
<table cellpadding="0">
<tr>
<td align="center" colspan="2">登录</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名:</asp:Label>
</td>
<td>
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="必须填写“用户名”。" ToolTip="必须填写“用户名”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密码:</asp:Label>
</td>
<td>
<asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="必须填写“密码”。" ToolTip="必须填写“密码”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:CheckBox ID="RememberMe" runat="server" Text="下次记住我。" />
</td>
</tr>
<tr>
<td align="center" colspan="2" style="color:Red;">
<asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
</td>
</tr>
<tr>
<td align="right" colspan="2">
<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="登录" ValidationGroup="Login1" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
<TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />
</asp:Login>
</div>
</form>
</body>
18 登录状态控件(LoginStatus)
<asp:LoginStatus ID="LoginStatus1" runat="server" />
19 密码更改控件(ChangePassword)
<body>
<form id="form1" runat="server">
<div>
<asp:ChangePassword ID="ChangePassword1" runat="server">
<ChangePasswordTemplate>
<table cellpadding="1" cellspacing="0" style="border-collapse:collapse;">
<tr>
<td>
<table cellpadding="0">
<tr>
<td align="center" colspan="2">更改密码</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="CurrentPasswordLabel" runat="server" AssociatedControlID="CurrentPassword">密码:</asp:Label>
</td>
<td>
<asp:TextBox ID="CurrentPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="CurrentPasswordRequired" runat="server" ControlToValidate="CurrentPassword" ErrorMessage="必须填写“密码”。" ToolTip="必须填写“密码”。" ValidationGroup="ChangePassword1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="NewPasswordLabel" runat="server" AssociatedControlID="NewPassword">新密码:</asp:Label>
</td>
<td>
<asp:TextBox ID="NewPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="NewPasswordRequired" runat="server" ControlToValidate="NewPassword" ErrorMessage="必须填写“新密码”。" ToolTip="必须填写“新密码”。" ValidationGroup="ChangePassword1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="ConfirmNewPasswordLabel" runat="server" AssociatedControlID="ConfirmNewPassword">确认新密码:</asp:Label>
</td>
<td>
<asp:TextBox ID="ConfirmNewPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="ConfirmNewPasswordRequired" runat="server" ControlToValidate="ConfirmNewPassword" ErrorMessage="必须填写“确认新密码”。" ToolTip="必须填写“确认新密码”。" ValidationGroup="ChangePassword1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:CompareValidator ID="NewPasswordCompare" runat="server" ControlToCompare="NewPassword" ControlToValidate="ConfirmNewPassword" Display="Dynamic" ErrorMessage="“确认新密码”与“新密码”项必须匹配。" ValidationGroup="ChangePassword1"></asp:CompareValidator>
</td>
</tr>
<tr>
<td align="center" colspan="2" style="color:Red;">
<asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
</td>
</tr>
<tr>
<td align="right">
<asp:Button ID="ChangePasswordPushButton" runat="server" CommandName="ChangePassword" Text="更改密码" ValidationGroup="ChangePassword1" />
</td>
<td>
<asp:Button ID="CancelPushButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</ChangePasswordTemplate>
</asp:ChangePassword>
</div>
</form>
</body>