May 10

Dynamic data pages: auto.axd, the new handler in town…

Posted in ASP.NET      Comments Off on Dynamic data pages: auto.axd, the new handler in town…

[After talking with my friend JPC, I think I should change the title of these posts After all, aspx pages are DYNAMIC pages!]

The auto.axd handler is a cool handler that lets you navigate through all the data maintained on a database. To illustrate the usage of this handler, lets start by building a simple database that only has 2 tables: Students and Contacts (which I”ll be using in the next posts). Here”s the SQL code to generate it.

After creating the tables, it”s time to configure our Dynamic web app to use our database. The first thing we need to do is add a connection string to our web.config file:

   <add name=”myCnn”
          connectionString=”data source=.SQLEXPRESS;Integrated      
          Security=SSPI;AttachDBFilename=|DataDirectory|demo.mdf;User Instance=true” />

Since we”re at the config file, lets enable the auto.axd handler:

<add path=”auto.axd” verb=”*” type=”Microsoft.Web.DynamicDataControls.AutoHandler”/>

If you open the browser and point it to your localhost/folder/auto.axd, you”ll get a simple page that lets you navigate quickly between the tables of your database. Having this handler active is a security hole, but it”s really a cool way of getting started. Note how you can navigate between your tables and

As I”ve said before, the Dynamic web app template adds several elements to the web.config file. Today we”re particulary interested in the <dynamicDataControls> section. The element has a boolean attribute (showAllTables) which lets you specify if your tables should be shown in your page (if you don”t set it to true, you won”t see anything when getting info from an SQL Server database; however, it seems like EXPRESS has a different behavior. if you use an EXPRESS db, setting it to false won”t be enough for not getting the list of custom tables – ie,you”ll still get them even if you do that. ). In my opinion,you should always set it to false in a real app. Besides this attribute, there are another two:

  • connectionString: lets you specify the connection string name you”re using in this dynamic app (keep on reading the next paragraphs for more info). As you”ve guessed by now, this implies that you”ve added a connection string to the <connectionStrings> section;
  • dataLayerType: used to define the data layer class that should be used. Currently, there”s only one (SqlDataLayer), but nothing prevents you from implementing your own layer.

Before ending the post, let”s just spend some time analysing the connectionString attribute. You should use it when you have more than one connection string and don”t have a single mdf file on the app_data folder. If you have a single mdf file on that folder, the app will use that  file (even if you have several entries on the <connectionStrings> section). If you don”t have one (or have more than one) and don”t have a single entry on the <connectionStrings> section, you”ll get an exception which says that you must specify the connection string you want to use.

If you have a single connection string (and no single mdf file), then you don”t need to set the attribute since your connection string entry will be used automatically. btw, do note that in this example you do not need to specify a connection string if you”ve already have a single mdf file on the app_data folder (like i do).

on the next post, we”ll start looking at the dynamic controls.