简体   繁体   中英

Cannot get instance of usercontrol from aspx page

I have a usercontrol and inside the html for the usercontrol, I have the following definition for a Popup control which I am trying to access from my aspx page via javascript.

<dx:ASPxPopupControl ID="MainASPxPopupControl"
                ClientInstanceName="MainASPxClientPopupControl"
                runat="server"
                HeaderStyle-HorizontalAlign="Left"
                RenderMode="Lightweight"
                AutoUpdatePosition="true"
                PopupHorizontalAlign="WindowCenter"
                PopupVerticalAlign="WindowCenter"
                OnInit="MainASPxPopupControl_Init">

In the code-behind for the usercontrol, I have the following property so my aspx page will be able to access it:

public static object MainASPxClientPopupControl { get; set; }

In the code behind of my aspx page in the Page_load event, I have the following. This prevents the error: "The Controls collection cannot be modified because the control contains code blocks”

Page.Header.DataBind();

In my JS, instead of using:

<%=

which will cause the above error, I am using:

<%#

which is a databinding expression and need for the snippet in my page_load method.

Here is where the real problem lies... I have code in a JS function on my aspx page that gets hit, but always displays that the object is null. I am simply trying to get an instance of the object (the popup control in the usercontrol) where I can use it accordingly.

var cntrl = document.getElementById('<%# BMCIS.Source.Controls.ManagerSwipe.MainASPxClientPopupControl %>');
cntrl.Show();

During debugging, the runtime code is displayed as follows:

var cntrl = document.getElementById('');

The error message given is: "Unable to get property 'Show' of undefined or null reference" which obviously means (checked during debugging) that the variable "cntrl" is null and cannot get the instance of the popup control in the user control.

How can I successfully get an instance of this control???

Consider using Control.ClientID Property

.NET object cannot be got on JS side and will not work in JS DOM environment, you will need to use ClientID to get DOM object in JS and then use any kind of JS method to display, like http://www.randomsnippets.com/2008/02/12/how-to-hide-and-show-your-div/

Actually, what I needed to do was to get the "server control name.ClientInstanceName" and place a databind in the Load event of the control for the Page Header since I'm using a databinding expression "<%#".

Here is the code snippet. var swipe = eval('<%# MyManagerSwipe.ClientInstanceName %>');

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