更新时间:2023-12-03 20:31:11
您可以将此代码用于将为您显示消息的登录控件.
You can use this code for the login control that will display message for you.
<asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false">
<LayoutTemplate>
<span class="failureNotification">
<asp:Literal ID="FailureText" runat="server"></asp:Literal>
</span>
<asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification"
ValidationGroup="LoginUserValidationGroup"/>
<div class="accountInfo">
<fieldset class="login">
<legend>Account Information</legend>
<p>
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label>
<asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required."
ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
</p>
<p>
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
<asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required."
ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
</p>
<p>
<asp:CheckBox ID="RememberMe" runat="server"/>
<asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in</asp:Label>
</p>
</fieldset>
<p class="submitButton">
<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="LoginUserValidationGroup"/>
</p>
</div>
</LayoutTemplate>
</asp:Login>
< pre lang ="c#">
/////登录验证/////////
受保护的void Button1_Click(对象发送者,EventArgs e)
{
SqlConnection con =默认值(SqlConnection);
SqlCommand cmd =默认值(SqlCommand);
SqlDataReader dr =默认值(SqlDataReader);
//种子定义
字符串uid = null;
uid = TextBox1.Text;
会话[用户名"] = uid;
字符串constr = ConfigurationManager.ConnectionStrings ["SomeDataBase"].ToString();
con =新的SqlConnection(constr);
con.Open();
cmd = new SqlCommand("select * from Reg",con);
dr = cmd.ExecuteReader();
字符串s = null;
字符串s1 = null;
while((dr.Read()))
{
s = dr ["Username"].ToString();
s1 = dr ["Password"].ToString();
如果((((TextBox1.Text == s.Trim())&(TextBox2.Text == s1.Trim())))
{
Response.Redirect("Home.aspx");
}
Label3.Visible = true;
}
con.Close();
}</pre>
<pre lang="c#">
///// login validate /////////
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = default(SqlConnection);
SqlCommand cmd = default(SqlCommand);
SqlDataReader dr = default(SqlDataReader);
// Seession define
string uid = null;
uid = TextBox1.Text;
Session["Username"] = uid;
string constr = ConfigurationManager.ConnectionStrings["SomeDataBase"].ToString();
con = new SqlConnection(constr);
con.Open();
cmd = new SqlCommand("select * from Reg", con);
dr = cmd.ExecuteReader();
string s = null;
string s1 = null;
while ((dr.Read()))
{
s = dr["Username"].ToString();
s1 = dr["Password"].ToString();
if (((TextBox1.Text == s.Trim()) & (TextBox2.Text == s1.Trim())))
{
Response.Redirect("Home.aspx");
}
Label3.Visible = true;
}
con.Close();
}</pre>
如果您正在使用aspnet成员表
您可以轻松实现
使用登录控件
&如果出现LoginError
设置LoginError.FailureText
或者您可以在设计时添加它
if u are using aspnet membership tables
u could make it easy
use login control
& in the event of LoginError
set the LoginError.FailureText
or u could add it in desigin time