Hi,
The following code worked in my test site. To spare the bandwidth, you can further extend it to send only Json strings that describe the new nodes (e.g. [label:"...", coordinates:"...", color:"..."]) and create nodes from JavaScript based on the Json content, instead of reloading the diagram.
Note that if the user draws anything, it will be lost after clicking the "new node" button, because the applet changes are not sent to the callback method. If your application lets users draw interactively, you could send the applet's saveToString() result as argument to the callback instead of using the session data.
I hope that helps,
Stoyan
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="MindFusion.Diagramming.WebForms" Namespace="MindFusion.Diagramming.WebForms"
TagPrefix="ndiag" %>
<!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>
<form id="form1" runat="server">
<div>
<ndiag:DiagramView runat="server"
ID="diagramView"
Width="800px" Height="600px">
</ndiag:DiagramView>
</div>
</form>
<br />
<button id="btnNewNode"
onclick="newNode('', '')">
New node
</button>
</body>
<script type="text/javascript">
function diagramReceived(result, context)
{
var applet = <%= diagramView.AppletElement %>;
applet.loadFromString(result);
}
</script>
</html>
using System;
using System.Web.UI;
using MindFusion.Diagramming;
public partial class _Default : Page, ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// upon initial GET, save the empty diagram as session variable
var diagram = Session["Diagram"];
if (diagram == null)
Session["Diagram"] = diagramView.Diagram.SaveToString(SaveToStringFormat.CompressedXml, false);
}
ClientScriptManager cm = Page.ClientScript;
String callback = cm.GetCallbackEventReference(
this, "arg", "diagramReceived", "");
String newNode = "function newNode(arg, context) {" +
callback + "; }";
cm.RegisterClientScriptBlock(GetType(), "newNode", newNode, true);
}
public string GetCallbackResult()
{
var diagram = new Diagram();
diagram.LoadFromString(Session["Diagram"].ToString());
diagram.Factory.CreateShapeNode(r.Next(100), r.Next(50), 30, 15)
.Text = "node " + diagram.Nodes.Count;
var str = diagram.SaveToString(SaveToStringFormat.CompressedXml, false);
Session["Diagram"] = str;
return str;
}
public void RaiseCallbackEvent(string eventArgument)
{
}
Random r = new Random();
}