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:
Dim chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Dim random = New Random()
Dim result = New String(Enumerable.Repeat(chars, 5).[Select](Function(s) s(random.[Next](s.Length))).ToArray())
Dim flag As Integer = 0
Dim con As SqlConnection = New SqlConnection("Initial Catalog = ibs;Data Source = localhost;Persist Security Info=True;Integrated Security = True;")
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=" End If
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.
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)