The cause of the OnClick event is performed two times by clicking the button in ASP.

Source: Internet
Author: User

today wrote a simple chat room, have login page and chat room, when jumping from the login page to chat room, but found to click two times send button to send to the display area, and then send the button to run normally.

Through constant testing, it was found that the first click did not perform a click event, and the second time the Click event was performed. And also found the problem. The following is a simple small example to illustrate:

Page server_transfer.aspx:

<asp:button id= "Button1" runat= "Server" text= "button" onclick= "Button1_Click"/>//There is only one button to jump to Btnonc Lick.aspx page

Server_Transfer.aspx.cs

protected void Button1_Click (object sender, EventArgs e)
{
Server.Transfer ("btnonclick.aspx");
}

Btnonclick.aspx page

Click the Submit button to display the contents of the text box below in the text box above

The final result is: when I jump from the server_transfer.aspx page to the Btnonclick.aspx page, enter the value in the text box below, click Submit, the page will refresh, but the text box above will not show the value I entered, then enter the value again, click the Submit button, the page refreshes, the text box above will show the value I entered.

If I put

Server_Transfer.aspx.cs

protected void Button1_Click (object sender, EventArgs e)
{
Server.Transfer ("btnonclick.aspx");
}

In the Server.Transfer ("btnonclick.aspx"); instead: Response.Redirect ("btnonclick.aspx"); so long as this is not the case, enter the page click can be executed.

Now to understand the server. the difference between Transfer and Response.Redirect

Response.Redirect : BrowseDeviceASPfilePleaseAsk -ServiceServicesDevicePracticeLine -encounteredResponse.Redirectlanguagesentence -ServiceServicesDeviceHairSendResponse.Redirectthe following addressGiveGuestHouseholdsthe machine-sideBrowseDevice -BrowseDevicePleaseAskPracticethe new address of the line.
Server. Transfer:BrowseDeviceASPfilePleaseAsk -ServiceServicesDevicePracticeLine -encounteredServer.Transferlanguagesentence -ServiceServicesDeviceTurnto a new file.

With the above, when we go through the server page to jump to the new page, the browser does not know that this is a new page, from the unchanged address bar can be seen. The onclick event is to submit the required data to its corresponding page, when clicking the Submit button, it found that this is not the page he needs to submit (in fact, the browser does not know the---address and the original address is not correct) he will jump to the page he needs to operate, do not perform the onclick event, This time the address bar will jump to his original address, not the address of the previous page (I experimented, the address changed), when you click, because the address is right, he will perform the onclick event.

The cause of the OnClick event is performed two times by clicking the button in ASP.

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.