Click here to Skip to main content
16,020,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I created one form where i have one button..when ever i am clicking the button control is automatically going to the page_load()

Please tell me why it is happening

below is my Page_Load() code:

VB
Dim chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
       Dim random = New Random()
       Dim result = New String(Enumerable.Repeat(chars, 5).[Select](Function(s) s(random.[Next](s.Length))).ToArray())
       'TextBox1.Text = result.ToString()
       Dim flag As Integer = 0
       Dim con As SqlConnection = New SqlConnection("Initial Catalog = ibs;Data Source = localhost;Persist Security Info=True;Integrated Security = True;")
       con.Open()
       Dim cmd As SqlCommand = New SqlCommand("insert into t1 values ('" + result + "','" + flag + "')", con)
       Dim i As Integer = cmd.ExecuteNonQuery()
       If i > 0 Then
           Response.Write("<script LANGUAGE='JavaScript'>alert('" + result + "')</script>")
       End If
Posted

1 solution

It is default behavior. When you click on button actually it postback request to the aspx page(consider asp.net webform app). When request goes to the Page(it is also a http handler) it's page life cycle events are fired. So you found that code goes to Page load event. If you want to execute code conditionally then you can use Page.IsPostBack property as a condition. In your problem i think you just add condition like If (Page.IsPostBack) before your code in form load event though next time code is not executed.
 
Share this answer
 
Comments
fjdiewornncalwe 26-Feb-13 9:01am    
+5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900