Hi
I'm trying to load a UserControl with Page.Load(), the user control has a RadComboBox and I need the javascript code to handle the ComboBox events, but the javascript isn't loaded.
I tried with the SrciptManager.RegisterClientScriptBlock() and the RadAjaxManager.GetCurrent(this.Page).ResponseScripts.
the second let me put code like alert('hello') and it work, but if I put some like RadAjaxManager.GetCurrent(this.Page).ResponseScripts.Add("function hello(){alert('hello');}") and try to call it don't.
the user control ChartWidget.ascx
... | |
<telerik:RadAjaxPanel ID="AjaxPanel1" runat="server" Height="200px" Width="300px"> | |
<telerik:RadComboBox ID="ChartSelector" runat="server" Height="150px" Width="200" OnClientDropDownOpened="OnClientDropDownOpenedHandler"> | |
<ItemTemplate> | |
<div id="div1"> | |
<telerik:RadTreeView ID="Charts" runat="server" OnClientNodeClicking="nodeClicking"> | |
</telerik:RadTreeView> | |
</div> | |
</ItemTemplate> | |
<Items> | |
<telerik:RadComboBoxItem Text="" /> | |
</Items> | |
</telerik:RadComboBox> | |
</telerik:RadAjaxPanel> | |
... |
the code behind
protected void Page_Load(object sender, EventArgs e) | |
{ | |
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "RadDropDownFunc", | |
"function comboLoad(sender, eventArgs){sender.set_text(sender.get_items().getItem(0).get_value());}"+ | |
"function StopPropagation(e){if (!e) {e = window.event;}e.cancelBubble = true;}"+ | |
string.Format("function OnClientDropDownOpenedHandler(sender, eventArgs){var tree = sender.get_items().getItem(0).findControl('{0}'); var selectedNode = tree.get_selectedNode(); if (selectedNode){selectedNode.scrollIntoView();}}",this.ChartSelector.ClientID), | |
true); | |
LoadData(); | |
} | |
public void LoadData() | |
{ | |
ChartSelectorData = new List<ChartListItem> | |
{ | |
new ChartListItem { Text = "Nodo 1", NodeID=1 , ParentID=0}, | |
new ChartListItem { Text = "Nodo 2", NodeID=2 , ParentID=0}, | |
new ChartListItem { Text = "Nodo 3", NodeID=3 , ParentID=0}, | |
new ChartListItem { Text = "Nodo 1-1", NodeID=4 , ParentID=1}, | |
new ChartListItem { Text = "Nodo 1-2", NodeID=5 , ParentID=1}, | |
new ChartListItem { Text = "Nodo 2-1", NodeID=6 , ParentID=2}, | |
new ChartListItem { Text = "Nodo 2-2", NodeID=7 , ParentID=2}, | |
new ChartListItem { Text = "Nodo 3-1", NodeID=8 , ParentID=3} | |
}; | |
ChartSelectorDataBind(); | |
ChartSelectorInitialize(); | |
} | |
private void ChartSelectorDataBind() | |
{ | |
RadTreeView tree = GetChartSelectorTreeView(); | |
tree.DataTextField = "Text"; | |
tree.DataNavigateUrlField = ""; | |
tree.DataFieldID = "NodeID"; | |
tree.DataFieldParentID = "ParentID"; | |
tree.DataSource = ChartSelectorData; | |
tree.DataBind(); | |
} | |
private void ChartSelectorInitialize() | |
{ | |
RadTreeView tree = GetChartSelectorTreeView(); | |
foreach (RadTreeNode Node in tree.GetAllNodes()) | |
{ | |
if (Node.Nodes.Count == 0) | |
{ | |
Node.ImageUrl = "~/Includes/Skin/Default/chart.gif"; | |
} | |
} | |
} | |
private RadTreeView GetChartSelectorTreeView() | |
{ | |
return (RadTreeView)ChartSelector.Items[0].FindControl("Charts"); | |
} |
the code behind where i want to load
protected void Button1_Click(object sender, EventArgs e) | |
{ | |
chartWidget = LoadControl("~/widgets/ChartWidget.ascx", new object[] { }) as ChartWidget; | |
chartWidget.LoadData(); | |
Panel1.Controls.Clear(); | |
Panel1.Controls.Add(chartWidget); | |
} | |
private UserControl LoadControl(string UserControlPath, params object[] constructorParameters) | |
{ | |
List<Type> constParamTypes = new List<Type>(); | |
foreach (object constParam in constructorParameters) | |
{ | |
if (constParam != null) | |
constParamTypes.Add(constParam.GetType()); | |
} | |
UserControl ctl = Page.LoadControl(UserControlPath) as UserControl; | |
ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray()); | |
constructor.Invoke(ctl, constructorParameters); | |
return ctl; | |
} |
the page where the usercontrol loads
... | |
<asp:ScriptManager ID="ScriptManager1" runat="server"> | |
</asp:ScriptManager> | |
<telerik:RadAjaxManager ID="AjaxManager1" runat="server"> | |
<AjaxSettings> | |
<telerik:AjaxSetting AjaxControlID="Button1"> | |
<UpdatedControls> | |
<telerik:AjaxUpdatedControl ControlID="Panel1" /> | |
</UpdatedControls> | |
</telerik:AjaxSetting> | |
</AjaxSettings> | |
</telerik:RadAjaxManager> | |
... | |
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> | |
.. | |
<asp:Panel ID="Panel1" runat="server"> | |
</asp:Panel> |