Adding Dynamic Content to Your Pages in ASP.NET
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.




24. Jun, 2007 








No comments yet... Be the first to leave a reply!