简体   繁体   English

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

[英]Accesing variables in ascx.cs to use them in .ascx webpart

I have a variable declared in "Mapas.ascx.cs" and I want to use it at "Mapas.ascx", but I don't know how to do it. 我有一个在“ Mapas.ascx.cs”中声明的变量,我想在“ Mapas.ascx”中使用它,但我不知道该怎么做。 (I'm using sharepoint2013) (我正在使用sharepoint2013)

My variable was declared like that: 我的变量被这样声明:

    public string apiKey, direccion;

And the code where I want to use it is that: 我要使用的代码是:

<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>

Anyone can helps me? 有人可以帮助我吗? Thanks a lot! 非常感谢!

If you want call method or property in .ascx design code, you should define them as public static , 如果您想要.ascx设计代码中的调用方法或属性,则应将它们定义为public static

public static string ApiKey
{
    get
    {
        return "Name";
    }
}
public static string DoSomething()
{
    //Code here
    return "What you expected";
}

then in aspx design code: 然后在aspx设计代码中:

 <span><%= yourClassName.DoSomething()%> </span>
 <span><%= YourClassName.ApiKey %> </span>

UPDATE: For example you have something like: 更新:例如,您有以下内容:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ClassName.ascx.cs" Inherits="YourNameSpace.X.Z.ClassName" %>

in your ascx design code in top of the page , so if you can't access them by ClassName.ApiKey then write the namespace like YourNameSpace.XZClassName . 在页面顶部的ascx设计代码中,因此,如果无法通过ClassName.ApiKey访问它们,则编写名称空间,例如YourNameSpace.XZClassName

Rebuilt your solution also. 也重建您的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM