菜鸟问题:Request传值问题~
小弟这两天刚在研究VS2005,碰到这个问题,自己琢磨了2个小时,实在是不知道哪的问题,请各位大侠帮忙~
1.login.htm内的form代码
<form method= "post " action= "Webform.aspx ">
<br />
<br />
<br />
<br />
<table border= "1 " cellpadding= "1 " cellspacing= "1 " style= "width: 293px ">
<tr>
<td style= "width: 89px; height: 21px ">
用户名: </td>
<td style= "width: 242px; height: 21px ">
<input id= "txtUserName " type= "text " /> </td>
</tr>
<tr>
<td style= "width: 89px ">
密码: </td>
<td style= "width: 242px ">
<input id= "txtUserPwd " type= "text " /> </td>
</tr>
<tr>
<td style= "width: 89px ">
</td>
<td style= "width: 242px ">
<input id= "Submit1 " type= "submit " value= "提交 " onclick= "return Submit1_onclick() " /> </td>
</tr>
</table>
</form>
2.Webform.aspx.cs内接收部分的代码
protected void Page_Load(object sender, EventArgs e)
{
string userName = Request[ "txtUserName "].ToString();
string userPwd = Request.Form.Get( "txtUserPwd ").ToString();
Response.Write( "用户名为: " + userName + ": 密码为: " + userPwd);
}
头都晕了,一直提示:未将对象引用设置到对象的实例。
异常详细信息: System.NullReferenceException
[解决办法]
问题一:
先判断是否为空,如楼上所言
问题二:
<input id= "txtUserName " type= "text " /> 有问题,在后台是取HTML控件的
值时是根据它的name属性来取的,而不是id,所以这里它的name属性是必须设的,
否则肯定是找不到这个对象的.
<input id= "txtUserName " name= "txtUserName " type= "text " />
密码文本框也是一样...