Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WebFormsVB.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 21: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
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 450
1 Sergey Alexandrovich Kryukov 325
2 Shai Vashdi 318
3 OriginalGriff 265
4 Peter Leow 171
0 Sergey Alexandrovich Kryukov 9,109
1 OriginalGriff 5,290
2 Peter Leow 4,010
3 Maciej Los 3,535
4 Abhinav S 3,263


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