Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WebForms VB.NET
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:
 
 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 25-Feb-13 22:10pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Marcus Kramer at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 305
1 OriginalGriff 225
2 DamithSL 130
3 Kornfeld Eliyahu Peter 130
4 Peter Leow 95
0 OriginalGriff 7,355
1 DamithSL 5,199
2 Sergey Alexandrovich Kryukov 4,942
3 Maciej Los 4,906
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100