Forgive my somewhat-lacking ASP.NET knowledge for this question :)
Here's the scenario: I'm playing around in VS2010, I've created a new ASP.NET Web Application
(under Visual C#, Web templates). Nothing special, just a basic web application. No fancy MVC stuff.
Included in the template-generated solution is a Login page; Account/Login.aspx
. On that page, is a Login button; the HTML looks like this:
<p class="submitButton">
<asp:Button ID="LoginButton"
runat="server"
CommandName="Login"
Text="Log In"
ValidationGroup="LoginUserValidationGroup"/>
</p>
Again, nothing fancy. Now, the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Account_Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
}
}
Even less fancy. And finally, from web.config:
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
</authentication>
So, here is what I can't quite figure out: when I run the app, I can click the Login button, and the app does something - but where is the code for that something?
Coming from the WPF world (with some background in ASP.NET), my first instinct is to zero-in on CommandName
- but a search for anything relating to "Login" turns up dry. My second instinct is to look at the code-behind, but again, it's pretty sparse and I don't see anything that looks like it has anything to do with a Login button being clicked.
So where does the "magic" behind this button happen? There has to be something; I feel like I've overlooked something that is sitting right in front of my eyes.
If you're referring to the Default Web Application (not the empty) that comes out of the box with Visual Studio, you'll notice that login button is inside an <asp:Login>
server control. That's where the magic is happening. It interfaces with the ASP.NET Membership provider if you look in the web.config, you'll see references to that.
The page is posting back. but there is no event handler wired up so it doesn't do anything. I knows how to do this by the code that gets generated when the button is added. The button is generating some JavaScript code that calls a function to post-back the page.
You'll need to add an event handler. The easiest way is just to double-click the button on the form designer and it will generate the code for you.
The Button
is a server side control - this causes it to post back when clicked.
If you look at the source of the page, you will see a call to __dopostback
, a javascript function that posts the page back to itself.
At this point, the page reloads, which is probably what you are seeing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With