JOATIT

Jack of all things IT

Menu

Skip to content
  • Home
  • About
  • Downloads
  • Projects
    • Forward Schedule of Change (for Self-Service)
    • Purchase Magic
    • Reporting Magic
    • TechMagic
  • Resources
    • Useful calculated fields
    • User Form Gallery

Daily Archives: February 17, 2009

Standard

Posted by

ajrbarber

Posted on

February 17, 2009

Posted under

ASP.NET, C#, VB.NET

Comments

Leave a comment

ASP.NET 2.0 + Login Control With Focus

I have experimented with many different login control layouts and have settled on a very simple implementation of the out of the box LoginCtrl that ships with ASP.NET 2.0 and above, and as such thought I would share:

HTML

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<!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>Demo Login Page</title>
<style type="text/css">
*
{
margin: 0;
padding: 0;
}
fieldset
{
margin: 0 0 10px 0;
padding: 5px;
}
fieldset p
{
clear: left;
padding-top: 5px;
}
legend
{
margin: 0;
padding: 5px;
background-color: #DDDDDD;
border-style: solid;
border-width: 1px;
border-color: #FFF #AAA #666 #FFF;
font-size: 1em;
font-weight: bold;
}
dt
{
float: left;
width: 150px;
padding: 5px;
font-weight: bold;
}
dd
{
margin-left: 155px;
padding: 5px;
}
.txtNormal
{
width: 250px;
}
</style>
</head>
<body>
<form id="frm" runat="server">
<fieldset>
<legend>Existing Users</legend>
<p>
<asp:Login ID="LoginCtrl" runat="server" Width="100%">
<LayoutTemplate>
<dl>
<dt>Username:</dt>
<dd>
<asp:TextBox ID="UserName" runat="server" CssClass="txtNormal"></asp:TextBox></dd>
<dt>Password:</dt>
<dd>
<asp:TextBox ID="Password" TextMode="Password" runat="server" CssClass="txtNormal"></asp:TextBox></dd>
</dl>
<asp:Literal ID="FailureText" runat="server"></asp:Literal>
<p>
<asp:Button ID="Login" runat="server" CommandName="LogIn" Text="Log In" /></p>
</LayoutTemplate>
</asp:Login>
</p>
</fieldset>
<br />
<fieldset>
<legend>New Users</legend>
<p>
Click <a href="NewUser.aspx">here</a> to register for an account.</p>
</fieldset>
</form>
</body>
</html>

Code-Behind

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox txtUserName = (TextBox)LoginCtrl.FindControl("UserName");
txtUserName.Focus();
}
}
}

VB.NET

Partial Class Login
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
CType(LoginCtrl.FindControl("UserName"), TextBox).Focus()
End If
End Sub
End Class

I am quite sure that others can improve further on this but as a starter for ten, it works for me. As always, any comments, positive or negative, are always welcome.

Recent Posts

  • Making the SDE Knowledge Base work for you
  • Allowing Staff To Opt In To Emails
  • Service Tree By Description Rather Than Name
  • Apologies – I’m back…
  • Maintenance Plan for SQL Server 2005 Express

Recent Comments

shawnseib on HTML Emails in SDE
Sina on How to count the number of occ…
ksarhan on Service Tree By Description Ra…
The Problem Manager on Making the SDE Knowledge Base…
bwing on How to count the number of occ…

Archives

  • October 2011
  • September 2011
  • August 2011
  • May 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007

Categories

  • ASP.NET
  • Business Processes
  • C#
  • Excel
  • Featured
  • General
  • Javascript
  • Service Desk Express
  • SQL Reporting Services
  • SQL Server
  • VB.NET

Meta

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.com
Blog at WordPress.com.
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Follow Following
    • JOATIT
    • Already have a WordPress.com account? Log in now.
    • JOATIT
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar