KoffeeKoder


  • Dynamic Website Project allows you to create customizable support pages. This can be really helpful if part of your job is to create boring support pages. Recently, I had to create few support pages so I thought why not try the Dynamic Website Project. So, I added the LINQ to SQL as the data source and used one of the databases to create the support pages.
    In our application whenever a row in the database is updated we update the DateModified field. This can easily be performed inside the GridView_RowUpdating event as shown below (ListDetailsTempate.aspx):

     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            e.NewValues["DateModified"] = DateTime.Now;
        }

    Now, whenever you update any row the DateModified field wil be modified automatically.
    Another thing that I needed to add was the support for choosing multiple databases. If I add a table from a different database to the LINQ to SQL designer then it will use the new database as the primary database and you will not be able to access the old database tables. This is because the connetionString will be overriden and will be pointing to the new database. To fix this issue I created a DropDownList which is populated with all the names of the databases and their respective context classes(The context classes will be the classes automatically generated by the LINQ to SQL).

    <dataContextSettings>
      <dataContext name = "Exams" type="CasaSupportPages.DatabaseContexts.ExamsDataContext" />
      <dataContext name="Users" type="CasaSupportPages.DatabaseContexts.UsersDataContext" />
    </dataContextSettings>

    Here is the code to populate the DropDownList:

     private void PopulateDropDownList()
            {
                XDocument doc = XDocument.Load(Server.MapPath("~/ContextSettings.xml"));
                foreach (var element in doc.Root.Elements("dataContext"))
                {
                    
                    ddlDatabases.Items.Add(new ListItem((string)element.Attribute("name"),(string) element.Attribute("type")));    
                }            
            }  

    Finally, after selecting the correct database you press the button and now your Dynamic Website will be created using the new database.

    public static void SetDatabaseContext(string contextType)
            {
                Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
                ConfigurationSectionGroup group = config.GetSectionGroup("system.web.extensions");
                ConfigurationSection section = group.Sections["dynamicData"];
                PropertyInformation propInformation = section.ElementInformation.Properties["dataContextType"];
                propInformation.Value = contextType;
                config.Save();
            }

    This way you can create support pages (dynamic pages) using multiple databases.