How do I access a variable from the code behind of another page?

前端 未结 2 1324
梦如初夏
梦如初夏 2020-12-20 07:31

I have an index.aspx (index.aspx.cs) which will include the body.aspx (body.aspx.cs) using Server.execute("body.aspx");

using System;
         


        
2条回答
  •  北荒
    北荒 (楼主)
    2020-12-20 08:13

    If you mark the variable as static, it ceases to become a property of a particular instance of the page and becomes a property of the type of page.

    You can then access it as index.text1 from anywhere that can see the index class.

    However, this means that the value is then shared between every instance of this page: if it is changed by an instance of the page (or by any other class that can now see it), subsequent page loads will reflect the changed value.

    If you don't want this - if this variable should be different between each instance of the page - then what you want isn't possible. ASP.NET pages don't exist other than while they're being generated by the server, so there is no page for you to get this value from.

    If this value is never meant to change, mark it as const and you don't have to worry about things changing it.

提交回复
热议问题