访问ascx.cs中的变量以在.ascx webpart中使用它们

时间:2018-01-15 13:03:53

标签: c# variables sharepoint ascx

我在" Mapas.ascx.cs"中声明了一个变量。我希望在#34; 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>

任何人都可以帮助我吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

如果您想在.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这样的名称空间。

重建你的解决方案。

相关问题