初学者的asp.net登录页面 [英] asp.net login page for beginner

查看:82
本文介绍了初学者的asp.net登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是关于学习ASP.NET的一个基本问题,我曾在Google,StackOverflow,MSDN和CodeProject上搜索过,但结果没有找到。



我正在使用ASP.NET(C#)在2个文本框中设计页面 default.aspx 和一个按钮(用户名,密码和提交)。



登录成功后,

 Response.Redirect(  userprofile.aspx ); 

会将我重定向到该页面。



这是我的问题:在页面上 userprofile.aspx ,如果我点击链接转移到 default.aspx ,如何更改2个文本框和一个按钮(禁用或消失在页面 default.aspx )通过2个链接用户名退出



谢谢!

解决方案

如果您想在c#中使用两个文本框创建简单的登录面板,例如emailid和密码以及Log按钮控件在'然后在你的页面中的按钮点击事件中写下面的代码

c#代码在这里

< blockquote class =quote>< div class =op > Quote:< / div> string Query =select * from tblLogin where emailid ='+ txtEmailID.Text +'and password ='+ txtPassword.Text +';

SqlDataAdapter adp = new SqlDataAdapter(Query,con);

DataSet ds = new DataSet();

adp.Fill(ds);

if(ds.Tables [0] .Rows.Count> 0)

{

lblErrorMsg.Text =成功登录;

}

else

{

lblErrorMsg.Text =EmailID /密码不正确;

txtEmailID.Focus();

}< / blockquote>



完整详细信息请访问此链接....我希望此链接对您有所帮助

http:// aspdotnet-alam .COM /条/如何对创建登录页面与 - 正确的验证和记住,我的功能 - 使用 - 天冬氨酸-NET-C-夏普1.aspx


最好使用asp.net成员资格或身份来做这些事情,但是如果你这样做是一个学习任务,那么你可以存储用户是否使用Session登录。存储他们的ID或用户名,如果您需要知道他们是否已登录,请检查相关的会话变量是否有值。



一种方便的方式来显示\ n隐藏某些元素取决于是否有人通过身份验证是使用您可见或隐藏的占位符。



 <   asp:PlaceHolder     ID   =  placeAuthenticated    runat   =  server   可见  =  false >  
< div > Hello <% =会话[ 用户名] %> < asp:HyperLink ID = linkLogout < span class =code-attribute> runat = server NavigateUrl = 〜/ Logout.aspx > logout < / asp:HyperLink >
< / div >
< / asp:PlaceHolder >

< asp:PlaceHolder ID = placeAnonymous runat = 服务器 可见 = true >
< asp:按钮 ID = cmdLogin runat = server 文字 = 登录 OnClick = cmdLogin_Click / >
< / asp:PlaceHolder >

< br $> b $ b

 受保护  void < /跨度> Page_Load( object  sender,EventArgs e)
{
if (!string。 IsNullOrWhiteSpace(( string )会话[ 用户名]))
{
placeAnonymous.Visible = false ;
placeAuthenticated.Visible = true ;
}
}

受保护 void cmdLogin_Click( object sender,EventArgs e)
{
// 确保用户有效,如果存储他们的用户名或用户ID(或两者)
//
会话[ 用户名] = user1;

Response.Redirect( test.aspx); // 此页面只是为了简单而重定向到自身
}


I know that it's a basic question about learning ASP.NET, I had searched on Google, StackOverflow, MSDN and CodeProject but nothing was found for my result.

I'm using ASP.NET (C#) to design a page default.aspx within 2 textboxes and a button (username, password and submit).

After login successfull,

Response.Redirect("userprofile.aspx");

will redirect me to the page.

Here is my question: On the page userprofile.aspx, if I click a link to move to default.aspx, "How to change 2 textboxes and a button (disable or disappear on page default.aspx) by 2 links "Hi, username" and "Logout"?

Thank you!

解决方案

If you want create simple login panel in c# with two textbox like emailid and password and button control for 'Login' then write below code in button click event in your page

c# Code here

<blockquote class="quote"><div class="op">Quote:</div>string Query = "Select * from tblLogin where emailid='" + txtEmailID.Text + "' and password='" + txtPassword.Text + "'";

SqlDataAdapter adp = new SqlDataAdapter(Query,con);

DataSet ds = new DataSet();

adp.Fill(ds);

if (ds.Tables[0].Rows.Count > 0)

{

lblErrorMsg.Text = "Successfully login";

}

else

{

lblErrorMsg.Text = "EmailID/Password Incorrect";

txtEmailID.Focus();

}</blockquote>



for full details go to this link ....i hope this link will helpful for you

http://aspdotnet-alam.com/Article/how-to-create-login-page-with-proper-validation-and-remember-me-functionality-using-Asp-Net-C-Sharp-1.aspx


It's better to use asp.net membership, or identity to do these things, but if you are doing this is a learning task then you can store if the user is logged in using the Session. Store their ID or username, and if you need to know if they are logged in check to see if the relevant session variables have a value.

A handy way to show\hide certain elements depending on if someone is authenticated is to use placeholders which you make visible or hidden depending.

<asp:PlaceHolder ID="placeAuthenticated" runat="server" Visible="false">
    <div>Hello <%=Session["Username"] %> (<asp:HyperLink ID="linkLogout" runat="server" NavigateUrl="~/Logout.aspx">logout</asp:HyperLink>)
    </div>
</asp:PlaceHolder>

<asp:PlaceHolder ID="placeAnonymous" runat="server" Visible="true">
    <asp:Button ID="cmdLogin" runat="server" Text="Login" OnClick="cmdLogin_Click" />
</asp:PlaceHolder>



protected void Page_Load(object sender, EventArgs e)
{
    if(!string.IsNullOrWhiteSpace((string)Session["Username"]))
    {
        placeAnonymous.Visible = false;
        placeAuthenticated.Visible = true;
    }
}

protected void cmdLogin_Click(object sender, EventArgs e)
{
    // ensure the user is valid, if so store their username, or userid (or both)
    // in the session
    Session["Username"] = "user1";

    Response.Redirect("test.aspx"); // this page just redirects to itself for simplicity
}


这篇关于初学者的asp.net登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