How to have scripts inside UserControl?
I'm trying to create a UserControl but can't quite figure out how to get scripts to work inside. The code I'm using is below. The nodeLoad is not working, even though this same code works if in a regular aspx page. Am I missing something here? I really need the scripting to be contained within the control, not outside of it.
Code:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="FolderView.ascx.vb" Inherits="FolderView" %>
<%@ Register Assembly="Coolite.Ext.Web" Namespace="Coolite.Ext.Web" TagPrefix="ext" %>
<style type="text/css">
.x-tree-node .x-tree-node-inline-icon{background:transparent;height:16px;width:16px;}
</style>
<script type="text/javascript">
function nodeLoad(node) {
Coolite.AjaxMethods.LoadFolder(node.id, {
success: function(result) {
var data = eval("(" + result + ")");
node.loadNodes(data);
},
failure: function(errorMsg) {
Ext.Msg.alert('Failure', errorMsg);
}
});
}
function nodeClear(node) {
while (node.hasChildNodes()) {
node.removeChild(node.item(0));
}
}
</script>
<ext:FitLayout ID="FitLayout1" runat="server">
<ext:TreePanel ID="TreePanel1" runat="server" Border="false" Animate="false">
<Listeners>
<BeforeLoad Fn="nodeLoad" />
</Listeners>
</ext:TreePanel>
</ext:FitLayout>
RE: How to have scripts inside UserControl?
Hi,
If you define AjaxMethod in INamingContainer (MasterPage, UserControl) then you need use ClientID of in ajax method javascript name
Code:
Coolite.AjaxMethods.ClientIDOfUserControl.MyMethod();
In 0.8 version you can define own rules which name must be with AjaxMethodProxyIDAttribute and AjaxMethodNamespace
Also in 0.8 you can use #{AjaxMethods} which will use current context automatically (detect what currect context and add required prefixed (like ClientID) automatically)
Code:
#{AjaxMethods}.MyMethod();
RE: How to have scripts inside UserControl?
This seems to work now. So until v0.8, how do I possibly do this if the user control will be used in various places throughout the application with different ids? Do I have to hard code every possible name of the user control?
RE: How to have scripts inside UserControl?
Your code sample seems like it should work.
Where are you defining the AjaxMethod "Coolite.AjaxMethods.LoadFolder"?
If the "LoadFolder" Mehtod is defined in the UserControl code-behind, does your sample work if you add the .ClientID of the UserControl before .LoadFolder?
Example
Code:
Coolite.AjaxMethods.<%= this.ClientID %>.LoadFolder
Hope this helps.