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.