确定回发是 page_load 还是 itemcommand

发布于 2024-07-17 13:41:44 字数 153 浏览 7 评论 0原文

我在数据网格中的页面左侧有一个用户列表,我想在页面右侧加载一个包含单击用户信息的 div。 我假设我将在 itemcommand 事件中加载 div,但是如何处理页面加载呢? 我需要知道是什么导致了回发吗? 我是否还需要在 page_load 上重新加载网格?

I've got a user list on the left side of my page in a datagrid and I want to load a div in the right side of my page with the clicked user's information. I assume I'll do the load of the div in the itemcommand event, but how do I handle page load then? Do I need to know what caused the postback? Do I need to reload the grid on page_load as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

套路撩心 2024-07-24 13:41:45

那么我该如何处理页面加载?

每次回发都会使用页面类的全新实例,并完全重建页面。 唯一不同的是,某些控件可能是通过 ViewState 预先填充的。 您不需要为任何这些控件重复加载工作。

我需要知道导致回发的原因吗?

ASP.Net 将为您处理该事件并触发该事件。 您只需要知道它是否是回发,甚至可能不需要知道(请参阅下一部分)。

我还需要在 page_load 上重新加载网格吗?

是的。 嗯,有点像。 您需要将网格的 html 重新渲染到浏览器。 好消息是您的网格数据可能已经处于视图状态并且它会自动发生。 您不需要自己担心。

但是,在许多情况下,您可能会发现最好关闭网格的视图状态并在每次回发时重新加载它们。 这是因为 ViewState 只是页面上的隐藏输入,必须通过每个请求将其发布(上传)到服务器。 大多数互联网用户的上传带宽非常有限,因此较大的 ViewState 可能会让您的网站显得缓慢,即使您的服务器几乎没有出汗。

根据您的具体情况,您可以通过禁用所选控件上的 ViewState,以牺牲一些多余服务器性能来换取站点响应能力,效果可能会更好。 在这种情况下,您将始终加载网格,并且不再需要关心请求是否是回发。

相比之下,如果这是一个内部局域网应用程序,其中用户通常通过本地以太网连接到您的 Web 服务器,那么在平衡响应能力和服务器性能方面很难击败 ViewState。

how do I handle page load then?

Every postback uses a brand new instance of your page class, and completely rebuilds the page. The only thing that's different is that some controls might be pre-populated via ViewState. You don't need to repeat the load work for any of those controls.

Do I need to know what caused the postback?

ASP.Net will handle that for you and fire the event. You only need know whether it is a postback, and maybe not even that (see the next part).

Do I need to reload the grid on page_load as well?

Yes. Well, sort of. You need to re-render the html for the grid to the browser. The good news is odds are your grid data is already in viewstate and it will happen automatically. You don't need to worry about it yourself.

However, in many cases you may find that it's better to turn off viewstate for grids and reload them on each postback anyway. This is because ViewState is just a hidden input on your page that must be posted (uploaded) to the server with each request. Most internet users have very limited upload bandwidth, and so a large ViewState can make your site seem slugish, even if your server is hardly breaking a sweat.

Depending on your situation, you might do better by trading some excess server performance for site responsiveness by disabling ViewState on select controls. In this case, you will always load the grid and no longer need care whether or not a request is a postback.

By contrast, if this is an intranet application where users typically have local ethernet connections to your web server it's hard to beat ViewState for balancing responsiveness and server performance.

暮凉 2024-07-24 13:41:45

通过检查 !IsPostBack For div itemcommand 排除网格绑定,

if(!IsPostBack){
   // Bind the Grid
}

您可以检索从网格中单击的值并加载用户信息。

因此,您不需要每次都加载网格。 它只会在第一次加载。 我认为您不需要检查导致回发的原因,因为无论如何您只会加载网格一次。

Exclude the grid binding with checking !IsPostBack

if(!IsPostBack){
   // Bind the Grid
}

For div itemcommand you get retrieve the value of which is clicked from grid and load the user's information.

So, you will not required to load the grid everytime. It will just load first time. I dont think you will required to check what cuased the postback, because anyway you will load grid once only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文