如何确定IsPostBack的代码段 [英] How to decide code section for IsPostBack

查看:89
本文介绍了如何确定IsPostBack的代码段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专家,

我正在开发c Sharp asp.net应用程序.

其中有一页 subscribe.aspx

在此页面中,用户填写信息.经过所有验证后,当用户单击提交"时,我会将这些数据发送到subscribepost.aspx页面进行进一步处理.此页面包含页面加载时的插入代码和其他一些代码.

现在我很困惑.我已经在简单的页面加载中编写了此代码.
这是正确的方法还是我必须在 IsPostBack block
中编写它
请帮帮我.

预先感谢

Hi Expert ,

I am developing c sharp asp.net application.

In this there is one page subscribe.aspx

In this page user fill information. after all validation, when user clicks submit, I am sending this data to subscribepost.aspx page for further processing. This page contains insert code on page load and some other code.

Now I am confused. I had written this code on simple pageload.
IS it right approach or I have to write it in IsPostBack block

Please help me.

Thankssssssssssss in advance

推荐答案

简单来说,您可以采用以下方法:

In simple words, you can follow this approach :

If(!IsPostBack)
{
    //Write that code which is to be called only on first page load and not 
    //everytime page postback occurs.
}

//Rest of the code will go here after the curly braces. 
//Remember, this code will be called everytime page loads
//May it be Postback, first page load.



希望这会有所帮助.
一切顺利.



Hope this helps.
All the best.


由您决定,
IsPostBack property indicates that whether this is the first time user has requested for the page or it is reloaded based on any response on postback.

This property checks for __VIEWSTATE or __EVENTTARGET parameter in Request object. if these parameters are absent that means it is requested for the first time and if these parameters are present then this request is not first request.




ispostback内部的代码将首次执行,如果您单击页面上的任何其他控件(如按钮单击),则不会调用ispostback部分内部的代码.




Code inside the ispostback will execute the first time,if you are clicking any other control of the page like button click then code inside the ispostback section would not called.


首先,实现对 isPostBack .
根据页面生命周期.在每个请求页面上创建并发行. Page_load事件被调用多次. (例如,当您单击按钮"时,刷新页面)

isPostBack 用于需要在首页加载中执行代码的情况.当由于诸如按钮单击之类的其他事件而调用页面加载时,则不会.

isPostBack 检查该页面是否已经发布到服务器.

您只需要在第一个Page_load上完成任务即可.放得更好
在代码中 If(!IsPostBack).
first, realize the use of isPostBack.
According to pagelife cycle. on each request page get''s created and distroyed. Page_load event get called several times. (e.g. when u click on Button, refresh page)

isPostBack is used when u need to execute your code on first page Load. not when page load called due to other events like Button Clicked.

isPostBack checks that page is already posted to server or not.

You need to caried out your task on first Page_load only. so better to put
If(!IsPostBack) in your code.


这篇关于如何确定IsPostBack的代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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