vb.net 在 Page_Load 子例程中使用 .IsPostBack

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13651839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:24:07  来源:igfitidea点击:

Using .IsPostBack in the Page_Load sub routine

asp.netvb.netispostback

提问by Emad-ud-deen

Is it a "Best Practice" to always use .IsPostBack in the Page_Load sub routine of a web form like in this example coding?

像本示例编码一样,始终在 Web 表单的 Page_Load 子例程中使用 .IsPostBack 是“最佳实践”吗?

I hope it's ok to ask this question. If not, I will immediately remove the question.

我希望可以问这个问题。如果没有,我会立即删除问题。

Basically I want to code the way most of you are coding.

基本上,我想以你们大多数人编码的方式进行编码。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then

        ' More coding will go here.
        '--------------------------
    End If

Please give pros and cons for it's usage.

请给出其使用的优缺点。

回答by Darren Wainwright

It's not so much a case of "Best Practice" but more a case of whether you need to use it at all.

这与其说是“最佳实践”的案例,不如说是您是否需要使用它的案例。

It's true, you would normally put IsPostBackin the Page_Load, though you could also put it in the Page_Init- basically in any of the page events that fire before rendering out the HTML.

这是真的,你通常会放IsPostBackPage_Load,不过你也可以把它放在Page_Init-基本上在任何页面的事件是呈现出先火HTML

You're essentially using the command to, in this case, prevent the code in the body from firing when the page posts back to itself; such as a form submission or AutoPostBackon a server control, for example the DropDownList.

在这种情况下,您实际上是在使用该命令来阻止正文中的代码在页面回发到自身时触发;例如表单提交或AutoPostBack在服务器控件上,例如DropDownList.

There aren't any, at least that I can think of, pro's and con's. Its a case of need or don't.

没有任何,至少我能想到的,优点和缺点。它是一个需要或不需要的情况。

An example of when you would ideally need it would be only wanting to get data from the database once and bind it to a DropDownList. This data would be available in the viewstate when you postback. so you wouldn't need to visit the database again on postback.

理想情况下何时需要它的一个示例是只想从数据库中获取一次数据并将其绑定到DropDownList. 当您回发时,此数据将在视图状态中可用。所以你不需要在回发时再次访问数据库。

An example of when you wouldn't put code in it is if you were generating server controls (button for example) that have an event handler, such as click, added at the same time. this would need to be re-generated on postback for the event handler to be available.

不将代码放入其中的一个示例是,如果您正在生成click同时添加了事件处理程序(例如 )的服务器控件(例如按钮)。这需要在回发时重新生成,以便事件处理程序可用。

回答by webNoob

The benefit is that you can do your expensive operations only once. Binding to gridView...etc.

好处是您只能执行一次昂贵的操作。绑定到 gridView...等。

Mostly stuff you do not want to perform during a refresh.

大多数是您不想在刷新期间执行的内容。

回答by slfan

It always depends on what you want to optimize. If your initialization code takes a long time, it is better to do it only the first time and let your controls be initialize through ViewState. Then you use If Not IsPostBack.

这始终取决于您要优化的内容。如果你的初始化代码需要很长时间,最好只在第一次做,让你的控件通过 ViewState 进行初始化。然后你使用If Not IsPostBack.

But if you target for mobile devices where bandwidth is more important, you might turn of the ViewState and initialize your data again on postbacks (you could read it from Cache or from SessionState). Always watch your ViewState, I have seen pages with 20 kByte ViewState or more.

但是,如果您针对带宽更重要的移动设备,您可能会关闭 ViewState 并在回发时再次初始化您的数据(您可以从缓存或会话状态中读取它)。始终注意您的 ViewState,我见过具有 20 kByte ViewState 或更多的页面。

Pros:

优点:

  • less overhead for initialization (e.g. access to database)
  • less memory on server (session or cache)
  • 更少的初始化开销(例如访问数据库)
  • 服务器上的内存减少(会话或缓存)

Contra:

反对:

  • more bandwith for ViewState
  • 更多的 ViewState 带宽