How to dynamically change meta tags when using MasterPage in ASP.NET and C#

If you have lots of dynamic pages instead of static, you could be losing out on how your pages are indexed. For example, if you use lots of querystrings, the chances are that all pages (or instances of that page) have the same meta tags (title, description, keywords). To overcome this, we can set up our site so that we can set these meta tags dynamically (upon certain actions). If using a MasterPage, this can get a little trickier, but can still be done. This tutorial shows how.
Firstly, on our MasterPage, we give our meta tags and the page title an ID, and runat attribute:

Next, we put the following code in MasterPage code-behind:

The Default.aspx (content page) looks like the following:

Then in the code-behind of Default.aspx we have the following, which will set the meta tags:

Download Source Files