Click here to Skip to main content
15,911,524 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi..

i have create one user controll for map driving direction.
and in this controll i have declared to property like fromaddress and toaddress but problem is i can't access this property in may aspx.cs page
my code is here..

<pre lang="c#">
<script runat="server">
    private Unit width, height;
    private string fromAddress, toAddress;
    private string mapElementID = string.Empty;
    private bool autoLoad = true;
    
    //Api Key related
    private string apikey = string.Empty;
    const string apikey_localhost = "ABQIAAAAn3Z0cRcS4vYvtWFgxDT-XxQJG1Plr3ELCWOTj1GK3462JN6umRRq3gfzRVjeS1XDH3ssgRa4stz-uA";
    const string api_include = "http://www.google.com/jsapi?key={0}" ;

    public bool AutoLoad
    {
        get { return autoLoad; }
        set { autoLoad = value; }
    }

    public string APIKey
    {
        get { return apikey; }
        set { apikey = value; }
    }
    
    
    public string MapElementID
    {
        get { return mapElementID; }
        set { mapElementID = value; }
    }
    
    public Unit Width
    {
        get { return width; }
        set { width = value; Directions.Style[HtmlTextWriterStyle.Width] = width.ToString(); }    
    }

    public Unit Height
    {
        get { return height; }
        set { height = value; Directions.Style[HtmlTextWriterStyle.Height] = height.ToString(); }
    }

    public string FromAddress
    {
        get { return fromAddress; }
        set { fromAddress = value; }
    }

    public string ToAddress
    {
        get { return toAddress; }
        set { toAddress = value; }
    }
    
    protected override void OnPreRender(EventArgs e)
    {
        string apiURL = null;
        if (Request.Url.IsLoopback)
            apiURL = string.Format(api_include, apikey_localhost);
        else
        {
            if (apikey.Length > 0) //if an api key is specified
                apiURL = string.Format(api_include, apikey);
            else
            {
                Directions.InnerHtml = "You need to specifiy a Google API key for this host: " + Request.Url.Host;
            }
        }
        
        if (apiURL != null)
        {
            Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "API_KEY_REFERENCE", apiURL);

            //if (toAddress.Length > 0 && fromAddress.Length > 0) //We can render directions
            //   Page.ClientScript.RegisterStartupScript(this.GetType(), "DD_LOAD", "_nco_dd.loadDirections();", true);
            
        }
            
        base.OnPreRender(e);
    }
    
</script>
<div  runat="server" id="Directions">
GoogleMaps Driving Directions<br />
No api key is needed for running it on localhost.<br />
But you will need an api key for the production URL.<br />
You may obtain a key from code.google.com<br />
</div>

<script type="text/javascript">
//<![CDATA[
    
    function _nco_gdir()
    {
        this.fromAddress = "<%=fromAddress%>";
        this.toAddress = "<%=toAddress%>";
        this.mapElementID = "<%=mapElementID%>";
        this.autoLoad = "<%=autoLoad.ToString().ToLower() %>";
        this.directions = null;
        var self = this; //don't ask
        google.load("maps","2"); //load version 2 of GoogleMaps API

        this.handleErrors = function()
        {
            
            if(self.divMap) self.divMap.innerHTML = "";
            if (self.directions && self.directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
            {
                alert("Driving directions are not available for the address you entered.\nThis may be because the from address is relatively new, or it may be incorrect.");                
            }
            else
            {
                alert("We are currently unable to display directions for the specified addresses.");
            }
        };
                
        this.loadDirections = function()
        {
            if(!self.directions)
            {
                if(self.mapElementID.length>0)
                {
                    self.divMap = document.getElementById(self.mapElementID);
                    
                    if(self.divMap!=null)
                    {
                        self.dirMap = new google.maps.Map2(self.divMap);
                        self.dirMap.addControl(new google.maps.LargeMapControl());					    
                        self.dirMap.addControl(new google.maps.ScaleControl());        
                    }
                    else
                        self.dirMap = null;
                }
                
                self.dirpanel = document.getElementById("<%=Directions.ClientID%>");
                self.directions = new google.maps.Directions(self.dirMap, self.dirpanel);
                google.maps.Event.addListener(self.directions, "error", self.handleErrors);
            }
            
            //Clear Directions, if any
            self.dirpanel.innerHTML = "";

            //if(this.divMap!=null)
               // this.divMap.innerHTML = "Loading Map... Please wait";
            
            var ft = "from: " +  self.fromAddress + " to: " + self.toAddress;
                
            self.directions.load(ft);
            
        };
        
        if(this.fromAddress.length>0 && this.toAddress.length>0 && this.autoLoad)
            google.setOnLoadCallback(this.loadDirections); //The google way to register to window.onload
    }
    
    _nco_dd = new _nco_gdir();
            
//
</script>     


And my aspx.cs error is here user controll name is "gdirections"
C#
gdirections.FromAddress = fromAddress;//i can not able to access FromAddress
gdirections.ToAddress = toAddress;//i can not able to access ToAddress
gdirections.AutoLoad = true; //load directions automatically
Posted

1 solution

Hi Zarna,
If you are trying to access the variable fromaddress and toaddress of your user control(gdidirections) then you must declare them as public because you can't access the private variables outside of your control if they are private.
 
Share this answer
 
Comments
Ankur Ramanuj 22-Jun-13 6:27am    
hi,
i have done this but its not working steel.and user controls code is in html side in script

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900