Click here to Skip to main content
13,299,727 members (59,731 online)
Rate this:
Please Sign up or sign in to vote.
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:

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;")
       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
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 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.
Marcus Kramer 26-Feb-13 9:01am

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171207.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2017
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