user controll无法访问在ascx页面中定义的属性。 [英] user controll can't access property which define in ascx page.

查看:72
本文介绍了user controll无法访问在ascx页面中定义的属性。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..



i为地图行车方向创建了一个用户控件。

并且在此控件中我已向财产声明fromaddress和toaddress,但问题是我无法在aspx.cs页面访问此属性

我的代码在这里..



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>     





我的aspx.cs错误在这里用户控制名称是gdirections



And my aspx.cs error is here user controll name is "gdirections"

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

推荐答案

嗨Zarna,

如果您正在尝试访问用户控件的变量fromaddress和toaddress(gdidirections),那么您必须将它们声明为public,因为您可以如果它们是私有的,则访问控制之外的私有变量。
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.


这篇关于user controll无法访问在ascx页面中定义的属性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