1. IsPostBack的官方解释:
获取一个值,该值指示页是第一次呈现还是为了响应回发而加载。
如果是为响应客户端回发而加载该页,则为 true;否则为 false。
2. 做如下试验:
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 this.txtHidden.Value = "!IsPostBack";
6 }
7 }
8
9 protected void Button1_Click(object sender, EventArgs e)
10 {
11 Response.Write("<script type='text/javascript'>alert('"
2 {
3 if (!IsPostBack)
4 {
5 this.txtHidden.Value = "!IsPostBack";
6 }
7 }
8
9 protected void Button1_Click(object sender, EventArgs e)
10 {
11 Response.Write("<script type='text/javascript'>alert('"
+ this.txtHidden.Value + "');</script>");
12 }
结果:!IsPostBack
稍作改动
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 this.txtHidden.Value = "!IsPostBack";
6 }
7 else
8 {
9 this.txtHidden.Value = "IsPostBack";
10 }
11 }
12
13 protected void Button1_Click(object sender, EventArgs e)
14 {
15 Response.Write("<script type='text/javascript'>alert('"
2 {
3 if (!IsPostBack)
4 {
5 this.txtHidden.Value = "!IsPostBack";
6 }
7 else
8 {
9 this.txtHidden.Value = "IsPostBack";
10 }
11 }
12
13 protected void Button1_Click(object sender, EventArgs e)
14 {
15 Response.Write("<script type='text/javascript'>alert('"
+ this.txtHidden.Value + "');</script>");
16 }
结果:IsPostBack
这里说明,如果需要在页面加载时给控件赋值,在!IsPostBack中进行;
由页面逻辑操作改变控件的值,可以在IsPostBack中进行。