我要做登陆界面,可目前没法获取到界面上的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>
因为本人没有任何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>
解决方案 »
- SNS用户搜索功能如何实现的?
- 求助啊 关于date的
- 求:用户上传图片,在线生成带效果的视频或flash,并可以下载swf或exe(无需联网就可以在本地直接播放和网上一样的效果)
- 关于DataList中HyperLink点击次数计算的问题(在线等!!)
- 这个问题有点难度 在asp.net中如何同时继承页面的控件以及cs代码? 谢谢大家
- 让我要砸键盘的Asp.Net问题
- 我的IIS的MIME读取WORd和excel文件时认为是无定义格式
- GridView增加列的问题
- 关于DataSet我太迷惘了,在线求助
- 为何在我的机器上建立的project的.aspx页面无法浏览?
- 问一个纳闷,但是常识性的问题,什么是组件?.net有没有组件可言?
- 权限问题
Request.Form.Get("Password")---->Password.text
string User = Request["UserName.Text"];
string Pwd = Request["Password.Text"];
可是我在response.write(User)的时候还是空的
是否还有其他地方的问题?
string Pwd = Password.Text;
或者使用老方法:
string User = Request.Form["UserName"];
string Pwd = Request.Form["Password"];
服务器端是可以直接引用这些控件的属性string User = UserName.Text;
string Pwd = Password.Text;
string User = Request.Form["UserName"];结果---空值
其实这些我都试过了,所以是不是错在其他地方?
<asp:Login ID="Login1" runat="server" FailureText="Logged failed,try again pls.">
<LayoutTemplate>
</LayoutTemplate>
</asp:Login>这样你就可以用:
string User = UserName.Text;
string Pwd = Password.Text;
或者使用老方法:
string User = Request.Form["UserName"];
string Pwd = Request.Form["Password"];
来获得你要的值了
// Login1.Password;
// Login1.UserName;
但是最好别用这些控件 ,灵活性很差,还自动给你生成数据库表 烦
格外感谢chx_xuxu(逍遥客)的正解:)
因为用控件实在可以省写很多代码,也是出于好奇
以后会多多学习后再尝试使用现成控件