Home | 2.0 Content | Code Samples | Tips and Tricks | -- Tutorials -- | ASP.Net Books | Resources | Hosting
The Best Place Yet for ASP.Net - ASPNet101.com
Power Search


Don't Miss any New Content!
Sign up for Newsletter!

  Menu  
  Code Samples
Tips and Tricks
Tutorials
ASP.Net FAQ
Training
Resources
Hosting
Recommended Books
Home
 
     
  Our Sponsors :  
 
ASP Express WebHost4Life DotNetSlackers
 
     

72
If you find this site helpful, please donate to help keep it online


     
 
   

Adding Dynamic Content to Your Pages
(Data Driven Page Title, Meta Tags and Content)

In a data-driven website, there becomes a need to have only one page serve up different data, based on the call to the database. Naturally, you wouldn't want to have the same Title on the page's drag bar, and you certainly would like to have different Keywords in your meta tag along with every section of content received from the database. This tutorial will attempt to provide a simple example/overview of how to dynamically add, not only the content, but the Title of the page and the Keywords for the HTML Meta Tags in order to get you started in this direction.

First we'll start with the database table structure. Keep in mind, I previously said 'a simple way', so, keeping that in mind, here would be four fields needed to do this:

  • id (in MS Access, Autonumber/in SQL Server - Identity)
  • Title
  • Keywords
  • Content - content for the page
For each section of content we plan to show in this page, when we enter the data into the table, we will, of course, make the fields not accept nulls.Since the ID field is autonumbering, we don't need to worry about that field when inserting data. When we call this page (let's call it 'Content.aspx'), it will need a querystring added to it, which will be based on the ID field - something like 'Content.aspx?id=1". This way, we will then query the database in the Page_Load event, requesting record #1 from our table. it will then return the Title, Keys and the Content for the page. Outside of Page_Load, we dimension the variables:
Dim sKeys, sTitle, sContent as String
We'll only need a DataReader to retrieve this information, and we can put the information into variables for use within the page, like this:
While objDR.Read
	sContent=objDR("Content")
	sTitle=objDR("Title")
	sKeys=objDR("keywords")
End While

For the Meta Tag Keywords, we'll use an ASP.Net Literal control, within the HTML Head Tags called 'litMeta':

<asp:Literal ID="litMeta" runat="server"></asp:literal>
Then, when we retrieve the information from the database, we'll populate the Meta Tag like this:
litMeta.text="<META NAME='KEYWORDS' CONTENT='" & strKeys & "'>"
For the Title of the page, we'll use another Literal Control, called 'litTitle':
<title><asp:Literal ID="ltTitle" runat="server"></asp:literal></title>
Then, to populate the Title when we retrieve the information for the database:
ltTitle.text="The Basic Name of your page - " & strTitle
As you can see, each of these uses the Literal control in different ways, based on the needs of that particular tag, showing that there are many different ways HTML tags can use data in ASP.Net.

Now - for the content portion of the page. This can be done in many different ways. For my tutorial pages on the site, I put each tutorial in a user control, and then, all I add to the Content field in the database is the name of the ASCX page itself. I place an ASP.Net Panel Control (called 'ph1') on the page where I want the user control loaded. When the page loads, I then just ad the particular user control to the panel:

Dim myControl as Control=CType(Page.LoadControl(sContent), Control) ph1.Controls.Add(myControl) ph1.visible="true"
Naturally, you could just enter the HTML text for your page into the Content field and not have to worry about loading controls. To do this, you could just add another Literal Control to the page (let's call it 'litContent'), and add the HTML text in the database to that literal:
litContent.Text=sContent
Of course, if you didn't want to worry with the HTML markup of the page, you could purchase one of the great Rich Content Controls on the market, which will replace the Multiline textbox with their control, and you could add the content in much the same concept as a MSWord or any of the WYSIWYG HTML editors.

As you see, this tutorial has given you an 'overview' in how to get started with a Data-Driven website concept. Hopefully, it has met its purpose and given you lots of ideas on how you can now recreate this concept on your own website.

   
Choose From Tutorials:
    Beginners Guide to Functions    
    Defining Error Displays Globally    
    Creating a Method to Return a DataSet in C#    
    Creating a Feedback Form    
    Beginners Guide to Comparing Strings    
    Introduction to Parsing Strings    
    Beginners Guide: Scoping of Methods    
    Adding 'Last Updated' To Footer    
    A Beginners Guide to the HyperinkField    
    Error Trapping in ASP.Net    
    Page Level Impersonation    
    Creating a Hit Counter    
    Inserting and Updating with a SQLDataSource    
    Sorting/Filtering Fixed DataSet Results    
    Using PreviousPage with a Master Page    
    The Basics of Using SQL    
    Helper Functions    
    Querystring Results and SQL Statements    
    A Beginners Guide to the Connection String    
    Beginners Guide to Com in ASP.Net    
    Membership/Roles with Remote DB    
    Creating a Login/Email/Activation Page    
    Multiple DropdownLists with AppendDataBoundItems    
    The Beginner's Guide to an ArrayList    
    Creating a Popup Details Page    
    Configuring a Trusted SQL Srvr Connection    
    Beginners Guide to the BulletedList    
    Adding Dynamic Content to Your Pages    
    Emailing Form Results with ASP.Net 2.0    
    Menu User Control with Rollovers    
    Using TemplateFields    
    Transferring Form Results to 2nd Page    
    Buttons and LinkButtons    
    Using the AdRotator Control    
    Two Page Data Retrieval    
    The 3 'Execute' Command Methods    
    Using the QueryStringParameter    
    Displaying DataBase Information    
    Beginner's Guide to Master Pages    
    Sessions/Visitors Online    
    Beginner's Guide to Themes    
    Sending Multiple Emails At Once    
    Sending Emails With ASP.Net 2.0    
    Emailing Form Results    
    A Beginner's Guide to the GridView    
    Inserting Data Into Two Tables    
    A Beginner's Guide to the DataSource Control    
    File System List With System.IO    
    Coolest New v2.0 Additions    
    Understanding Regular Expressions    
    Remote SQL Server/Web Matrix Server/WinXP Home    
    Parameterized Queries - Part 2    
    Creating a 'States' User Control    
    Creating an Online Bible    
    Iterating Through a List-Type Server Control    
    Nesting Server Controls    
    Beginner's Guide - Installing MSDE    
    String Properties/Methods - Part 2    
    Function Libraries    
    String Properties/Methods - Part 1    
    An Introduction to Validation Controls - Part I    
    Examining List Controls    
    Sending Mass Emails Part 2    
    If Not Page.IsPostBack    
    Beginners Guide to Forms Authentication    
    Single & Double Quotes in SQL    
    Using MySQL with ASP.NET    
    Parameterized Queries in ASP.Net    
    Matching Regular Expressions    
 
     
Home | 2.0 Content | Code Samples | Tips and Tricks | -- Tutorials -- | ASP.Net Books | Resources | Hosting
Send us your comments, questions or suggestions : Suggestions
Copyright © 2010 ASPNet101.com All Rights Reserved