Saturday, 8 May 2010

AJAX Control Toolkit HTMLEditor - buttons with jQuery

Have you tried adding buttons to the toolbar of the HTML Editor in the Ajax Control Toolkit? If so, you will know it is a "mission". I gave up and built my own with jQuery. To use, just add your own buttons to the "images" div...


Hopefully modifying the script for your own purposes is relatively painless; I think the code is mostly self-documenting??


<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="HTMLEditorButtons.aspx.vb" Inherits="Foo.HTMLEditorButtons" %>


 


<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc2" %>


 


<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit.HTMLEditor"


    TagPrefix="cc1" %>


 


<!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 id="Head1" runat="server">


    <title></title>


</head>


<body>


 


    <script src="../Script/jquery-1.3.2.js" type="text/javascript"></script>


    <form id="form1" runat="server">


        <cc2:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">


        </cc2:ToolkitScriptManager>


    <div>


        <cc1:Editor ID="Editor1" runat="server" Height="400px"


            onclientactivemodechanged="modeChanged" />


 


    </div>


 


    <div id=images style="display: none; position: absolute; border: solid 1px black; background-color: White; overflow: scroll; cursor: hand;height: 150px; width:80px ">


        <img src="../Images/Stuffed_Folder.png" /><br />


        <img src="../Images/User_Complete.gif" /><br />


        <img src="../Images/Move.gif" /><br />


        <img src="../Images/help.gif" />


    </div>


 


    </form>


 


 


    <script language=javascript>


        $(document).ready(function() {


 


            $(".ajax__htmleditor_editor_toptoolbar > div").append('<img class="ajax__htmleditor_toolbar_button customButton" title="Images" id="btnImages" alt="" src="../images/picture.png" onclick="showImages()" />');


 


 


 


            $("#images").mouseleave(hideImageList);


            $("iframe").mouseover(hideImageList);


 


            $("#images img").click(function(e) {


                var editor = $find('Editor1')


                editor._editPanel._modePanels[0].insertHTML("<img src='" + $(this).attr("src") + "' />");


                hideImageList();


            }).css("cursor", "pointer");


 


            $(".customButton").mouseenter(function() {


                $(this).addClass("ajax__htmleditor_toolbar_button_hover");


            });


 


            $(".customButton").mouseleave(function() {


                $(this).removeClass("ajax__htmleditor_toolbar_button_hover");


            });


 


        });


 


        function showImages() {


            var imgPos = $("#btnImages").offset();


            $("#images").css("top", imgPos.top + 23).css("left", imgPos.left);


            $("#images").slideToggle(100);


 


        }


 


        function hideImageList() {


            $("#images").fadeOut(100);


 


        }


 


        function modeChanged(e) {


            if (e._activeMode == 0) {


                // show custom buttons


                $(".customButton").show();


            } else {


                $(".customButton").hide();     


            }


 


        }


    </script>


</body>


</html>


 


 


Tuesday, 2 February 2010

Online Javascript Hex viewer - Local

Check it out, similar functionality to XVI32, but written entirely in Javascript. Uses new HTML5 methods to access local files, so no transfer of data to the server necessary. No easy means of writing file back to disk via Javascript yet, so view only for now, no edit :(

Unfortunately only works on Firefox 3 at the moment, but expect other browsers will adopt these standards reasonably quickly.

Can be a bit slow on old computers...




Monday, 2 November 2009

Linq GetEnumerator.reset and new MS Chart charting controls

So I thought I would try out the new Microsoft charting controls for ASP.Net and winforms...


Looks fantastic but when I came to do some runtime databinding to a simple linq collection (selecting an x and y value into an enumerable anonymous type...) it failed miserably with an obscure error message.

With a bit of snooping I found that the problem is that the chart control always calls .Reset() on its IEnumerable(Of T) datasource before iterating the collection. Unfortunately the System.Linq.Enumerable.Iterator(Of TSource) that Linq returns as a concrete IEnumerable throws a "NotImplementedException" in its implementation of IEnumerable.Reset() - good one MS! (Check it out in Reflector)

Simple answer - call .ToArray() on your Linq collection and hey presto your Linq query will work as a chart datasource.

UPDATE - Click here and vote for MS to fix this bug!!!

Saturday, 22 August 2009

Retro Games Released

As a programming "hobby" I have been writing retro 8-bit games - you can play the results here:



Friday, 13 March 2009

An Improved MsgBox for Asp.Net

Often times a simple messagebox is required at the client under ASP.Net. This is easy to do by sending back a JavaScript Alert(). However, this does not work so well if the user navigates around using the back and forward buttons, as they will see the message multiple times. Also, we would like our msgbox to be usable from AJAX enabled apps, so that an asynchronous postback can also trigger an alert.


The below vb.net 3.5 code meets these criteria:




Module PageExtensions
 
    <extension()> _
    Public Sub MsgBox(ByVal aPage As Page, ByVal aMessage As String)

        Dim timeStr As String = "msgbox" & Date.Now().Ticks.ToString

        ' Set a cookie after showing alert so as will not re-show if back button or refresh is used.

        Dim alertScript As String = "if (document.cookie.indexOf('" & timeStr & "') == -1) {"
        alertScript &= "alert(""" & aMessage & """);"
        alertScript &= "document.cookie = '" & timeStr & "=deleted';}"

        System.Web.UI.ScriptManager.RegisterStartupScript(aPage, GetType(String), aMessage, alertScript, True)

    End Sub

End Module


To use within a page, you can now simply type:
MsgBox("Hello World"), just like in a Windows App.

A temporary cookie is set for each msgbox invoked so that the msgbox will only be shown once. The method is an extension method of the Page class, so needs to be contained within a Module, or used without the attribute.

Note also you will need a reference to System.Web.Extensions in your project.

Wednesday, 1 October 2008

EazyCaptcha.com Simple Captcha Server

As a programming exercise I created a public captcha server that can be used to generate captchas without requiring any software libraries or complex programming by the developer.

You can try it out here...

www.eazycaptcha.com

Sunday, 28 September 2008

VegePlan

VegePlan is a pet .Net project I have been working on to get up to speed on AJAX and LINQ.

It is a basic drag-n-drop vegetable garden planner and designer.

I have openend it up for anyone to use here:

www.vegeplan.com

I have used the JavaScript Prototype library for the AJAX, and YUI and the Ajax Control Toolkit for the drag-n-drop and other UI functionality.