The linkbutton is submitted twice to solve the problem.

Source: Internet
Author: User
Today, I solved a seemingly strange problem (it was not surprising to think about it later, but it was just a day's work. Program ). The problem is that, in my Asp.net application, click the linkbutton to insert the database. The results show that two records are generated in the database. At the beginning, it was a bit technical. Later I thought Code And stored procedure. So I searched carefully and found that my button was executed twice by clicking the event handler. After analysis, it turns out to be like this.

In my. aspx file, the linkbutton code is as follows:

< ASP: linkbutton Runat = "Server" ID = "Updatebutton" Onclick = "Insert" Text = "Add" >
Add </ ASP: linkbutton >

The following code is available in my. CS file:

Protected   Void Insert ( Object Sender, system. eventargs E)
{
//Insert data
}

And Private   Void Initializecomponent ()
{
This . Updatebutton. Click + =   New System. eventhandler ( This . Insert );
This . Load + =   New System. eventhandler ( This . Page_load );

}

I think everyone understands that the reason why the code is executed twice is that there is an onclick in the HTML code.= "Insert", and a click Time is processed later. Therefore, I remove the onclick code from the HTML code. The result is normal. After thinking about it, sometimes you need to click a button and then execute the code twice. Can this method be used. Haha, but I am very happy to solve the problem that has plagued me for more than an hour.

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.