Question

I would like to display the differences between versions of the same content. Initially I rolled out my own WebControl, however the differencing algorithm I came up with was slightly naive.

Does anyone know of any .Net code or a WebControl out there on the Internets that might be of use? The implementation that stackoverflow uses, is just the thing I'm looking for e.g:

alt text

Update: 12/12/2008 I've wrapped the jsdiff implementation into a self contained web control assembly and uploaded it to the MSDN Code Gallery Text differencing and syntax highlighting ASP.Net WebControls.

Update: 11/12/2008 Came across the following javascript differencing library. Will experiment wrapping it in a custom WebControl. The output looks similar to that used by stackoverflow:

alt text

Was it helpful?

Solution

I use the js lib you mention.

Include the .js file inside a script tag, add a named asp:Literal to the page

<asp:Literal ID="litCompare" runat="server">
</asp:Literal>

and add to the code-behind:

litComparison.Text = "<pre id=\"lbDiffPre\" class=\"code\"> </pre>";
ClientScript.RegisterStartupScript(GetType(), "calccompare",
    @"document.getElementById('lbDiffPre').innerHTML = 
    diffString(document.getElementById('" + edit1.ClientID + "').value, 
        document.getElementById('" + edit2.ClientID + "').value).
        replace(/\r\n/g, '<br>';", true);

In my case I had 2 controls which display the original values as well, you might want to include the strings literally in the diffString() function. The additional replace() converts \r\n line breaks into HTML line breaks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top