母版HTML页,代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form action="" method="post">
<input type="text" name="txtname" value="{num}"/>
<input type="submit"/>
</form>
</body>
</html>一般处理程序对其引用,然后自增
int a=1 ;
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string path = context.Server.MapPath("Addself.htm");
string model = System.IO.File.ReadAllText(path);
if (!string.IsNullOrEmpty("txtxname"))
{
int.TryParse(context.Request.Form["txtname"],out a);
}
a++; model = model.Replace("{num}", a.ToString());
context.Response.Write(model);
不明白的是 int a,无论我设a为多少,为什么走完
if (!string.IsNullOrEmpty("txtxname"))
{
int.TryParse(context.Request.Form["txtname"],out a);
}
之后,a就只会被赋值为0了?HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form action="" method="post">
<input type="text" name="txtname" value="{num}"/>
<input type="submit"/>
</form>
</body>
</html>一般处理程序对其引用,然后自增
int a=1 ;
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string path = context.Server.MapPath("Addself.htm");
string model = System.IO.File.ReadAllText(path);
if (!string.IsNullOrEmpty("txtxname"))
{
int.TryParse(context.Request.Form["txtname"],out a);
}
a++; model = model.Replace("{num}", a.ToString());
context.Response.Write(model);
不明白的是 int a,无论我设a为多少,为什么走完
if (!string.IsNullOrEmpty("txtxname"))
{
int.TryParse(context.Request.Form["txtname"],out a);
}
之后,a就只会被赋值为0了?HTML
if (!string.IsNullOrEmpty(context.Request.Form["txtname"]))
{
int.TryParse(context.Request.Form["txtname"],out a);
}
你这条件永远为真,txtxname字符串当然不为空
你应该判断context.Request.Form["txtname"]这个值是否为空
"txtxname" 不等于context.Request.Form["txtname"]"txtxname"这里时一个字符串变量,而且是有值的
"txtxname" 不等于context.Request.Form["txtname"]"txtxname"这里时一个字符串变量,而且是有值的是不是说,if (!string.IsNullOrEmpty("txtxname")) 判断的是一个内容为"txtname"的字符串是否为空,肯定不为空了,而不是我想让他判断的form中的那个控件内的值?<input type="text" name="txtname" value="{num}"/> 这样txtname控件的值是空吗,放了个占位符啊,只占位符还是属于空吗?
我是小白,前辈莫嘲笑
"int.TryParse 与 int.Parse 较为类似,但它不会产生异常,转换成功返回 true,转换失败返回 false。最后一个参数为输出值,如果转换失败,输出值为 0"
刚刚查到的,看来就是这么回事了 谢前辈
if (!string.IsNullOrEmpty("txtxname"))
本身isnullorempty就是接受一个string,您劳大直接写进一个去
结果当然是一样了