Dynamic Javascript Tag Insert – ASP.NET 2.0

A lot of JavaScript utilities need to be inserted at the bottom of the page so that the HTML elements that use them (or at least the function calls) do not generate errors. When using ASP.NET 2.0 master pages, you might want to insert the <SCRIPT> tags dynamically so that a particular JavaScript resource is available on every page.

I spent a lot of time trying to find a solution to this, and never really found a perfect one. The method I describe here is the best solution I could find at the moment.

Firts add a PlaceHolder Control, right before the end form tag. We’ll use this to write the <SCRIPT> tag so that it appears almost at the end of the document:

<asp:PlaceHolder ID=”plhJavaScript” runat=”server”></asp:PlaceHolder>

Then you can modify the master page’s Page_Load event to create a generic control, which will end up as the <SCRIPT> tag that is our goal. Once you have it, all you have to do is add it to the placeholder:

‘create generic html control

Dim JS As New HtmlControls.HtmlGenericControl(“script”)

JS.Attributes.Add(“type”, “text/javascript”)

JS.Attributes.Add(“src”, HttpContext.Current.Request.ApplicationPath & “/js/MyJavascriptFile.js”)

plhJavaScript.Controls.Add(JS)

That’s pretty much it. I think the code above might need tweaking to get the path always perfect if you’re down in a sub-directory, but you get the point.

ADAL 1.0.6

Hi all,

Options:  Download now, or read and then download at the bottom.

 ADAL 1.0.6 has been released.  There is a new feature in this release for anyone doing their own extensions, PopulateFromReader!  Basically this method that is available on every Item that ADAL creates will accept a SQLDataReader and auto populate the properties of the object for you.  Yes, that’s right, no more:

 IIF(MyReader(“ColumnName”) Is DBNull.Value, Nothing, MyReader(“ColumnName”))

Now isn’t that nifty?  This allows you to create an extension in a Partial class without too much worry.  Just make sure that the reader has all the fields the object will need or you’ll see a yellow screen of death.

 The code that ADAL creates for a Select Now looks something like:

Public Function [Select](ByVal ColumnName As String) As Classes.TableItem
Dim retObject As New Classes.TableItemTry

Dim MyReader As SqlDataReader
MyCommand.CommandText = “ADAL_usp_TableItem_Select”

With MyCommand.Parameters
.Clear()
.Add(“@ColumnName”, SqlDbType.VARCHAR, 2).value = ColumnName
End With

SqlConnection1.Open()
MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection)retObject.PopulateFromReader(MyReader)
MyReader.Close()Catch ex As Exception
Throw
Finally

If SqlConnection1.State = ConnectionState.Open Then SqlConnection1.Close()
End Try

Return retObject
End Function

The new PopulateFromReader is in bold. 

There’s also a bug fix in this version.  When ADAL was creating code to check for the length of strings during Property Sets if failed to check if the value was not nothing, and ended up causing an exception if it was (it was just using If MyString.Length > 0 Then).

Maybe you want to head on over to SourceForge and give it a Download?

ADAL 1.0.5

