我定义了两个ASPX页 一个叫login1.aspx,一个叫login2.aspx,其中login1.aspx有两个TextBox,分别定义为UserName和 userPwd
然后在login1.aspx页面的FORM里定义<form id="form1" runat="server" method="post" action="login2.aspx"> 传值给Login2.aspx。主要是从数据库里验证用户输入的帐号和密码是否正确。··········其中login1.aspx 的代码是··································
··············································<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login1.aspx.cs" Inherits="login1" %><!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" method="get" action="login2.aspx">
<div>
<asp:TextBox ID="userName" runat="server"></asp:TextBox><br />
<br />
<asp:TextBox ID="userPwd" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
</form>
</body>
</html>
···················login2.aspx的代码是····························
·····················································
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login1.aspx.cs" Inherits="login1" %><!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" method="get" action="login2.aspx">
<div>
<asp:TextBox ID="userName" runat="server"></asp:TextBox><br />
<br />
<asp:TextBox ID="userPwd" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
</form>
</body>
</html>一运行, string userName = Request.Form["userName"].ToString();这一行就被系统标识为黄色,提示”“”未将对象引用设置到对象的实例“”“。谢大家了,我急等!
然后在login1.aspx页面的FORM里定义<form id="form1" runat="server" method="post" action="login2.aspx"> 传值给Login2.aspx。主要是从数据库里验证用户输入的帐号和密码是否正确。··········其中login1.aspx 的代码是··································
··············································<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login1.aspx.cs" Inherits="login1" %><!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" method="get" action="login2.aspx">
<div>
<asp:TextBox ID="userName" runat="server"></asp:TextBox><br />
<br />
<asp:TextBox ID="userPwd" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
</form>
</body>
</html>
···················login2.aspx的代码是····························
·····················································
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login1.aspx.cs" Inherits="login1" %><!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" method="get" action="login2.aspx">
<div>
<asp:TextBox ID="userName" runat="server"></asp:TextBox><br />
<br />
<asp:TextBox ID="userPwd" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
</form>
</body>
</html>一运行, string userName = Request.Form["userName"].ToString();这一行就被系统标识为黄色,提示”“”未将对象引用设置到对象的实例“”“。谢大家了,我急等!
把textbox 改成<input type="text" type="userName" />其它的也改成这样的方式 把buttion 改成<button type="submit">提交</button>应该就行了吧
如要验证用户输入的帐号和密码是否正确一个ASPX便可搞定了~
为啥要用两个ASPX~
似乎是还是ASP思维~
string userName = Request.Form[ "userName "].ToString();
你这样是获取不了数值的,因为你都没有传递过去!
Login2.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;
using System.Data.SqlClient;
using System.Web.SessionState;
using System.Drawing;
using System.ComponentModel;public partial class login2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string userName = Request.Form["userName"].ToString();
string userPwd = Request.Form["userPwd"].ToString();
SqlConnection con = new SqlConnection("server=.;database=MSG;uid=sa;pwd=;");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM login WHERE userName='" + userName + "' and userPwd='" + userPwd + "'", con);
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
Response.Redirect("main.aspx");
}
else
{
Response.Redirect("falllogin.aspx");
}
}
}
login1.aspx的cs是什么?
login1.aspx
<form id="aa" method=get action= "login2.aspx ">
<input id="userName" type="text" runat=server />
<input id="Submit1" type="submit" value="submit" />
</form>
login2.aspx.cs string a = Request["userName"].ToString();
Response.Write(a);
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;
using System.Data.SqlClient;public partial class login1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
}
}