页面按钮单击时在内部自动加载 [英] Page is loading internally autometically on button click

查看:92
本文介绍了页面按钮单击时在内部自动加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好吧



我创建了一个表单,我有一个按钮..当我点击按钮控件时会自动转到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.


这篇关于页面按钮单击时在内部自动加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