public void ProcessRequest (HttpContext context) {
string modelPath = context.Server.MapPath("LoginModel.htm");
string htmlSendBack = System.IO.File .ReadAllText(modelPath);
context.Response.ContentType = "text/plain";
context.Response.Write(htmlSendBack);
if (!string .IsNullOrEmpty(context.Request.Form[ "txtName"]))
{
if (context.Request.Form["txtName" ] == "zhu" &&
context.Request.Form[ "txtPassword"] == "123" )
{
context.Response.Write( "登录成功!" );
}
else
context.Response.Write( "登录失败!" );
}
}
<form action="login.ashx" method="post">
<input type="text" name="txtname"/>
<input type="password" name="txtpwd"/>
</form>
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
System.Text. StringBuilder sbHTML = new System.Text.StringBuilder();
sbHTML.Append( "<html><head><title>登录页面</title></head><body><form action='03Login.ashx' method='post'>");
sbHTML.Append( "用户名:<input type='text' name='txtname' /> <br />" );
sbHTML.Append( "密码:<input type='password' name='txtpwd' /> <br/>" );
sbHTML.Append( "<input type='submit' value='登录'/><a href='04Reg.ashx?a=222'>注册</a> <br/>");
sbHTML.Append( "</form></body></html>" );
context.Response.Write(sbHTML.ToString());
//获得浏览器表单post方式传递来的值
string strUserName = context.Request.Form["txtname"];
if (!string .IsNullOrEmpty(strUserName))
{
context.Response.Write( "Form中的值:" + strUserName);
}
//获得浏览器表单get方式传递来的值
string strUserNameGet = context.Request.QueryString["txtname"];
if (!string .IsNullOrEmpty(strUserNameGet))
{
context.Response.Write( "Url中得到的值:" + strUserNameGet);
}
}
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string strNum1 = context.Request.Form["txtNum1" ];
string strNum2 = context.Request.Form["txtNum2" ];
//判断是否格式正确
string result = "0" ;
int num1 = 0, num2 = 0;
if (!string .IsNullOrEmpty(strNum1) && ! string.IsNullOrEmpty(strNum2))
{
if (int .TryParse(strNum1, out num1) && int.TryParse(strNum2, out num2))
{
result = (num1+num2).ToString();
}
else
{
result = "输入格式错误" ;
}
}
System.Text. StringBuilder sbHTML = new System.Text.StringBuilder();
sbHTML.Append( "<!DOCTYPE><html><head><title>计算器</title></head><body><form action='06Calculate.ashx' method='post'>");
sbHTML.Append( "<input type='text' name='txtNum1' value='" +
num1.ToString() + "' /> + <input type='text'name='txtNum2' value='" +
num2.ToString() + "'/> = <input type='text' readonly='readonly' value='" +
result.ToString() + "' <br/>");
sbHTML.Append( "<input type='submit' value='计算'/><br />" );
sbHTML.Append( "</form></body></html>" );
context.Response.Write(sbHTML.ToString());
}
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string strNum1 = context.Request.Form["txtNum1" ];
string strNum2 = context.Request.Form["txtNum2" ];
//判断是否格式正确
string result = "0" ;
int num1 = 0, num2 = 0;
//如果包含隐藏域的话, 才执行相加操作
if (!string .IsNullOrEmpty(context.Request.Form[ "hidIsPostBack"]))
{
if (!string .IsNullOrEmpty(strNum1) && ! string.IsNullOrEmpty(strNum2))
{
if (int .TryParse(strNum1, out num1) && int.TryParse(strNum2, out num2))
{
result = (num1 + num2).ToString();
}
else
{
result = "输入格式错误" ;
}
}
}
System.Text. StringBuilder sbHTML = new System.Text.StringBuilder();
sbHTML.Append( "<!DOCTYPE><html><head><title>计算器</title></head><body><form action='06Calculate.ashx' method='post'>");
sbHTML.Append( "<input type='text' name='txtNum1' value='" +
num1.ToString() + "' /> + <input type='text'name='txtNum2' value='" +
num2.ToString() + "'/> = <input type='text' readonly='readonly' value='" +
result.ToString() + "' <br/>");
sbHTML.Append( "<input type='submit' value='计算'/><br />" );
sbHTML.Append( "<input type='hidden' name='hidIsPostBack' value='1' /></form></body></html>" );
context.Response.Write(sbHTML.ToString());
}
//---------------------------------类定义--------------------------------------------
/// <summary>
///一个计算器类
/// </summary>
public class Class1
{
//第一个操作数
public int num1 { get; set; }
//第二个操作数
public int num2 { get; set; }
//操作符
public string calculateChar{ get; set; }
//结果
public string result { get; set; }
public Class1()
{
}
/// <summary>
/// 计算结果
/// </summary>
/// <param name="a"> 第一个操作数 </param>
/// <param name="b"> 第二个操作数 </param>
/// <param name="oper"> 操作符</param>
public void GetResult(int a, int b, string oper)
{
this.num1 = a;
this.num2 = b;
this.calculateChar = oper;
switch (this .calculateChar)
{
case "+" :
result = (num1 + num2).ToString();
break;
case "-" :
result = (num1 - num2).ToString();
break;
case "*" :
result = (num1 * num2).ToString();
break;
case "/" :
result = (num1 / num2).ToString();
break;
}
}
}
//------------------------------------------------------页面类----------------------------------------------------------
public class _07CalculateFour : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
//实例化一个计算器对象
Class1 calcu = new Class1();
string strNum1 = context.Request.Form["txtNum1" ];
string strNum2 = context.Request.Form["txtNum2" ];
string strOper = context.Request.Form["optionOper" ];
int num1 = 0;
int num2 = 0;
if (!string .IsNullOrEmpty(context.Request.Form[ "hidIsPostBack"]))
{ //模拟回访
if (!string .IsNullOrEmpty(strNum1) && ! string.IsNullOrEmpty(strNum2))
{ //判断为空
if (int .TryParse(strNum1, out num1) && int.TryParse(strNum2, out num2))
{ //判断格式
calcu.GetResult(num1, num2, strOper);
}
else
{
calcu.result = "参数格式不正确" ;
}
}
}
System.Text. StringBuilder sbHTML = new System.Text.StringBuilder();
sbHTML.Append( "<!DOCTYPE ><html><head></head><body><form action='07CalculateFour.ashx' method='post'>");
sbHTML.Append( "<input type='text' name='txtNum1' value='" +calcu.num1.ToString()+"'/>");
sbHTML.Append( "<select name='optionOper'><option value='"+calcu.calculateChar+ "'>"+calcu.calculateChar+"</option><option value='+'>+</option><option value='-'>-</option><option value='*'>*</option><option value='/'>/</option></select>" );
sbHTML.Append( "<input type='text' name='txtNum2' value='" +calcu.num2.ToString()+"'/> = ");
sbHTML.Append( "<input type='text' readonly='readonly' name='txtResult' value='" +calcu.result+"'/>");
sbHTML.Append( "<input type='submit' value='计算'/>" );
sbHTML.Append( "<input type='hidden' name='hidIsPostBack' value='1'/></form></body></html>" );
context.Response.Write(sbHTML.ToString());
}
public class _08Cal : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
//--------------------读取html内容模版----------------------
//根据虚拟路径获得物理路径
string path = context.Server.MapPath("CalculateModel.htm"); //这里仔细记住
string strHTML = System.IO.File.ReadAllText(path); //这里也要好好记住
//------------------获得浏览器提交的内容---------------------------
string strNum1 = context.Request.Form["txtNum1"];
string strNum2 = context.Request.Form["txtNum2"];
int num1 = 0;
int num2 = 0;
string result = "";
if (!string.IsNullOrEmpty(context.Request.Form["hidIsPostBack"]))
{
if (!string.IsNullOrEmpty(strNum1) && !string.IsNullOrEmpty(strNum2))
{
if (int.TryParse(strNum1, out num1) && int.TryParse(strNum2, out num2))
{
result = (num1 + num2).ToString();
}
else
{
result = "输入格式错误";
}
}
}
//-------------------------输出html到浏览器------------------------
//字符串替换,进行赋值
strHTML = strHTML.Replace("{num1}", num1.ToString()).Replace("{num2}", num2.ToString()).Replace("{result}", result.ToString());
context.Response.Write(strHTML);
}
public bool IsReusable {
get {
return false;
}
}
}
//---------------------------------模版网页显示---------------------------------------
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html>
< head>
< title> 计算器 </title >
</ head>
< body>
< form action ='06Calculate.ashx' method ='post'>
< input type ='text' name ='txtNum1' value ='{num1}' /> +
< input type ='text' name ='txtNum2' value ='{num2}' /> =
< input type ='text' readonly ='readonly' value ='{result}' />< br />
< input type ='submit' value ='计算' />
< input type ='hidden' name ='hidIsPostBack' value ='1' />
</ form>
</ body>
</ html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有