Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我想在每个 .aspx 页面上运行相同的功能。例如,假设此函数始终生成 UTC DateTime 和用户的 DateTime。如何在一个地方编写代码来计算每次页面加载的这些值,然后使它们的值可用于整个 .aspx 页面?我意识到我可以在每个页面中声明这些变量然后调用该函数,但是没有办法编写一次以避免重复编写相同的代码吗?谢谢。
我建议使用母版页。即使您在母版页中看不到任何内容,也可以让其 onload 或 oninit 进行计算,并将其存储为母版页的属性。
public Date date{get;set;} protected void onLoad(){ this.date = ... calculate ....; }
然后从子页面您可以访问母版页
((YourMasterClassName)this.Master).date