页面按钮单击时在内部自动加载

问题描述:

大家好吧



我创建了一个表单,我有一个按钮..当我点击按钮控件时会自动转到page_load()



请告诉我它为什么会发生



以下是我的Page_Load()代码:



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

这是默认行为。当你点击按钮实际上它回发请求到aspx页面(考虑asp.net webform应用程序)。当请求进入页面(它也是一个http处理程序)时,它的页面生命周期事件被触发。所以你发现代码转到了Page load事件。如果要有条件地执行代码,则可以使用Page.IsPostBack属性作为条件。在您的问题中,我认为您只需在表单加载事件中的代码之前添加If(Page.IsPostBack)等条件,但下次代码不会执行。
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.