aspx小集合

时间:2021-07-21 23:27:18

1.注意GroupName

<asp:RadioButton ID="rdoF" runat="server" Text="男" Checked="true"
GroupName="sex" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:RadioButton ID="rdoM"
runat="server" Text="女" GroupName="sex" />

2.SqlTransaction

conn = new SqlConnection(SqlConn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
SqlTransaction tran = null;
//根据EmpID判断是否是修改操作还是新建操作
if (EmpID == "")
{
try
{
conn.Open();
//添加数据库事务
tran = conn.BeginTransaction();
// 绑定事务
cmd.Transaction = tran;

string strSqlText = "insert into dbo.Employee values('" + txtName.Text + "','" + rdoF.Checked + "','" + DateTime.Parse(txtBirth.Value) + "'," + int.Parse(ddlC.SelectedValue) + ",'" + txtAddress.Text + "','" + txtEmail.Text + "','" + txtRemark.Text + "')";
cmd.CommandText = strSqlText;
cmd.CommandType = CommandType.Text;
//提交人员注册的信息
int i = cmd.ExecuteNonQuery();
//获取新增的人员id
cmd.CommandText = "select E_Id from dbo.Employee where E_Name='" + txtName.Text + "' and E_Brith='" + txtBirth.Value + "' and E_Address='" + txtAddress.Text + "'";
SqlDataReader sr = cmd.ExecuteReader();
//新增的人员id
int E_ID = 0;
while (sr.Read())
{
E_ID = sr.GetInt32(0);
}
sr.Close();
//新增人员登录的账号和密码
cmd.CommandText = "insert into dbo.Emp_Submit values(" + E_ID + ",'" + txtAccount.Text + "','" + txtPwd1.Text + "')";
i += cmd.ExecuteNonQuery();
//提交事务
tran.Commit();
if (i > 1)
{
//成功后的提示信息
Response.Write("<script>alert('注册成功," + txtName.Text + "欢迎进入本站!')</script>");
txtNull();
lblNull();
}
else
{
Response.Write("<script>alert('注册失败!')</script>");
}
}
catch (Exception ex)
{
//回滚事务
tran.Rollback();
Response.Write("<script>alert('" + ex.Message + "')</script>");
}
finally
{
conn.Close();
}

3.

Ø在ASP.NET中,控件分成HTML服务控件和Web服务控件两种
Ø所有的 ASP.NET Web 服务器控件都派生自 System.Web.UI.WebControls 命名空间
Ø使用Web服务器控件需要注意AutoPostBack的使用
•HTML 服务器控件是由 ASP.NET 更新的标准 HTML 标签,通过添加 runat=“server” 属性将其用作服务器控件
HTML 服务器控件属于 System.Web.UI.HTMLControls 命名空间,派生自 HTMLControl 基类
 
4.浏览器向用户显示一个窗体,用户与该窗体进行交互,这导致该窗体回发到服务器

因为与服务器组件进行交互的所有处理必须在服务器上发生,这意味着对于要求处理的每一操作而言,必须将该窗体发送到服务器、进行处理、然后返回到浏览器。

5.

if((img.fileSize/1024)>500)
                            {
                                alert("你选择的第"+(i+1)+"个图片过大,必须在500kb以内!");return false;
                            }

6.批量上传

HttpFileCollection hfc = this.Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
//Response.Write("<script>alert('" + hfc[i].FileName + "')</script>");
string fileUrl = hfc[i].FileName;
if (fileUrl == "")
{
continue;
}
//截取最后从双斜杠开始到结束的字符创(截取文件名称和后缀)
fileUrl = fileUrl.Substring(fileUrl.LastIndexOf("\\") + 1);
//服务器保存的路径
fileUrl = this.MapPath("userUpLoad\\") + fileUrl;
//小文件上传
hfc[i].SaveAs(fileUrl);
}
Response.Write("<script>alert('批量上传成功!')</script>");

7.

文本框不能为空验证

<tr>
<td class="style1">姓名:</td>
<td class="style1">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
<td class="style1">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtName" ErrorMessage="姓名不能为空" Display="None"></asp:RequiredFieldValidator>
</td>
</tr>

比较验证,比如密码

<tr>
<td>密码:</td>
<td>
<asp:TextBox ID="txtPwd1" runat="server" TextMode="Password"></asp:TextBox>
</td>
<td></td>
</tr>
<tr>
<td>确认密码:</td>
<td>
<asp:TextBox ID="txtPwd2" runat="server" TextMode="Password"></asp:TextBox>
</td>
<td>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="txtPwd1" ControlToValidate="txtPwd2"
ErrorMessage="密码与确认密码不一致!"></asp:CompareValidator>
</td>
</tr>

出生日期不得低于某个固定日期

<tr>
<td class="style1">出生日期:</td>
<td class="style1">
<asp:TextBox ID="txtBirth" runat="server"></asp:TextBox>
</td>
<td class="style1">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtBirth" Display="None" ErrorMessage="出生日期不能为空!"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server"
ControlToValidate="txtBirth" ErrorMessage="出生日期不匹配!" Operator="LessThanEqual"
Type="Date" ValueToCompare="2012-11-15"></asp:CompareValidator>
</td>
</tr>

输入的必须是整数

<tr>
<td>工作年限:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server">0</asp:TextBox>
</td>
<td>
<asp:CompareValidator ID="CompareValidator3" runat="server"
ControlToValidate="TextBox2" ErrorMessage="输入的值必须是数字" Operator="DataTypeCheck"
Type="Integer"></asp:CompareValidator>
</td>
</tr>

数值必须处于某个范围内

<tr>
<td class="style1">账号:</td>
<td class="style1">
<asp:TextBox ID="txtAccount" runat="server"></asp:TextBox>
</td>
<td class="style1">
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="txtAccount" ErrorMessage="必须是在1000到10000之间的数字"
MaximumValue="10000" MinimumValue="1000" Type="Integer"></asp:RangeValidator>
</td></tr>

验证电子邮箱的格式是否正确

<tr>
<td class="style1">电子邮件:</td>
<td class="style1">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
<td class="style1">
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtEmail" ErrorMessage="电子邮件的值不匹配!"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>

该控件收集本页的所有验证错误信息,并可以将它们组织以后再显示出来

<tr>
<td colspan="2">
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
DisplayMode="List" />
</td>
<td>&nbsp;</td>
</tr>

CustomValidator(自定义验证)控件

<td>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBox3" ErrorMessage="验证码不正确!"
onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
</td>

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (TextBox3.Text == "1234")
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}

args.IsValid 为ture则不报错,若false则报ErrorMessage="验证码不正确!"

验证控件请参考 http://www.51cto.com/specbook/14/3305.htm