Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello could anyone provide any example, which show functionality with multiple user controls and javascript? Because I try to find solution with google and in other forum but without success.

So my user control looks like this:
C#
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Bar.ascx.cs" Inherits="Portal.Web.Widgets.Bar" %>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="Widgets/Widget.js" type="text/javascript"></script>
        

<dx:ASPxDockPanel ID="BarPanel" ClientIDMode="Static"  runat="server" DragElement="Window" ShowHeader="false"
    ShowShadow="false" Border-BorderWidth="0" ClientInstanceName="dpBar" AllowedDockState="DockedOnly">
        <ContentCollection>
            <dx:PopupControlContentControl id="pcBar"  runat="server" SupportsDisabledAttribute="True">
                <div id="dockPanelContent" style="width: 100%; height: 100%">
                        <dx:WebChartControl ID="chartControl" ClientIDMode="Static"  runat="server" ClientInstanceName="chartControl"
                                                             önCustomCallback="chartControl_CustomCallback" ClientSideEvents-EndCallback="chartControl_EndCallback">
                        </dx:WebChartControl>
                        <dx:ASPxButton ID="checkButton"  runat="server" ClientIDMode="static" ClientInstanceName="checkButton">
                        
                        </dx:ASPxButton>
                 </div>
            </dx:PopupControlContentControl>
        </ContentCollection>
    <ClientSideEvents AfterResizing="dockPanel_AfterResizing"/>
</dx:ASPxDockPanel>


And Widget.js javascript code in external file like this:
JavaScript
var waiting = false;
var callback = false;
var newWidth;
var newHeight = 0;
function dockPanel_AfterResizing(s,e) {
 
    if (s.IsDocked()) {
        newWidth = dockPanelContent.clientWidth;
        newHeight = dockPanelContent.clientHeight;
    }
    else {
        newWidth = dockPanelContent.clientWidth - 10;
        var height = dockPanelContent.parentNode.parentNode.clientHeight;
        var divContent = dockPanelContent.parentNode;
        newHeight = height - parseInt($(divContent).css("padding-top")) - parseInt($(divContent).css("padding-bottom")) - 10;
    }
    chartControl.SetWidth(newWidth);
    chartControl.SetHeight(newHeight);

    if (!waiting) {
        waiting = true;
        dockPanel = s;
        setTimeout("TimeoutedCallback()", 1000);
    }
}

function TimeoutedCallback() {
    chartControl.PerformCallback(newWidth + "|" + newHeight);
}

function chartControl_EndCallback(s, e) {
    waiting = false;
}


On my page i put multiple user control instances
web.config
C#
<controls>
                               <add src="~/Widgets/Bar.ascx" tagName="Bar" tagPrefix="widget" />
                               <add src="~/Widgets/Gantt.ascx" tagName="Gantt" tagPrefix="widget" />

default.aspx
C#
<div id="UC1">
            <widget:Bar ID="BarWidget"  runat="server" OwnerZoneID="ASPxDockZone1" ClientInstanceName="bar" ClientIDMode="static" />
<div id="UC1"></div>
            <widget:Gantt ID="GanttWidget"  runat="server" OwnerZoneID="ASPxDockZone2" ClientInstanceName="gantt" ClientIDMode="static" />
       </div>


and try to run functions but no vain. Each user control's overridden by last usercontrol.
Please could you help me. Thanks in advance.
Posted
Updated 4-Apr-13 0:03am
v2
Comments
sri senthil kumar 4-Apr-13 6:28am    
one suggestion would be if your doing anything with html control/tags then load those scripts in the ascx itself and put tht control as runat="server" and use control.ClientId. In your case it will be, assuming "dockPanelContent" div should be added with runat server attribute and load that script in the ascx.

1 solution

Hi,

It's because of ClientInstanceName in your case is static it is always the same for all your controls. In ClientInstanceName you defining the name of variable where client side JS API object will be stored. And because these names are equals for all your user contriols (and browser knows nothing about user controls, only final HTML and JS) you getting the last one after page initialization (client side) phase.
+ similar behaviour with ClientIDMode="Static"

So how to fix:
1. in this part - newWidth = dockPanelContent.clientWidth;
you referring to node directly by ID, obviously if you have several node on page with the same ID it can't work.
I would recommend to search within sender main control to identify element
for example:
var dockPanelContent = $('#dockPanelContent', e.GetMainElement()).get(0);

2. Create some prefix that should be different for every control on page. Use this prefix to dynamically modify client instances and static IDs within your user control.
Rewrite your script, in you case it can't be static (actually it can but it's a long story), it should be different (because you'll have different IDs and client instance names) for every instance of user control on page.

Description of similar problem: http://www.devexpress.com/Support/Center/p/B141088.aspx[^]
 
Share this answer
 
v2

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