http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3343|回复: 6

[学生提问] C#.net

[复制链接]
发表于 2016-3-10 23:14:58 | 显示全部楼层 |阅读模式


想点击一下计算按钮,方框的值会自动加一。
但是在打开网页是方框出现的值是$num,是不是强制转换类型没有成功呢?点击按钮时提示字符串类型错误。
代码如下:
<body>
<form method="post" action="Handler2.ashx">
用户名:<input type="text" value="$num" name="textname" />
<input type="submit" value="计算" />
<input type="hidden" value="$num" name="hiddenh" />
</form>

using System;
using System.Web;
using System.IO;
public class Handler2 : IHttpHandler {
    //protected int num = 0;
     public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        string file1 = context.Request.MapPath("index-2htm.htm");
        //file1是接收index-2htm.htm网页的物理地址
        string file2 = File.ReadAllText(file1);
    //file2是把文件里面所有的行然后关闭文件。
        int num =Convert.ToInt32(context.Request.Form["hiddenh"]);//接收隐藏域的值。
        num++;
        file2 = file2.Replace("$num",num.ToString());
        //读取的文本文件里面的空字符num被替换。
        context.Response.Write(file2);
         
    }
求赐教。



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2016-3-11 08:35:07 | 显示全部楼层
强制转换肯定成功不了,你通过context.Request.Form["hiddenh"]取得的值$num,肯定转化不成int
 楼主| 发表于 2016-3-13 11:35:08 | 显示全部楼层
songwenqi 发表于 2016-3-11 08:35
强制转换肯定成功不了,你通过context.Request.Form["hiddenh"]取得的值$num,肯定转化不成int

那要怎么转换成Int呢?
发表于 2016-3-14 11:09:29 | 显示全部楼层
My-self 发表于 2016-3-13 11:35
那要怎么转换成Int呢?

把你input 隐藏域的input 默认值设为0
 楼主| 发表于 2016-3-14 13:42:11 | 显示全部楼层
songwenqi 发表于 2016-3-14 11:09
把你input 隐藏域的input 默认值设为0

<body>
<form method="post" action="Handler2.ashx">
用户名:<input type="text" value="$num" name="textname" />
<input type="submit" value="计算" />
<input type="hidden" value="0" name="hiddenh" />
</form>
</body>
using System;
using System.Web;
using System.IO;
public class Handler2 : IHttpHandler {
    //protected int num = 0;
     public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
        string file1 = context.Request.MapPath("index-2htm.htm");
        //file1是接收index-2htm.htm网页的物理地址
        string file2 = File.ReadAllText(file1);
    //file2是把文件里面所有的行然后关闭文件。
        int num =Convert.ToInt32(context.Request.Form["hiddenh"]);//接收隐藏域的值。
        num++;
        file2 = file2.Replace("$num",num.ToString());
        //读取的文本文件里面的空字符num被替换。
        context.Response.Write(file2);
         
    }
换成0之后可以运行,打开网页是$num,点击计算按钮时是1,之后在点击就不会再自动加一了,请问是什么原因呢?
发表于 2016-3-14 14:36:22 | 显示全部楼层

把隐藏input的值每次也加1
 楼主| 发表于 2016-3-15 23:28:36 | 显示全部楼层
songwenqi 发表于 2016-3-14 14:36
把隐藏input的值每次也加1

<body>
<form method="post" action="Handler2.ashx">
用户名:<input type="text" value="$num" name="textname" />
<input type="submit" value="计算" />
<input type="hidden" value="0" name="hiddenh" />
</form>
</body>
using System;
using System.Web;
using System.IO;
public class Handler2 : IHttpHandler {
     //protected int num = 0;
      public void ProcessRequest (HttpContext context) {
         context.Response.ContentType = "text/html";
         string file1 = context.Request.MapPath("index-2htm.htm");
         //file1是接收index-2htm.htm网页的物理地址
         string file2 = File.ReadAllText(file1);
     //file2是把文件里面所有的行然后关闭文件。
         int num =Convert.ToInt32(context.Request.Form["hiddenh"]);//接收隐藏域的值。
         num++;
         file2 = file2.Replace("$num",num.ToString());
         //读取的文本文件里面的空字符num被替换。
         context.Response.Write(file2);
         
     }
num++;不是每次加一吗?
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-8 02:28

© 2014-2021

快速回复 返回顶部 返回列表