ShowUsYour<Blog>

Irregular expressions regularly

January 2004 - Posts

MarkItUp.WebControls.ContextMenu : Making a start

A couple of days ago I discussed a string buffering technique that I'm using for a client-side context menu widget.  In one of the comments on that post I mentioned that I'd blog about my progress in turning it into a useable ASP.NET ContextMenu Server Control.  So here goes...

After some thought I decided that the best model would be to create it as a control that used the "Inner Default Property Persistance" model.  That is where a control class has default collection sub-property which requires persistance.  In my case these would be the ContextMenuItem instances.

So, the model will be something like: ContextMenu-->Items-->ContextMenuItem.  I've mocked up a brief demo and shown some sample syntax for creating the control on my website:

    http://www.flws.com.au/ContextMenu/SamplePopUp.html

That demo exists purely as a "mock-up" and, I'm currently in the process of building the server control and refining some of the javascript positioning and rendering routines.

Consume a .NET Assembly from a classic ASP page.

Whenever I think that I'm getting to the bottom of .NET, there's, well: there's always something to remind me...

    http://users.cis.net/sammy/remindme.htm  ( open in new window while you continue reading for the full effect )

I haven't really done a lot with strongly named assemblies or interop so, this morning I thought that I'd be bold and try a little experiment; I thought that I'd create a .dll in Visual Basic .NET and consume it from a classic ASP page - seems pretty trivial, so off I went:

1) Open New Project named “SimpleDLL” - assembly name “SimpleDLL“, namespace “SimpleDLL“
2) Create a class named “SimpleClass”

Public Class SimpleClass
    Public Function WriteName(ByVal name As String) As String
        Return name
    End Function
End Class        
        

 

3) Register the assembly in the Registry
4) Register it for COM

    regasm /tlb SimpleDLL.dll

Voila!  A quick check of the HKEY\LocalMachine\Software\Classes\ tells me that I had (at least) some level of success and that the Assembly is, indeed in the registry.  So, off I go to create my classic asp page and consume it.  So, again...

5) Open Visual Studio
6) Create SimplePage.asp
7) Type the following into a page named SimplePage.asp:

Dim foo 
Set foo = Server.CreateObject("SimpleDLL.SimpleClass")
Response.Write foo.WriteName("blah")

 

Sure enough, it didn't work.

The page cannot be displayed

Error Type:
(0x80070002)
/SimplePage.asp, line 11


After a bit of head scratching it was apparent that the source of my problems was that there was not enough information for the file to be found. Therefore I decided that I needed to create a strong name for my assembly and register it in the GAC

8) Generate a public/private key pair

    sn -k MarkItUp.key

9) Add the attribute to my assembly for registering it:

     <Assembly: AssemblyKeyFile("C:\MarkItUp.key")>

10) Re-build the assembly
11) Install it into the GAC

    gacutil /i SimpleDLL.dll

12) Re-install it into the registry
Fire-up the asp page.

Did it work? Yep :-) Amazing eh?  Now I just have to work out how to get that type library information into the registry so that I can also get intellisense working while coding the asp page.

Finally, because I was having such luck I decided to call the Assembly from a Sql Server Stored Procedure too. That also worked first time!

DECLARE @object int
DECLARE @hr int
DECLARE @return varchar(255)

EXEC @hr = sp_OACreate 'SimpleDLL.SimpleClass', @object OUT
EXEC @hr = sp_OAMethod @object, 'WriteName', @return OUT, 'This is the text'
PRINT @return     -- Displays "This is the text"
EXEC @hr = sp_OADestroy @object
        
Minimize string creation in dhtml widgets

I wrote a component tonight which allows me to easily bind client-side pop-up menu's to objects on a web page.  A single menu is basically a Html TABLE with a collection of child menu-items represented by DIV's.  Because there is a high liklehood of menu's being created over and over again I decided that I would take steps to minimize the amount of string building by “caching” the html for the menu and each menu-item the first time that it is generated.  I liked it so much that I thought I'd blog it :-)  Here is the GetHtml method for the menu which caches it's chunk of Html in the array variable named _stringIntern_ the first time that it is generated and pulls it from there on subsequent rendering operations.  The same logic is followed for each menu-item.


