Login control in UpdatePanel solution to page refresh when validation of user information succeeds

Source: Internet
Author: User
Tags client
Resolve | control | refresh | page

With the Ajax.NET BETA 2 released today, let us see the Ajax and asp.net2.0 close combination of fast and efficient, we can even do not need to write a JS code can let the asp.net of the page to get the past to spend hours to write the JS code to achieve no refresh effect. It is also easy to combine all of this into ASP.net, simply move the control to a UpdatePanel control and set several parameters. However, at the same time as the experience Ajax.NET brings to the developer convenience, also can discover ajax.net sometimes also not perfect. Just like the author recently encountered asp.net2.0 login control in the UpdatePanel after the successful validation of user information will refresh the page bug, obviously this is against the Ajax no refresh principle, The following code was found after analyzing the user authentication method built into the login control:

private void Attemptlogin ()
{
LoginCancelEventArgs args1 = new LoginCancelEventArgs ();
This. Onloggingin (ARGS1);
if (!ARGS1. Cancel)
{
AuthenticateEventArgs args2 = new AuthenticateEventArgs ();
This. OnAuthenticate (ARGS2);
if (ARGS2. Authenticated)
{
After user information has been validated successfully, write cookie information to the client.
Formsauthentication.setauthcookie (this. Usernameinternal, this. Remembermeset);
This. Onloggedin (Eventargs.empty);

This is the following sentence response statement mischief, in the UpdatePanel control to perform a steering operation caused the page to refresh!
This. Page.Response.Redirect (this. Getredirecturl (), false);
}
}
}


By analyzing the Attemptlogin method It is not difficult to see that when we press the login button of the login control and successfully validate the user's information, we execute a response.redirect page-turn statement (even if no turning page is specified, the current page is tacitly assumed). And it is because of the implementation of the page's turn and caused the page to refresh. It would be nice to know the cause of the error and then someone would say that the custom control inherits the login control and overrides the Attemptlogin method, but is there a simpler way to do it than to customize the control? The answer is yes, since the built-in validation mechanism causes the page to refresh, then simply does not use the login control's validation processing, but uses the custom method to handle authenticates the user identity. First, in order to use a custom authentication method, we first locate the Login control and convert it to a template, then find the Loginbutton control inside the template and remove commandname= "Login" so that the control no longer uses the built-in method to authenticate the user's information. Then we add an onclick event to the Loginbutton code as follows:

protected void Loginbutton_click (object sender, EventArgs e)
{
Verify that the user name and password are correct
if (Membership.ValidateUser (Login1.username, Login1.password))
{
Write a cookie for the client based on the verification mechanism of the analysis login above.
Formsauthentication.setauthcookie (Login1.username, Login1.remembermeset);
After successful validation, you can do some processing here, such as hiding the login control
Login1.visible = false;
}
Else
{
Because the built-in validation mechanism is not used, the process of validation failure should be set itself.
(Login1.findcontrol ("FailureText") as Literal). Text = "User name or password is incorrect, please try again!" ";
}
}

Analyze the above code, where the user information to be validated by the login control is stored in the Aspnet_membership table of the SQL2005 ASPNETDB database. So we just use membership.validateuser this method can easily verify the user information, when the validation is successful, according to the above analysis of the Attemptlogin method for the client to write cookies, and then set the validation failure of the error message will be easy to change our login control Created to successfully authenticate user information and then no longer refreshes, the benefits of this transformation are not as complex as writing custom controls, and the effect is identical to the original login control, and can still be validated using the username created by the CreateUserWizard control. Controls related to login controls such as LoginStatus and LoginName can also be used as usual.

PS: If a pagerequestmanagerparsererrorexception error occurs when the login control verifies user information, check to see if Web.config has this sentence:
.....
<add name= "Scriptmodule" type= "Microsoft.Web.UI.ScriptModule, Microsoft.Web.Extensions, version=1.0.61025.0, Culture=neutral, publickeytoken=31bf3856ad364e35 "/>

Thank Saucer Reminder



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.