PDA

View Full Version : [CLOSED] UserControl DirectMethod call



cwolcott
Oct 02, 2015, 6:26 PM
I can duplicate your example of calling a DirectMethod defined by a user control from the usercontrol, but I launch user controls differently. The code below mimics your example when referencing the user control SampleUC01.ascx, but how can I get it to work for SampleUC02.ascx.



<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<%@ Register Src="SampleUC01.ascx" TagName="MyUserControl" TagPrefix="uc" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" DirectMethodNamespace="DM" />
<script runat="server">

[DirectMethod]
public void LaunchDialog(string dir, String controlNm)
{
UserControlRenderer.Render(new UserControlRendererConfig
{
UserControlPath = String.Format("{0}/{1}.ascx", dir, controlNm),
SingleControl = true
});
}
</script>

<script type="text/javascript">
Dashboard = {
launchDialog: function (dir, controlNm, controlId) {
var dialog = Ext.getCmp(controlId);
if (dialog) {
dialog.show();
}
else {
DM.LaunchDialog(dir, controlNm);
}
}
}
</script>

<uc:MyUserControl ID="UserControl1" runat="server" />

<ext:Button runat="server" Text="Open">
<Listeners>
<Click Handler="Dashboard.launchDialog('.', 'SampleUC02', 'Sample02Window');" />
</Listeners>
</ext:Button>

</form>
</body>
</html>


SampleUC01.ascx


<%@ Control Language="C#" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">

[DirectMethod]
public void Hello01()
{
X.Msg.Alert("...", "Hello from user control Sample 01 Window").Show();
}

</script>

<ext:Window ID="Sample01Window" runat="server" ClientIDMode="Static" Title="Sample 01 Window" Width="200" Layout="FitLayout" CloseAction="Hide">
<Items>
<ext:Button ID="SampleButton" runat="server" Text="Call DirectMethod in ascx">
<Listeners>
<Click Handler="#{DirectMethods}.Hello01();" />
</Listeners>
</ext:Button>
</Items>
</ext:Window>


SampleUC02.ascx


<%@ Control Language="C#" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">

[DirectMethod]
public void Hello02()
{
X.Msg.Alert("...", "Hello from user control Sample 02 Window").Show();
}

</script>
<ext:Window ID="Sample02Window" runat="server" ClientIDMode="Static" Title="Sample 02 Window" Width="200" Layout="FitLayout" CloseAction="Hide">
<Items>
<ext:Button ID="SampleButton" runat="server" Text="Call DirectMethod in ascx">
<Listeners>
<Click Handler="#{DirectMethods}.Hello02();" />
</Listeners>
</ext:Button>
</Items>
</ext:Window>

Daniil
Oct 03, 2015, 1:22 PM
Hi Chris,

There are two problems.

1. UserControlRenderer doesn't support rendering of DirectMethods. So, a response of LaunchDialog call doesn't contain client side proxies for the DirectMethod inside the user control.

2. Even if it would be able to render DirectMethods' client side proxies, it will not work because a user control would not be recreated during a server request and Ext.NET won't be able to find a server side method to execute.

There is a few quite comprehensive discussions on that.
http://forums.ext.net/showthread.php?25279
http://forums.ext.net/showthread.php?26744

In brief, rendering user controls on the fly and using its code behind DirectMethods is not going to work, unfortunately. In my opinion in such a scenario a user control is considered as a view (in the MVC terminology), i.e. it is not supposed to have server side handlers. In this case server side handler could be moved to web services (.asmx) or HTTP handlers (.ashx).

Hope this helps.