Image1.ImageUrl = "~/img/Lucy.png";
textbox控件:用于输入;
label控件:用于显示信息;
上传控件FileUpload的使用:
protected void btnUpload_Click(object sender, EventArgs e)
{
bool fileOK = false;
string path = Server.MapPath("~/uploadimage/");
if (FileUpload1.HasFile)
{
string fileExtension=System.IO.Path.GetExtension(FileUpload1.FileName.ToLower());
string[] allowedExtensions = { ".gif",".jpg",".png",".bmp"};
foreach (string k in allowedExtensions)
{
if (fileExtension == k)
{
fileOK = true;
}
}
}
if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
info.Text = "文件上传成功!<br>" + "上传文件名为" + FileUpload1.PostedFile.FileName
+ "<br>文件大小为:" + FileUpload1.PostedFile.ContentLength / 1024 + "KB" + "<br>文件的MIME类型为:" + FileUpload1.PostedFile.ContentType
;
}
catch (Exception ex)
{
info.Text = "文件上传失败,原因:" + ex.Message;
}
}
else
{
info.Text = "不可接受的文件类型";
}
}
CompareValidator控件的使用(优先显示标签之间的内容,然后出错时才显示ErrorMessage):
<HTML>
<HEAD>
<title>比较验证器</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="CV1" method="post" runat="server">
<asp:Label id="lblId" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 52px" runat="server"
Width="80px" Height="22px">输入 ID:</asp:Label>
<asp:TextBox id="txtId" style="Z-INDEX: 102; LEFT: 112px; POSITION: absolute; TOP: 52px" runat="server"
Width="177px" Height="23px"></asp:TextBox>
<asp:Label id="lblSdate" style="Z-INDEX: 103; LEFT: 8px; POSITION: absolute; TOP: 91px" runat="server"
Width="80px" Height="27px">起始日期:</asp:Label>
<asp:TextBox id="txtSdate" style="Z-INDEX: 104; LEFT: 112px; POSITION: absolute; TOP: 95px" runat="server"
Width="176px" Height="22px"></asp:TextBox>
<asp:Label id="lblEdate" style="Z-INDEX: 105; LEFT: 8px; POSITION: absolute; TOP: 129px" runat="server"
Width="88px" Height="24px">截止日期:</asp:Label>
<asp:TextBox id="txtEdate" style="Z-INDEX: 106; LEFT: 112px; POSITION: absolute; TOP: 133px"
runat="server" Width="175px" Height="25px"></asp:TextBox>
<asp:Button id="btnSubmit" style="Z-INDEX: 107; LEFT: 112px; POSITION: absolute; TOP: 176px"
runat="server" Width="88px" Height="31px" Text="提交"></asp:Button>
<asp:Label id="lblMessage" style="Z-INDEX: 108; LEFT: 8px; POSITION: absolute; TOP: 232px"
runat="server" Width="292px" Height="33px"></asp:Label>
<asp:CompareValidator id="ComVal1" style="Z-INDEX: 109; LEFT: 320px; POSITION: absolute; TOP: 57px" runat="server"
Width="242px" Height="22px" ErrorMessage="ID 无效" ControlToValidate="txtId" Display="Dynamic" Operator="DataTypeCheck"
Type="Integer">ID不对吧</asp:CompareValidator>
<asp:CompareValidator id="ComVal2" style="Z-INDEX: 110; LEFT: 320px; POSITION: absolute; TOP: 99px" runat="server"
Width="139px" Height="24px" ErrorMessage="日期无效" ControlToValidate="txtSdate" Operator="DataTypeCheck"
Type="Date">日期不对吧</asp:CompareValidator>
<asp:CompareValidator id="ComVal3"
style="Z-INDEX: 111; LEFT: 320px; POSITION: absolute; TOP: 132px" runat="server"
Width="233px" Height="20px" ErrorMessage="截止日期应迟于起始日期!!" ControlToValidate="txtEdate" ControlToCompare="txtSdate"
Display="Dynamic" Operator="GreaterThan" Type="Date" BorderStyle="Inset"></asp:CompareValidator>
</form>
</body>
</HTML>
后台代码:
private void Button1_Click(object sender, System.EventArgs e)
{
if (ComVal1.IsValid && ComVal2.IsValid && ComVal3.IsValid)
{
lblMessage.Text = "恭喜!!";
}
else
lblMessage.Text ="";
}
CompareValidator的属性: | CustomVlidator控件的属性 |
![]() | ![]() |
customvalidator控件使用:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label">请输入系统密码:</asp:Label>
<asp:TextBox ID="txtPWD" runat="server"></asp:TextBox><asp:CustomValidator ID="CustomValidator1"
runat="server" ErrorMessage="密码不正确哦" ControlToValidate="txtPWD"
onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
</div>
<asp:Button ID="Button1" runat="server" Height="21px" onclick="Button1_Click"
style="margin-left: 118px" Text="登录" Width="121px" />
<br /><asp:Label ID="Label2" runat="server"
Text="Label"></asp:Label>
</form>
</body>
</html>
后台代码:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
string strval = args.Value.ToLower();
if (strval.Equals("admin"))
{
args.IsValid = true;
}
else
args.IsValid = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (CustomValidator1.IsValid)
{
Label2.Text = "恭喜登录成功!";
}
else {
Label2.Text = "登录失败";
}
}
RegularExpressionValidator控件使用:
输出照片流,绘制图片:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace Asp.NetTeaPro2012.ch05.experiment
{
public partial class writeImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "Image/JPEG"; //"text/html";
using (Bitmap bitmap = new Bitmap(500, 400))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
//......
// g.
g.DrawString("IP地址是:" + Request.UserHostAddress, new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Chocolate, 0, 0);
g.DrawString("操作系统:" + Context.Request.Browser.Platform, new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red, 0, 50);
g.DrawString("浏览器信息:" + Context.Request.Browser.Type, new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red, 0, 100);
g.DrawString(":"+Request.CurrentExecutionFilePath,new System.Drawing.Font("Arial",40),System.Drawing.Brushes.Cyan,50,150);
}
bitmap.Save(Context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
绘制出来的图片:
水印例子代码:
Context.Response.ContentType = "Image/JPEG";
string name = Request["Name"];
string fullPath = Request.MapPath("dd.jpg");
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fullPath))
{
int x = bitmap.Width/4;
int y = bitmap.Height-50;
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
g.DrawString(name, new System.Drawing.Font("宋体", 40), System.Drawing.Brushes.SkyBlue, x, y);
}
bitmap.Save(Context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
如果传入的Name=nicolas的话http://localhost:1043/receiveName.aspx?Name=nicolas