<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8572931427744315425</id><updated>2011-11-27T16:08:52.904-08:00</updated><title type='text'>Lee @ Austin Media's Blog</title><subtitle type='html'>http://www.austin-media.net</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://austin-media.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8572931427744315425/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://austin-media.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>lee@austin-media.net</name><uri>http://www.blogger.com/profile/11974920222989153130</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8572931427744315425.post-8941752233153684197</id><published>2008-12-30T07:53:00.001-08:00</published><updated>2008-12-30T08:13:28.832-08:00</updated><title type='text'>Composite Controls in ASP.NET (Creating a simple control)</title><content type='html'>Creating composite controls is a very powerful way to create re-usable components for web sites. Once created they can be quickly and easily deployed and offer total control over the components that you use in your composite control.&lt;br /&gt;&lt;br /&gt;Follow the steps below to create your first composite control:&lt;br /&gt;&lt;br /&gt;1. Create a new class with a meaningful name&lt;br /&gt;&lt;br /&gt;2. Import the following libraries (not all are needed but they will come in useful)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;System.Web;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;System.Web.UI;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;System.Web.UI.WebControls;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;System.Web.UI.HtmlControls;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;3. Now make the class you have just created public and inherit from CompositeControl&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;public class MyControl : CompositeControl&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;3a. Add a label control to your composite control by overriding the CreateChildControls method in the body of your class.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;protected override void CreateChildControls()&lt;br /&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;  Label myLabel = new Label();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;  myLabel.text = "Hello world";&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;  Controls.Add(myLabel);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;  base.CreateChildControls();&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;As you can see I have created a Label control, set the text "Hello world" and then added it to the pages Controls collection.&lt;br /&gt;&lt;br /&gt;Any controls added to the Controls collection will be rendered in turn and as long as they are added to this collection they will be rendered.&lt;br /&gt;&lt;br /&gt;3b. This alternative method is more of a raw and lower level way of creating content in your composite control&lt;br /&gt;&lt;br /&gt;Instead of overriding the CreateChildControls method override the RenderContents method in the body of your class.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;protected override void RenderContents(HtmlTextWriter output)&lt;br /&gt;{&lt;br /&gt;  output.Write("Hello world");&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Both of these methods achive exactly the same thing but are very different ways of doing things. and both have their advantages and disadvantages.&lt;br /&gt;&lt;br /&gt;4. Now you have to add the control to a (*.aspx) web page so firstly register the assembly (your composite control). Below is an example but depending on how your project and namespace is named and arranged it will vary.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;&lt;%@ Register Assembly="MyControls" TagPrefix="MyControl" Namespace="MyControls" %&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;5. Add the control to your page. Again the actual code witll vary depending on how you have named things but below is an exaple.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;&lt;mycontrol:mycontrol runat="server"&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now when you view your page in the browser "Hello world" should be written to the screen. Not very useful but shows the basics to creating your own composite controls.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8572931427744315425-8941752233153684197?l=austin-media.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://austin-media.blogspot.com/feeds/8941752233153684197/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://austin-media.blogspot.com/2008/12/composite-controls-in-aspnet-creating.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8572931427744315425/posts/default/8941752233153684197'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8572931427744315425/posts/default/8941752233153684197'/><link rel='alternate' type='text/html' href='http://austin-media.blogspot.com/2008/12/composite-controls-in-aspnet-creating.html' title='Composite Controls in ASP.NET (Creating a simple control)'/><author><name>lee@austin-media.net</name><uri>http://www.blogger.com/profile/11974920222989153130</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8572931427744315425.post-3923708175573549793</id><published>2008-12-22T09:00:00.000-08:00</published><updated>2008-12-22T09:24:49.700-08:00</updated><title type='text'>Creating a dynamic site map using ASP.NET and VS 2008</title><content type='html'>&lt;span style="font-family:arial;"&gt;Site maps are an integral part of Search Engine Optimisation (SEO) and if used along with Google Web Master tools provides an easy and powerful way to automatically get all your pages indexed.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;Creating a sitemap XML file by hand is very easy but what if you have a site where content is being added all the time, it would be very time consuming to add a new entry to your site map file everytime a new item was added to your site. This is a requirement a client had q while back and so I took the following steps to create them a dynamic site map and I now use this code all all applicable sites I create.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;1. Open up your website project in Visual Studio (I use 2008)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;2. Add a new Item to your project, choose "Web Form" and give the file a name (e.g. "sitemap.aspx")&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;3. Once VS has created and opened your new file remove everything from *.aspx file apart from the page declaration line (enclosed in &lt;% %&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;4. Open the *.aspx.cs file and make sure you have imported the "System.Xml" namespace&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;5. Add the following code to the "Page_Load"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;Response.Clear();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;xmlWriter.WriteStartDocument();&lt;br /&gt;xmlWriter.WriteStartElement("urlset");&lt;br /&gt;xmlWriter.WriteAttributeString("xmlns", &lt;/span&gt;&lt;a href="http://www.sitemaps.org/schemas/sitemap/0.9"&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;http://www.sitemaps.org/schemas/sitemap/0.9&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="font-size:85%;"&gt;xmlWriter.WriteEndElement();&lt;br /&gt;xmlWriter.WriteEndDocument();&lt;br /&gt;xmlWriter.Close();&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;This is the basis of your site map file and all its doing is clearing the Response stream (this is what is going to be written to the browser), creates a new Xml text writer and tells that to write it's output to the response stream.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;This means that the actual output will be an XML rather than HTML.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;6. For static pages add an entry like the following (replaing the parts between the &lt;&gt; with the apropriate information):&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;xmlWriter.WriteStartElement("url");&lt;br /&gt;xmlWriter.WriteElementString("loc", "http://&lt;url&gt;/&lt;page&gt;.aspx");&lt;br /&gt;xmlWriter.WriteElementString("changefreq", "weekly");&lt;br /&gt;xmlWriter.WriteEndElement();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;Add as many entries like this as you need and feel free to change the "changefreq" value to anything you want.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;8. Dynamically add other pages. Now the specifics of this will vary depending on how you store and retrieve information but for my example I have an object named "Article" which holds information about an article (ID, title, body and date created) and I have a collection of "Article" objects stored in a generic list called "ListArticles" which is fed straight from the database.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;foreach (Article article in listArticles)&lt;br /&gt;{&lt;br /&gt;  xmlWriter.WriteStartElement("url");&lt;br /&gt;  xmlWriter.WriteElementString("loc", "http://&lt;url&gt;/ViewArticle.aspx?articleID=" + article.ArticleID.ToString());&lt;br /&gt;  xmlWriter.WriteElementString("lastmod", article.DateCreateD.ToString("yyyy-MM-dd"));&lt;br /&gt;  xmlWriter.WriteEndElement();&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;As you can see I am looping round all the "Article" objects in my list and writing each one using my Xml Writer to the response stream.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;Below is the complete example code:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;  Response.Clear();&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;  XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;  xmlWriter.WriteStartDocument();&lt;br /&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;  xmlWriter.WriteStartElement("urlset");&lt;br /&gt;  xmlWriter.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");&lt;br /&gt;  xmlWriter.WriteStartElement("url");&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;  xmlWriter.WriteElementString("loc", "http://&lt;url&gt;/Default.aspx");&lt;br /&gt;  xmlWriter.WriteElementString("changefreq", "daily");&lt;br /&gt;  xmlWriter.WriteEndElement();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;  ListArticles listArticles = new ListArticles();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;  foreach (Article article in listArticles)&lt;br /&gt;  {&lt;br /&gt;    xmlWriter.WriteStartElement("url");&lt;br /&gt;    xmlWriter.WriteElementString("loc", "http://&lt;url&gt;/ViewArticle.aspx?articleID=" + article.ArticleID.ToString());&lt;br /&gt;    xmlWriter.WriteElementString("lastmod", article.DateCreate.ToString("yyyy-MM-dd"));&lt;br /&gt;    xmlWriter.WriteEndElement();&lt;br /&gt;  }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:85%;"&gt;  xmlWriter.WriteEndElement();&lt;br /&gt;  xmlWriter.WriteEndDocument();&lt;br /&gt;  xmlWriter.Close();&lt;br /&gt;}&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8572931427744315425-3923708175573549793?l=austin-media.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://austin-media.blogspot.com/feeds/3923708175573549793/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://austin-media.blogspot.com/2008/12/creating-dynamic-site-map-using-aspnet.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8572931427744315425/posts/default/3923708175573549793'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8572931427744315425/posts/default/3923708175573549793'/><link rel='alternate' type='text/html' href='http://austin-media.blogspot.com/2008/12/creating-dynamic-site-map-using-aspnet.html' title='Creating a dynamic site map using ASP.NET and VS 2008'/><author><name>lee@austin-media.net</name><uri>http://www.blogger.com/profile/11974920222989153130</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
