黑马程序员技术交流社区

标题: 表单请求为什么拿不到值? [打印本页]

作者: 崔增阳    时间: 2014-1-17 16:52
标题: 表单请求为什么拿不到值?
本帖最后由 崔增阳 于 2014-1-20 13:18 编辑

c#代码里的txtName和txtRemark为什么拿不到值呢?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>            
        </div>
    </form>
        <form action="Default2.aspx" method="POST">
            <input type="hidden" name="hiddenVierState" value="<%=txtViewStateValue %>"/>
            姓名:<input type="text" name="txtName" value="<%=txtName %>"/><br/>
            密码:<input type="text" name="txtRemark" value="<%=txtRemark %>"/><br/>
            <input type="submit" value="提交"/>
        </form>

</body>
</html>




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    public string txtName;
    public string txtRemark;
    public string txtViewStateValue;
    protected void Page_Load(object sender, EventArgs e)
    {
        txtName = Request.Form["txtName"];
        txtViewStateValue = txtName;
        txtRemark = Request["txtRemark"];
        if (Request["hiddenVierState"]!=null)
        {
            if (Request["txtName"]==(Request["hiddenVierState"]))
            {
                    Response.Write("没有改变");
            }
            else
            {
                Response.Write("改变了");
            }
        }
    }
}



作者: 师弟    时间: 2014-1-17 23:27
runat="server"  好象忘写了吧,你的第二个表单没写,在后台看来普通html代码,不给他生成ViewState的隐藏状态值
作者: 念~    时间: 2014-1-18 17:38
第二个表单没有写  runat="server"

HTML中的这种写法,  我没有记错的话,应该是从前台拿后台的值而不是从后台拿前台的值吧
作者: 王忠杰    时间: 2014-1-19 11:57
好像第二个表单没有写 runat="server"




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2