【发布时间】:2021-09-30 15:29:14
【问题描述】:
我希望在我的 Masterpage 后面的代码中执行代码,并在 Default.aspx 等子页面的 .aspx 页面上使用它,而无需通过 Default.aspx.cs 页面调用它。
这是我尝试像<% MasterPage.getPlanCost() %> 那样访问它,但是,这不起作用。因为getPlanCost()没有“定义”
后面的母版页代码:
public string getPlanCost()
{
var country = getCountry();
string gbp = "£5.99";
string euro = "€6.99";
string usd = "$8.99";
var currencyCost = usd;
if (country == "United Kingdom") // gbp
{
currencyCost = gbp;
}
else if (country == "United States" || country == "Canada" || country == "Australia" || country == "New Zealand") // usd
{
currencyCost = usd;
}
else // euro
{
currencyCost = euro;
}
return currencyCost;
}
Default.aspx 页面:
<p class="text-center under-title text-muted"><%=MasterPage.getPlanCost() %> Cancel Anytime.</p>
实现这一目标的最快/最有效的方法是什么?此外,我尝试使用在 StackOverflow 上看到的替代方法,使用 get 和 set 但我无法使其正常工作。对 C# 相当陌生,所以我很抱歉。
【问题讨论】:
-
您需要将
getPlanCost()代码移动到其他地方,这两个地方都可以使用。 -
哪里像?肯定可以通过 .ASPX 页面访问 MasterPage?
-
不,aspx 甚至不必知道它在母版页中。