Login control in updatepanel. After the user information is verified successfully, the page is refreshed.

Source: Internet
Author: User
With Ajax. Net beta 2 Released today, let's see Ajax And ASP. net2.0 Quick and efficient combination, we don't even need to write a sentence JS CodeYou can make ASP. NET The web page is written in the past several hours. JS The implementation of the Code is not refreshing. And integrate all of this ASP. NET It is also so easy, just move the control Updatepanel You can set several parameters in the control. However Ajax. net Developers can also find Ajax. net Sometimes it is not perfect. Just as I recently met ASP. net2.0 Of Login Control in In updatepanel, after successfully verifying the user information, the page will be refreshed. Obviously, this is against the principle of no refreshing Ajax, After analysis Login The following code discovers the built-in user identity authentication method of the 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 the user information is verified, write the cookie information for the client.
Formsauthentication. setauthcookie ( This . Usernameinternal, This . Remembermeset );
This . Onloggedin (eventargs. Empty );

//This is the following response statement. If you execute the redirection operation in the updatepanel control, the page will be refreshed!
This. Page. response. Redirect (This. Getredirecturl (),False);
}
}
}

Through analysisAttemptloginThe method is not hard to see, when we pressLoginControl LogonButtonAfter the user information is verified, the following statement is executed:Response. RedirectPage turning statement(This code is executed even if no redirection page is specified. The default value is the current page.)The page is refreshed because the page turns. It is easy to understand the cause of the Error. Someone may say that the custom control inheritsLoginControl and overrideAttemptloginBut is there any simpler method besides custom controls? The answer is yes. Since the built-in verification mechanism causes page refresh, simply do not useLoginControl, and use a custom method to verify the user identity. First, to use the custom verification method, we first findLoginControl, convert it to a template, and then findLoginbuttonThis controlCommandname= "Login"Removed, so that the control will no longer use the built-in method to verify user information.LoginbuttonAddOnclickThe Code is 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 ))
{
// According to the above analysis of the login authentication mechanism, write cookies for the client.
Formsauthentication. setauthcookie (login1.username, login1.remembermeset );
// Perform some processing here after the verification is successful, such as hiding the login Control
Login1.visible = false;
}
Else
{
// Because the built-in verification mechanism is not used, you must set the process for failed verification.
(Login1.findcontrol ( " Failuretext " ) As Literal). Text =   " Incorrect user name or password. Please try again! " ;
}
}

Analyze the code above, becauseLoginThe user information to be verified by the control is stored inSql2005OfAspnetdbDatabaseAspnet_membershipTable, so we only need to useMembership. validateuserThis method can easily verify the user information. After the verification is successfulAttemptloginThe method is written on the client.CookieAfter setting the verification failure error information, we can easilyLoginAfter the control is transformed to a user information verified successfully, it is no longer refreshed. The advantage of the transformation is that it is not as complicated as writing a custom control, and the effect and the originalLoginControls are identical and can still be used.CreateuserwizardThe user name created by the control is verified, and loginstatus, loginname, and other login control-related controls can also be used as usual.

PS: If the pagerequestmanagerparsererrorexception error occurs when the login control verifies the user information, check whether Web. config has this sentence:
<Httpmodules>
.....
<Add name = "scriptmodule" type = "Microsoft. Web. UI. scriptmodule, Microsoft. Web. Extensions, version = 1.0.61025.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/>
</Httpmodules>

Thanks for the reminder from saucer.

Download the complete code of this Article

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.