访问 ascx.cs 中的变量以在 .aspx Web 部件中使用它们
Accesing variables in ascx.cs to use them in .ascx webpart
我在 "Mapas.ascx.cs" 中声明了一个变量,我想在 "Mapas.ascx" 中使用它,但我不知道该怎么做。 (我使用的是 sharepoint2013)
我的变量是这样声明的:
public string apiKey, direccion;
我想使用它的代码是:
<iframe width="600" height="450" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?q=place_id:"DIRECCIONVARIABLE"&key="APIKEYVARIABLE" allowfullscreen></iframe>
谁能帮帮我?非常感谢!
如果你想在.ascx
设计代码中调用方法或属性,你应该将它们定义为public static
,
public static string ApiKey
{
get
{
return "Name";
}
}
public static string DoSomething()
{
//Code here
return "What you expected";
}
然后在aspx
设计代码中:
<span><%= yourClassName.DoSomething()%> </span>
<span><%= YourClassName.ApiKey %> </span>
更新:
例如你有这样的东西:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ClassName.ascx.cs" Inherits="YourNameSpace.X.Z.ClassName" %>
在页面顶部的 ascx
设计代码中,因此,如果您无法通过 ClassName.ApiKey
访问它们,那么请将命名空间写成 YourNameSpace.X.Z.ClassName
.
同时重建您的解决方案。
我在 "Mapas.ascx.cs" 中声明了一个变量,我想在 "Mapas.ascx" 中使用它,但我不知道该怎么做。 (我使用的是 sharepoint2013)
我的变量是这样声明的:
public string apiKey, direccion;
我想使用它的代码是:
<iframe width="600" height="450" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?q=place_id:"DIRECCIONVARIABLE"&key="APIKEYVARIABLE" allowfullscreen></iframe>
谁能帮帮我?非常感谢!
如果你想在.ascx
设计代码中调用方法或属性,你应该将它们定义为public static
,
public static string ApiKey
{
get
{
return "Name";
}
}
public static string DoSomething()
{
//Code here
return "What you expected";
}
然后在aspx
设计代码中:
<span><%= yourClassName.DoSomething()%> </span>
<span><%= YourClassName.ApiKey %> </span>
更新: 例如你有这样的东西:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ClassName.ascx.cs" Inherits="YourNameSpace.X.Z.ClassName" %>
在页面顶部的 ascx
设计代码中,因此,如果您无法通过 ClassName.ApiKey
访问它们,那么请将命名空间写成 YourNameSpace.X.Z.ClassName
.
同时重建您的解决方案。