// interning table
var _stringIntern_ = new Array() ;
Menu.prototype.GetHtml = function() { if(this.IsInterned()) return _stringIntern_[this.Id] ;
var s = "<table class=\"dhtmlmMenuTable\" cellpadding=\"3\" cellspacing=\"1\" width=\"100%\" border=\"0\">\n" ; for( var i = 0; i < this.MenuItems.length; i++ ) { s += "\t<tr>\n\t\t<td>\n\t\t\t" ; s += this.MenuItems[i].GetHtml() ; s += "\n\t\t</td>\n\t</tr>\n" ; } s += "</table>" ; this.Intern(s) ; return s ; } // GetHtml Menu.prototype.Intern = function( s ) { _stringIntern_[this.Id] = s ; } Menu.prototype.IsInterned = function() { return( _stringIntern_[this.Id] != 'undefined' ) }
Productivity and Efficiency - being more productive
Good to see that Mark has blog'ged about some of the productivity based ideas that came up during a recent e-mail session that we shared.  Hopefully I'll have time to construct some of the ideas that I had about this topic into a well written post sometime soon :-)
Personalization (ASP.NET) - a new GotDotNet User Sample

UPDATE: Useful HttpCookie faq: http://www.cookiecentral.com/faq/ regarding limits on sizes of cookies etc.


I've uploaded a new GotDotNet User Sample titled: Personalization (ASP.NET). This is a wrapper around cookies to simplify the task of storing small amounts of personalization data. An example might be a webform that needs to remember all of the previous selections made on a per-user basis. This sample comes complete with a .chm file laced with stacks of sample usage code.

Personalization uses "Stores" to store data and each store can contain multiple keys. So, if I want to remember user settings for a page named "PageA.aspx" I could do:

Private Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Unload
    
    ' create the store for this page
    Dim p As New Personalization("PageA.aspx")
    
    ' read in the values
    If Not Me.FieldA.Text.Trim = String.Empty Then
        p.SetValue("FieldA", Me.FieldA.Text.Trim)
    Else
        p.Remove("FieldA")
    End If
    
    If Not Me.FieldB.Text.Trim = String.Empty Then
        p.SetValue("FieldB", Me.FieldB.Text.Trim)
    Else
        p.Remove("FieldB")
    End If
End Sub
...then, reading the values in the next time the user visits is trivial:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then
    
        ' re-create the store
        Dim p As New Personalization("PageA.aspx")
        ' read in the items
        txtFieldA.Text = p.Items("FieldA")
        txtFieldB.Text = p.Items("FieldB")
    
    End If
    
End Sub

Get Personalization Here

Posted: Jan 28 2004, 06:01 AM by digory | with 1 comment(s)
Filed under: ,
Display an ASP.NET DataGrid in Microsoft Excel

I was putting the finishing touches on a web application tonight when I discovered something really cool...

I've got a form where a user can select a whole slew of parameters to build a dynamic query and, once the results have been fetched I needed to render them as either:

  1. A plain, sortable, pageable DataGrid
  2. Rendered in a dynamically generated GUI
  3. Opened in a Microsoft Excel worksheet

I had already done the plain grid and the dynamic UI stuff - which, in itself was pretty cool - and tonight set about the task of doing the Excel part. The first place that I went to was the ASP.NET Support Center. This is an incredible resource and, if you haven't already done so, I'd suggest that you spend some time scouring through the "How To's" on that site. Anyways, I plugged the term: "Excel" into the "Search (KB) ASP.NET" form and submitted. I slid my eyes down the page about 10 items until they landed upon: "HOW TO: Export Data in a DataGrid on an ASP . NET WebForm to Microsoft Excel"... VOILA! Perfection. This was my resultant code to render the Excel Worksheet:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    BindGrid()
    RenderGrid()
End Sub

Private Sub BindGrid()
    Dim dtResults As DataTable = CType(Session("DynamicFormResults"), DataTable)
    If Not dtResults Is Nothing Then
        DataGrid1.DataSource = dtResults.DefaultView
        DataGrid1.DataBind()
    End If
End Sub

