January 2008 - Posts
When you use the Login control in the toolbox, it generates code that looks like this:
<asp:Login ID="Login1" runat="server">
</asp:Login>
And from design view, it looks something like this (depending on your background):

When a user then uses the log in control to log into the site, ASP.NET sends them to the ValidateUser function in the MembershipProvider class.
But what if you want to run your own custom code prior to validating the user? You can add your own "OnLoggingIn" method.
Microsoft provides more information on the OnLoggingIn Method and explains that it "Raises the LoggingIn event when a user submits login information but before the authentication takes place." See this article on the Login.LoggingIn Event also. Both articles have some examples of this in use. You might also want to see the Login Control Events section of the ASP.NET Page Life Cycle Overview document.
So in effect, if you are using a Login control and you want to run your custom log in code before the system handles the log in, you may use the "OnLoggingIn" property to access your custom function.
<asp:Login ID="Login2" runat="server"
OnLoggingIn="Login1_LoggingIn">
</asp:Login>Notice I simply added this property to the log in control:
OnLoggingIn="Login1_LoggingIn"
You'll put your Login1_LoggingIn method in your code behind:
Protected Sub Login1_LoggingIn(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs)
' pre log in code goes here
End Sub
May your dreams be in ASP.NET!
Nannette Thacker
If you're new to ASP.net, this may be an issue for you at some point. What if you're programming, you put in your page load function, and then run your code and nothing happens? You turn on debugging and try to step through the code, but it never hits your Page_Load function? What in the world is going on? Why is this happening?
Let's take a look at what happens when you create a new web form page. When you right click in solution explorer, and add a new item, and add a new web form, visual web developer automatically generates an .aspx page for you. At the top you'll find the @ Page directive:
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="test.aspx.vb" Inherits="Blog_test" %>
Notice that the property 'AutoEventWireup="false"' is automatically inserted in the @ Page directive. If you assign a Master Page, this isn't an issue in your new page, but it may be an issue in your master page.
Microsoft explains the AutoEventWireup, in that "When AutoEventWireup is true, ASP.NET does not require events to specify event handlers like Page_Load or Page_Init. This means that the Handles keyword in Visual Basic is not required in the server script in the Web Form page. By default, when the ASP.NET Web application is created in Visual Studio, the value of the AutoEventWireup attribute is set to false in the .aspx page or .ascx control, and event handlers are not automatically created. Do not set AutoEventWireup to true if performance is a key consideration."
Since performance should typically always be a consideration, you would want to leave your AutoEventWireup as false, however, what is this "Handles keyword" of which they speak?
This simply means that in your codebehind page, if you desire to have an event handler such as Page_Load or Page_Init, you need to add the Handles keyword like so:
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
' page load code goes here...
End Sub
What happens if you don't have "Handles Me.Load"? Nothing. Absolutely nothing. You'll run the page, but your Page_Load function will not run. You'll ponder why this code isn't being hit, and you'll even put in debug breakpoints and try to step through it, but it never hits your Page_Load. And until you remember this little tidbit of advice, you'll be pulling out your hair wondering why your code is being ignored.
May your dreams be in ASP.NET!
Nannette Thacker
By Nannette Thacker
If you're new to ASP.net, but familiar with ASP Classic or HTML, you might wonder about the ASP.net table control.
Dragging an HTML table from your toolbox...

... will generate these table commands:
<table>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>But since we want to stick with the new ASP.net controls, we'll instead drag our Standard Table control onto our form.

But all it does is generate this code:
<asp:Table ID="Table1" runat="server">
</asp:Table>
The above code simply looks like this on the screen:

What in the world do you do with that?
At this point, right click the Table in the Design view and select Properties.

The Properties window will display with our Table1 properties.

To assign Rows, go to the bottom and select the Rows property. A little button will appear on the right [...], select it.

This will open the TableRow Collection Editor with properties for your row, <TR>. Select "Add" to add rows to your table.

To add Cells or "<TD>"s, go to the bottom of the TableRow Properties and select the "Cells" Property. A little button will appear [...]; select it. This opens the TableCell Collection Editor. Select "Add" to add columns to your table.

Select OK to close each window. Now look at your code.
We have inserted a few words within our TableCell properties. Now our finished code looks like this:
<asp:Table ID="Table2" runat="server">
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell ID="TableCell1" runat="server">
This is Cell 1
</asp:TableCell>
<asp:TableCell ID="TableCell2" runat="server">
This is Cell 2
</asp:TableCell>
</asp:TableRow>
</asp:Table>
On screen, it looks like this:

But where are the other common values you are used to using within tables, rows and cells?
Let's play around with our Properties. You may set the Table's Appearance. Notice here, I set the BackColor to White, the BorderColor to Red and the BorderWidth to 1px. I also set CellPadding and CellSpacing to 2 and assigned a CssClass. (If you set the class, it can handle the majority of these properties for you, but that's another story.) The Font Forecolor is set to Navy and Gridlines to Both. Play around with the Properties and you'll likely find other values you may need.

In our Layout Property, I set the HorizontalAlign to "Center."

Now I want to skip over to the TableCell Collection Editor and set the TableCell alignment to "Right" for the first cell and Wrap to False.

Our finished table now looks like this:
| This is Cell 1 |
This is Cell 2 |
And the generated code looks like this:
<asp:Table ID="Table1" runat="server" BackColor="White"
BorderColor="Red" BorderWidth="1px"
CellPadding="2" CellSpacing="2"
CssClass="tablecell" ForeColor="Navy"
GridLines="Both" HorizontalAlign="Center">
<asp:TableRow runat="server">
<asp:TableCell runat="server"
HorizontalAlign="Right" Wrap="False">
This is Cell 1
</asp:TableCell>
<asp:TableCell runat="server"
HorizontalAlign="Left"
VerticalAlign="Middle">
This is Cell 2
</asp:TableCell>
</asp:TableRow>
</asp:Table>From the above, you'll see the "Align" in an HTML Table is replaced by "HorizontalAlign." "Valign" is replaced with "VerticalAlign." "Nowrap" is replaced with Wrap="False."
Play around with the ASP.net Table control and you'll soon find all the missing HTML pieces.
If you View your Table in a web browser and View the Source Code, it has been generated client side to look like this. (I have added line breaks so it will fit on the screen.)
<table id="Table1" class="tablecell" cellspacing="2"
cellpadding="2" align="Center" rules="all" border="1"
style="color:Navy;background-color:White;border-color:Red;
border-width:1px;border-style:solid;">
<tr>
<td align="right" style="white-space:nowrap;">
This is Cell 1
</td>
<td align="left" valign="middle">
This is Cell 2
</td>
</tr>
</table>Nannette
I want to be a Paladin when I grow up!
More Posts