An official release of ADAL, version 1.0.5 is available form the ADAL SourceForge.net download page (http://sourceforge.net/projects/adal). There’s a couple new features in this version, most notably:

  • Uses an .msi installer
  • Has “File Save” and “File Open” commands that write the current configuration to a custom XML file so you can reload the settings you had last.

There’s also a quick bug fix. When a table is all primary keys, ADAL used to generate a “GRANT EXECUTE ON” for the UPDATE stored procedure, even though it didn’t generate the CREATE statement for the update, so it generated a SQL error when you ran the SQL script. This has been fixed.

Plans for the future are:

  • Templating – Develop a templating system so that users can create their own VB code templates that ADAL should generate around. this will be quite hard to get right, so I wouldn’t expect it too soon.
  • Use embedded (but still paramaterized) SQL. This would remove the need to create the stored procedures, which can be nice to keep the use of ADAL fairly simple.
  • Take a look at Paul Kimmel’s Ideas about a Data Acces Layer and see how much of that can be used in ADAL (http://www.developer.com/net/vb/article.php/3650241)

ADAL – Gone Open Source

I decided to make ADAL Open Source.   “Why not?” I figured.  I never planned on charging for it, and now that it’s open source I get to host it at SourceForge and get the use of their Subversion Source Control servers.  So anyway, I haven’t done an official release over at SourceForge yet, but at least you can get the code from Subversion and compile it yourself if you want the most recent version.  The SourceForge URL is: http://sourceforge.net/projects/adal

Don’t be afraid to use the SourceSorge forum and feature request system either…

 Have fun!

Removed Flickr Badge

I used to have the Flickr Badge on the right hand sidebar, but it seemed to be slowing down the blog (I’d frequently see my browser “Waiting for www.flickr.com…”). It’s gone now, we’ll see if performance improves.

ADAL v.03

  • Removed VB syntax highlighting until we develop our own.
  • Identifies tables with auto-increment fields and alters the insert to not attempt to insert into that field (would generate a SQL error). The DAL class also sets the ID property of the item passed in to the new auto-incremented value.
  • UPDATE statements now update every field (even the primary keys). This is weird but was needed in order to satisfy cases where every column in the table is part of the primary key (ADAL used to generate invalid code in this case). I suppose this makes the execution of the update statement a little slower, but in the interest of reliability I’m willing to make the sacrifice.
  • Interprets DECIMAL datatype correctly

Now With Options!

  • Options menu item opens options form!
  • Change root namespace (defaults to ADAL)
  • Put the item classes and DAL classes in the same or separate namespaces (Defaults to “Classes” and “DAL”)
  • Customizable stored procedure prefix. This allows you to easily identify which stored procedures belong to the ADAL objects (in case you need to delete either the ADAL or your own it’ll help prevent what I did when I deleted a few custom stored procedures because they all looked the same).
  • Option to generate “GRANT EXECUTE” statements on the stored procedures created. No more “Execute permission denied” errors!
  • Option to include validation code on varchar property gets and sets for length. ADAL throws an exception if you enable this and you try to set an items property with a value that’s too long.

Still With Problems

  • There are still data types (like tinyint that ADAL doesn’t know what to do with). I’ll get to those eventually….

Download: ADAL Version 0.3 

No More Spam & a New CMS

Spamalot
I just downloaded and installed Akismet so hopefully I won’t have to deal with spam any more. We’ll see.

CMS Search
I also checked out some CMS packages yesterday and think the one that looks the best so far is CMS Made Simple It has a nice simple interface that allows me to add new categories, and pages under the categories. It offers links between pages, but does not keep track of what pages are linked to what. So it allows me to delete a page that is being linked to from another, that’s the only part I don’t like so far…

For a free demo of CMS Made Simple, check out http://opensourcecms.com/index.php?option=content&task=view&id=452

I’ll keep looking on http://opensourcecms.com/ for a CMS, but so far this one’s out front.

Oh Yeah, there’s more

I forgot, there’s more stuff I need to do:

  1. Get a CVS source control solution that works with Visual Studio I believe this will consist of either a local cvs server, or sourceforge projects (and their cvs server). Hopefully I’ll get something like http://www.jalindi.com/igloo/ to work so I can connect Visual Studio to cvs and feel like I’m working with VSS.
  2. Finish varnishing my boat’s wood pieces. Also maybe fill the inner cabin with epoxy / filler to make it look nice inside, then I can paint it too.
  3. Change the oil in my truck.
  4. Delete this crappy linux OS.

Bowling Rocks!

I went bowling last night for the first time in…15 years?…my first game really sucked, I even had a couple gutter balls, but my second game was really good.  I had a few strikes, and one set of three strikes in a row.  Don’t tell anyone, but I’m pretty sure it was all luck.

I was starting to feel bad for the guys I was playing against until we played Halo later on in the night (around 1:00am).  They took their bowling frustrations out on me by completly kicking my ass.  Even with major handicaps on their part I was still only getting 8 kills out of 20 or whatever.

Programming

I’ve still got my homework list of programming chores for home.  With the nice weather it’s hard to motivate myself to do any programming at home, but the current list is:

  • Fix some of the problems with ADAL
  • Start work on MASS (Multi Author Story System)
  • Start work on Mage (.NET port of coppermine…sort of)
  • Start work on the AGI Application Gateway Interface
  • Maye install an open source CMS on the killfly root (http://www.killfly.com).
  • Delete this shitty Linux OS from my laptop and put XP back on.