Private Sub RenderGrid()
    Response.ContentType = "application/vnd.ms-excel"
    ' Remove the charset from the Content-Type header.
    Response.Charset = ""
    ' Turn off the view state.
    Me.EnableViewState = False
    Dim tw As New System.IO.StringWriter
    Dim hw As New System.Web.UI.HtmlTextWriter(tw)
    ' Get the HTML for the control.
    DataGrid1.RenderControl(hw)
    ' Write the HTML back to the browser.
    Response.Write(tw.ToString())
    ' End the response.
    Response.End()
End Sub
Posted: Jan 27 2004, 08:58 PM by digory | with 42 comment(s)
Filed under:
MarkUp component

I've uploaded the initial release of the MarkUp component on the GotDotNet workspace.  The workspace can be found here:

    http://www.gotdotnet.com/Community/Workspaces/workspace.aspx?id=b7e8dd86-b35b-4255-a7d4-b108c3fd327e

I've also uploaded 2 projects:

1) MarkUpComponent : this will be the standalone component responsible for marking up text.

2) SimpleMarkUp : this is a WindowsForms application which uses the MarkUpComponent to mark-up text. This application is the same as the one on the original Msdn article except that every piece of code related to "marking-up" has been moved into the MarkUpComponent.

So, moving forward, the goal will be to improve the performance and abilities of the MarkUpComponent while also improving the features of the tools which consume it.  The SimpleMarkUp tool has many improvements over the tool which was released with my original Msdn article such as:

  • Remembers user preferences
  • Provides definition for the javascript language
  • Bug Fixes
Posted: Jan 26 2004, 09:50 AM by digory | with no comments
Filed under:
RssFeed - a server control to display rss feeds

The other night I gave a talk to my local VB User Group about blogging and some of the technologies that have sprung up to support it such as Rss, Opml, Aggregators and Blogging apps (such as .Text). I also mentioned how it is amazing that there are so many free (many with full source code) tools that you can grab to quickly implement a blogging solution. One of the tools that I forgot to mention was the rssFeed control which was the brainchild of Scott Mitchell and now lives in its own GotDotNet workspace.

I used this control tonight to add the blogs.regexadvice.com community feed to the home page of RegexLib.com. It was as simple as doing this:

myRssFeed.DataSource = @"http://blogs.regexadvice.com/MainFeed.aspx" ;
myRssFeed.DataBind() ;

...and configuring an ASP.NET templated control. There's some excellent examples here:
    http://aspnet.4guysfromrolla.com/articles/102903-1.aspx

This is great because it's a dead simple way to add current, topical content to a site with a minimal amount of effort :-)

Posted: Jan 25 2004, 08:40 PM by digory | with 1 comment(s)
Filed under:
RegexLib.com minor site alterations

Last night I added a couple of new features to the RegexLib.com site:

  1. Client-side javascript testing for patterns
  2. Contact Us feedback form

The regex testing tool can be accessed from anywhere within the site via the "Test" link on both the top and side menu's. There is also a link to the tester from the individual pattern display screens which navigate to the testing tool and pre-populate the pattern with that regex.  I'm hoping that, over the weekend I'll be able to add the “Advanced“ testing mode which will display the results in a Tree.

The "Contact Us" form will make it more visible and easier to make recommendations for features that you'd like to see in the RegexLib.com application.

Please let me know - via the new feedback form ;-) - if you discover any bugs with the javascript regex testing. 

Cross posted from: My Regex Blog.

Posted: Jan 23 2004, 08:03 AM by digory | with 1 comment(s)
Filed under:
2 Notables - Rss + Rss and Outlook (Both come with source)

First, Duncan mentioned about RssConnect; I've been looking at this app for a week or so now and I loved some of the UI "bits":

  • Nice splash screen
  • Nice Tools|Options layout (same as VS)
  • Nice interaction with a local data store (SQL or Access providers provided)

Overall, very useable UI (good for getting ideas) - but, probably not enough features (yet, although you can obviously add them because it comes with source) to make it my main viewer - mind you it has shown me a few things that I do consider important, such as a database datastore.

Then, in another blog Ed Kaim mentions a workspace project for building Outlook add-ins:

    http://www.gotdotnet.com/community/workspaces/workspace.aspx?ID=E7071B93-7970-4962-A4C2-D72AA2CFBCFF

...sounds interesting!

Posted: Jan 23 2004, 07:40 AM by digory | with no comments
Filed under:
More Posts Next page »