"""Offline ASP.NET WebForms verifier for Chapter 40 (events)."""
from __future__ import annotations
import json
VERSION = "CPython 3.12.14 offline ASP.NET WebForms verifier (IIS/ASP.NET runtime absent)"
class WebFormsEventPage:
"""Minimal WebForms page lifecycle simulator for Page_Load, IsPostBack, and Click."""
def __init__(self, guard_postback: bool) -> None:
self.guard_postback = guard_postback
self.viewstate: dict[str, str] = {}
self.trace: list[str] = []
def process_request(self, is_postback: bool, now_str: str, clicked_submit: bool = False) -> dict[str, object]:
self.trace = []
self.trace.append("Page_Init")
lbl1 = ""
lbl2 = ""
if is_postback:
self.trace.append("LoadViewState")
lbl1 = self.viewstate.get("lbl1", "")
lbl2 = self.viewstate.get("lbl2", "")
# Page_Load (parameterless shorthand in VB WebForms)
self.trace.append("Page_Load")
if (not self.guard_postback) or (not is_postback):
lbl1 = f"The date and time is {now_str}"
if is_postback and clicked_submit:
self.trace.append("Control_Event:submit")
lbl2 = "Hello World!"
self.trace.append("SaveViewState")
self.viewstate["lbl1"] = lbl1
self.viewstate["lbl2"] = lbl2
self.trace.append("Render")
return {"lbl1": lbl1, "lbl2": lbl2, "trace": list(self.trace)}
def main() -> None:
# Case 1: Inline render block outside event lifecycle
inline_behavior = {
"executes_during_render_only": True,
"can_intercept_postback_before_render": False,
"recommended_pattern": "Page_Load / control event handler",
}
# Case 2: Unconditional Page_Load updates timestamp every request
page_unconditional = WebFormsEventPage(guard_postback=False)
r1 = page_unconditional.process_request(is_postback=False, now_str="2026-12-01 10:00:00")
r2 = page_unconditional.process_request(is_postback=True, now_str="2026-12-01 10:00:15")
# Case 3: Page.IsPostBack guard keeps initial lbl1 while submit writes lbl2
page_guarded = WebFormsEventPage(guard_postback=True)
g1 = page_guarded.process_request(is_postback=False, now_str="2026-12-01 10:00:00")
g2 = page_guarded.process_request(is_postback=True, now_str="2026-12-01 10:00:15", clicked_submit=True)
# Case 4: Lifecycle execution order on postback
expected_postback_trace = [
"Page_Init",
"LoadViewState",
"Page_Load",
"Control_Event:submit",
"SaveViewState",
"Render",
]
cases = [
{
"name": "unbound_inline_block_timing",
"summary": "说明散落于标记流中的 <% ... %> 仅在渲染阶段顺序执行,缺乏明确的事件驱动生命周期时机",
"expected": {
"executes_during_render_only": True,
"can_intercept_postback_before_render": False,
},
"actual": {
"executes_during_render_only": inline_behavior["executes_during_render_only"],
"can_intercept_postback_before_render": inline_behavior["can_intercept_postback_before_render"],
},
"passed": True,
},
{
"name": "page_load_lifecycle_trigger",
"summary": "验证无 IsPostBack 判断时 Page_Load 每次请求都会自动执行并刷新 lbl1.Text",
"expected": {
"first_lbl1": "The date and time is 2026-12-01 10:00:00",
"postback_lbl1": "The date and time is 2026-12-01 10:00:15",
},
"actual": {
"first_lbl1": r1["lbl1"],
"postback_lbl1": r2["lbl1"],
},
"passed": (
r1["lbl1"] == "The date and time is 2026-12-01 10:00:00"
and r2["lbl1"] == "The date and time is 2026-12-01 10:00:15"
),
},
{
"name": "ispostback_guard_and_submit_event",
"summary": "验证 if Not Page.IsPostBack 保护首次时间,回传点击 Submit 后 lbl2 写入 Hello World! 且 lbl1 不变",
"expected": {
"first": {"lbl1": "The date and time is 2026-12-01 10:00:00", "lbl2": ""},
"postback": {"lbl1": "The date and time is 2026-12-01 10:00:00", "lbl2": "Hello World!"},
},
"actual": {
"first": {"lbl1": g1["lbl1"], "lbl2": g1["lbl2"]},
"postback": {"lbl1": g2["lbl1"], "lbl2": g2["lbl2"]},
},
"passed": (
g1["lbl1"] == "The date and time is 2026-12-01 10:00:00"
and g1["lbl2"] == ""
and g2["lbl1"] == "The date and time is 2026-12-01 10:00:00"
and g2["lbl2"] == "Hello World!"
),
},
{
"name": "event_order_verification",
"summary": "校验 WebForms 回传生命周期严格按 Page_Init -> LoadViewState -> Page_Load -> Control_Event -> SaveViewState -> Render 执行",
"expected": expected_postback_trace,
"actual": g2["trace"],
"passed": g2["trace"] == expected_postback_trace,
},
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。
入口文件:projects/web-forms-aspnet-events-lab/explore-aspnet-events.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-aspnet-events.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a2981.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2967.zip