Search code examples
c#variablessharepointascx

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!


Solution

  • 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.X.Z.ClassName.

    Rebuilt your solution also.