简体   繁体   中英

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. (I'm using 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 ,

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

then in aspx design code:

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

Rebuilt your solution also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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