Viewing entries in
Development

Comment

jQuery 1.3 released!

jQuery 1.3 was released and the most useful new feature is the live event!

Previously in older versions when you dynamically added new DOM elements into you page your event handlers would not automatically be attached to those elements.

You run into this issue a lot if you use AJAX or are creating lots of elements on the fly. The only trick before this new feature was to rebind all those events whenever a new element was added.

With the live event, it automatically binds up all current and future elements matching the selector

So instead of using:

// Will only add the event handler to current matched elements in the DOM
$("div").click(function() { doSomething(); });

You can use

// Will add the event handler to all current and future matched elements in the DOM
$("div").live("click", function() { doSomething(); });

Check it out, it’s a great new feature.

Comment

3 Comments

Dynamic Data - Working with ASP.NET Membership provider and hiding the aspnet tables using T4 Templates

There are two ways to work when using ASP.NET Dynamic Data:

  1. Automatically Scaffold everything
  2. Scaffold only those tables you specify

Scaffolding manually is alright but you need to remember to add the ScaffoldTableAttribute to the new tables you want managed by ASP.NET Dynamic Data

I prefer to let it automatically scaffold all tables so as I’m developing and changing the database schema I automatically get the CRUD pages. I then choose which ones to exclude.

Note: You can scaffold all tables by setting the ScaffoldAllTables property to true in your Global.asax.cs

model.RegisterContext(typeof(TestDataContext), new ContextConfiguration() { ScaffoldAllTables = true });

This is great, until you start using the ASP.NET Membership and Role providers. When you use these (integrated into the one database) it adds a lot of aspnet_* tables to your database so when you regenerate your LINQ to Entities/SQL file.

ASPNET-MembershipTables.png DynamicData-MembershipTables.png

You can avoid these issues if you make ASP.NET generate these tables into a separate database but if you like everything in one database then there is a solution for you. T4 Templates - built right into Visual Studio. We can use these templates to automatically exclude the aspnet tables.

  1. Download Damien Guard’s LINQ to SQL templates. You will only need the L2ST4.ttinclude file.

     

  2. Create a MetaData folder in your solution
  3. Add new new file and call it MetaData.tt (you might be prompted with a warning - click OK)
  4. Paste the following code in:
    		<#@ template language="C#v3.5" hostspecific="True"#>
    <#@ include file="../L2ST4.ttinclude"#>
    <#@ output extension=".generated.cs"#>
    <# // Template by Eric Phan http://ericphan.info inspired by Damien Guard http://damieng.com
    var data = new Data(Host.TemplateFile.Replace("MetaData.tt","../NAMEOFYOURDBML.dbml")); 
    var code = new CSharpCodeLanguage();
    data.Serialization = SerializationMode.DataContractSP1;
    #>
    #pragma warning disable 1591
    //------------------------------------------------------------------------------
    // 
    // This code was generated by LINQ to SQL template for T4 C# v0.79
    // Generated at <#=DateTime.Now#>
    //
    // Changes to this file may cause incorrect behavior and will be lost if
    // the code is regenerated.
    //
    //------------------------------------------------------------------------------ using System; using System.ComponentModel; using System.Data.Linq; using System.Data.Linq.Mapping; using System.ComponentModel.DataAnnotations; using <#=data.EntityNamespace#>.MetaData; namespace <#=data.EntityNamespace#>.MetaData { <# foreach(Table table in data.Tables) { foreach(TableClass class1 in table.Classes) { if (class1.Name.StartsWith("Aspnet_")) {#> [ScaffoldTable(false)]<#}#> public partial class <#=class1.Name#>MetaData {<# if (class1.Columns.Count > 0) foreach(Column column in class1.Columns) if (column.IsPrimaryKey) { #> [ScaffoldColumn(false)] public object <#=column.Member#> { get; set; } <#}#> } <#}}#>} namespace <#=data.EntityNamespace#> { <# foreach(Table table in data.Tables) { foreach(TableClass class1 in table.Classes) { #> [MetadataType(typeof(<#=class1.Name#>MetaData))] public partial class <#=class1.Name#> { } <#}}#> } #pragma warning restore 1591
  5. RIght click on MetaData.tt and click Run Custom Tool
  6. Look at the MetaData.generated.cs file

Note: MetaData.tt will create all partial classes based on your DBML file, create the associated metadata classes and associate them to the class. It will also add the ScaffoldTable(false) attribute to any Aspnet tables and automatically unscaffold any primary key columns (workaround for GUID issue)

Resources

3 Comments