初学.net,简单问题请教
我要做登陆界面,可目前没法获取到界面上的UserName与Password值
因为本人没有任何C#的基础,只会一点asp,所以搜索了类似的问题,仍然不果...
请各位大人多多指教
以下是界面的代码
<%@ Page Language= "C# " %>
<%@ import Namespace= "System.Data " %>
<%@ import Namespace= "System.Data.SqlClient " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script runat= "server ">
protected void LoginButton_Click(object sender, EventArgs e)
{
SqlConnection Myconn = new SqlConnection();
string User = Request[ "UserName "];
//尝试了好几种方式都不行,不知道错是在获取的方法上还是根本就没有传递?
string Pwd = Request.Form.Get( "Password ");
string Constr = "server=xx;uid=xx;pwd=xx;database=xx;Connect Timeout=30 ";
string Sql = "sp_Employee_Login " + User + ", " + Pwd;
Myconn = new SqlConnection(Constr);
SqlCommand Strsql = new SqlCommand(Sql, Myconn);
Response.Write(User);
try
{
Myconn.Open();
SqlDataReader rs = Strsql.ExecuteReader(0);
if (rs.GetValue(0) != null)
{
Response.Redirect( "~data.asp ");
}
if (rs!= null)
{ rs.Close(); }
}
catch(System.Exception ex)
{
this.Response.Write( "Logged failed ");
}
finally
{
if (Myconn!=null)
{
Myconn.Close();
}
}
}
</script>
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> Login </title>
</head>
<body>
<form id= "form1 " runat= "server " method= "post ">
<div>
<asp:Login ID= "Login1 " runat= "server " FailureText= "Logged failed,try again pls. ">
<LayoutTemplate>
<table border= "0 " cellpadding= "1 " cellspacing= "0 " style= "border-collapse: collapse ">
<tr>
<td>
<table border= "0 " cellpadding= "0 ">
<tr>
<td align= "center " colspan= "2 ">
Log in </td>
</tr>
<tr>
<td align= "right ">
<asp:Label ID= "UserNameLabel " runat= "server " AssociatedControlID= "UserName "> UserName: </asp:Label> </td>
<td>
<asp:TextBox ID= "UserName " runat= "server "> </asp:TextBox>
<asp:RequiredFieldValidator ID= "UserNameRequired " runat= "server " ControlToValidate= "UserName "
ErrorMessage= "UserName could not be empty。 " ToolTip= "UserName could not be empty。 " ValidationGroup= "Login1 "> * </asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align= "right ">
<asp:Label ID= "PasswordLabel " runat= "server " AssociatedControlID= "Password "> Password: </asp:Label> </td>
<td>
<asp:TextBox ID= "Password " runat= "server " TextMode= "Password "> </asp:TextBox>
<asp:RequiredFieldValidator ID= "PasswordRequired " runat= "server " ControlToValidate= "Password "
ErrorMessage= "Password could not be empty。 " ToolTip= "Password could not be empty。 " ValidationGroup= "Login1 "> * </asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan= "2 ">
<asp:CheckBox ID= "RememberMe " runat= "server " Text= "Remember me next time。 " />
</td>
</tr>
<tr>
<td align= "center " colspan= "2 " style= "color: red ">
<asp:Literal ID= "FailureText " runat= "server " EnableViewState= "False "> </asp:Literal>
</td>
</tr>
<tr>
<td align= "right " colspan= "2 ">
<asp:Button ID= "LoginButton " runat= "server " CommandName= "Login " Text= "Go " ValidationGroup= "Login1 " OnClick= "LoginButton_Click " />
</td>
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:Login>
</div>
</form>
</body>
</html>
[解决办法]
Request[ "UserName "]----> UserName.text
Request.Form.Get( "Password ")----> Password.text
[解决办法]
string User = UserName.Text;
string Pwd = Password.Text;
或者使用老方法:
string User = Request.Form[ "UserName "];
string Pwd = Request.Form[ "Password "];
[解决办法]
可以这样获取
// Login1.Password;
// Login1.UserName;
但是最好别用这些控件 ,灵活性很差,还自动给你生成数据库表 烦