C#2005无法获取表单数据
现在有两个文件Login.htm(启动文件),Default.axps,为什么点提交后取到的是空值??代码如下:
Login.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head>
<title> 测试Response-Request对象 </title>
</head>
<body>
<form id= "form1 " action= "Default.aspx " method= "Post ">
<table style= "width: 280px; height: 64px ">
<tr>
<td style= "width: 67px ">
用户名 </td>
<td style= "width: 171px ">
<input id= "txtUserName " style= "width: 149px " type= "text " value= "gongxinheng " /> </td>
<td>
</td>
</tr>
<tr>
<td style= "width: 67px ">
密 码 </td>
<td style= "width: 171px ">
<input id= "txtPassWord " style= "width: 149px " type= "password " value= "123 " /> </td>
<td>
</td>
</tr>
<tr>
<td style= "width: 67px ">
</td>
<td style= "width: 171px ">
<input id= "btnSubmit " type= "submit " value= "提交 " /> </td>
<td>
</td>
</tr>
</table>
</form>
</body>
</html>
Default.aspx
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace TestResponseRequest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string userName = Request.Form[ "txtUserName "];
string userPwd = Request.Form[ "txtPassWord "];
//string userName = Request.Form.Get( "txtUserName ").ToString();
//string userPwd = Request.Form.Get( "txtPassWord ").ToString();
Response.Write( "登录的用户名为 " + userName + ";密码为 " + userPwd);
}
}
}
两种方法都试过了也不行!
[解决办法]
提交的表单认得是name而不是id
<input id= "txtUserName " name = "txtUserName " style= "width: 149px " type= "text " value= "gongxinheng " />
<input id= "txtPassWord " name= "txtPassword " style= "width: 149px " type= "password " value= "123 " />
就OK了