PDA

View Full Version : [CLOSED] DirectMethod's problem



softmachine2011
Jun 06, 2012, 3:28 PM
Hi,

In the next sample:


<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<ext:ResourceManager runat="server" />
<script runat="server">
[DirectMethod]
public void SetTimeStamp()
{
this.Label1.Text = string.Concat("Server Time: ", DateTime.Now.ToLongTimeString());
}
</script>

<ext:Button ID="Button5" runat="server" Text="Click Me" Icon="Lightning">
<Listeners>
<Click Handler="App.direct.SetTimeStamp();" />
</Listeners>
</ext:Button>
<ext:Label ID="Label1" runat="server" Format="Server Time: {0}" Text='<%# DateTime.Now.ToLongTimeString() %>' AutoDataBind="true" />
</body>
</html>


When click into button, not call to SetTimeStamp method and call to REST View Controller method.

Any idea?

Thanks,

Daniil
Jun 06, 2012, 3:56 PM
Hi,

DirectMethods/DirectEvents with code behind server handlers should not be used in MVC.

They should call controller actions.

Example View

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ext.Net.MVC v2 Example</title>
</head>
<body>
<ext:ResourceManager runat="server" />

<ext:Label
ID="Label1"
runat="server"
Format="Server Time: {0}"
Text='<%# DateTime.Now.ToLongTimeString() %>'
AutoDataBind="true" />

<ext:Button runat="server" Text="Update">
<Listeners>
<Click Handler="Ext.net.DirectMethod.request({
url : '/Test/Update',
params : {
labelId : 'Label1'
}
});" />
</Listeners>
</ext:Button>
</body>
</html>


Example Controller Action

public ActionResult Update(string labelId)
{
AjaxResult r = new AjaxResult();
Label label = X.GetCmp<Label>(labelId);
label.Text = DateTime.Now.ToLongTimeString();

return r;
}