Latest Entries

Access control in masterpage from content page

Here is a nice easy one, if you have a control in your .master file that you need to access from your content page you can do the following to manipulate that control.

 
// Create a new object that relates to your .master file control and call the FindControl function to return its properties
Xml xMasterHeaderXML = (Xml)Master.FindControl("xmlHeader");
 
// Do a basic is null check to see if hte control is there or not
if (xMasterHeaderXML != null)
{
     // Set whatever properties and values you need.
     xMasterHeaderXML.DocumentContent = "<root></root>";
     xMasterHeaderXML.TransformSource = "xsl/header.xsl";
}
 

C# Xml Manipulation: Copy Node

Just loop through your xml structure using xPath, create a new node object, set the innertext property to value of the node to copy and then insert the value at the end of the current node set.

 
XmlDocument xdocLocalFile = new XmlDocument();
 
xdocLocalFile.Load("MyXmlFile.xml");
 
foreach (XmlNode xNodeCopy in
          xdocLocalFile.SelectNodes("/root/phunk"))
{
     XmlNode xNodeNew =
     xdocLocalFile.CreateNode(XmlNodeType.Element, "NewNodeName", null);
 
     xNodeNew.InnerText =
     xNodeCopy.SelectSingleNode("CurrentNodeName").FirstChild.Value;
 
     xNodeCopy.InsertAfter(xNodeNew, xNodeCopy.LastChild);
}
 

C# Xml Manipulation: Replace InnerText of Node

Just loop through your xml structure using xPath as usual then call the InnerText property to set the new value.

 
XmlDocument xdocLocalFile = new XmlDocument();
 
xdocLocalFile.Load("MyXmlFile.xml");
 
foreach (XmlNode xNodeReplace in
           xdocLocalFile.SelectNodes("root/phunk"))
{
           xNodeReplace.FirstChild.InnerText = "My new Value";
}
 

C# Xml Manipulation: Remove Node

To remove a node from an xml document just loop through all the nodes using xPath and then call the RemoveChild function like below.

 
XmlDocument xdocLocalFile = new XmlDocument();
 
xdocLocalFile.Load("MyXmlFile.xml");
 
foreach (XmlNode xNodeReplace in
     xdocLocalFile.SelectNodes("root/phunk/firstname"))
{
     xNodeReplace.ParentNode.ParentNode.RemoveChild(xNodeReplace.ParentNode);
}
 

C# Xml Manipulation: Create Node

First loop through your nodes and for each one create a new XmlNode giving the name of the new node and then supplying the inner text to it. Then just add it to the end of the nodes in its parent.

 
XmlDocument xdocLocalFile = new XmlDocument();
 
xdocLocalFile.Load("MyXmlFile.xml");
 
foreach (XmlNode xNodeReplace in
          xdocLocalFile.SelectNodes("root/phunk"))
{
	XmlNode xNodeNew =
        xdocLocalFile.CreateNode(XmlNodeType.Element, "NewColumnName", null);
	xNodeNew.InnerText = "Here is my new value";
 
	xNodeReplace.InsertAfter(xNodeNew, xNodeReplace.LastChild);
}
 

Add a META tag to a head tag through C#

Here is a nice one as well, how to add a meta data tag to your page header tag. This one below adds the nofollow malarky needed for those SEO boffins.

 
HtmlMeta hmRobots = new HtmlMeta();
 
// Get a reference to the page header element.
HtmlHead hhRobots = (HtmlHead)Page.Header;
 
// Define an HTML <meta> element that is useful for search engines.
hmRobots.Name = "robots";
hmRobots.Content = "noindex,noarchive,follow";
hhRobots.Controls.Add(hmRobots);
 

Create a cookie and read it back in C#

Been a bit bare at the mo, lots of dev work going on so thought I would fill it with a simple how to with Cookies.

To create a cookie on your site...

 
// Lets check if it exists or not yet.
if (Request.Cookies["MySite_LastVisit"] == null)
{
 
// Create new Cookie object to store users last visit to the site.
HttpCookie ckUserLastVisit = new HttpCookie("MySite_LastVisit");
 
// Get current date/time
DateTime dtNow = DateTime.Now;
 
// Set the cookie value.
ckUserLastVisit.Value = dtNow.ToString();
 
// Set the cookie expiration date. We will say one day from now
// but you could say as long as you want.
ckUserLastVisit.Expires = dtNow.AddDays(1);
 
// Add the cookie.
Response.Cookies.Add(ckUserLastVisit);
 
}

So that is it created. Lets see how to read that information back.

 
if (Request.Cookies["MySite_LastVisit"] == null)
{
// Just assign our value to a new string and then do what we want with it.
String sLastVisitDate = Request.Cookies["MySite_LastVisit"].Value;
}
 

That should be it, nice and simple.

Convert bytes to Megabytes in C#

Here is a nice little function to quickly get the size of a file on your server. Great for displaying the size of images in a gallery for example.

private String fnGetImageSize(String sFilePath)
{
	try
	{
		System.IO.FileInfo fi = new System.IO.FileInfo(sFilePath);
		return ConvertBytesToMegabytes(fi.Length).ToString("0.00");
	}
	catch
	{
		return "0";
	}
}
 
static double ConvertBytesToMegabytes(long b)
{
	return (b / 1024f) / 1024f;
}

Adding twitter feeds to a C# website using Yedda

I found a great library today for importing twitter feeds to your application using Yedda. http://devblog.yedda.com. Below is a small sample of the basic use.

To install simply download the source, stick the Twitter.cs file in your bin directory and do the following on yoru page load. Then manipulate the returned XML as you wish in your XSL.

C# Code

  1.  
  2. Twitter twitNews = new Twitter();
  3. XmlDocument xmlTwitterDoc =
  4. twitNews.GetUserTimelineAsXML("username", "password", "johnboynolan",
  5. Twitter.OutputFormatType.XML);
  6.  

Simple as that!

Limit the amount of characters in xsl select string

Okay, a nice simple one that I found today, might be useful. If you ever have to display content on your site and need to limit the amount of characters in it, such as a summary, then you can use something like the following to display what you need. This also adds '...' after the final characters.

Great if you are not in control of the data source such as twitter feeds.

 
<xsl:value-of select="concat(substring(text, 1, 100), '...')" />
 


Copyright © 2004–2009. All rights reserved.

RSS Feed. This blog is proudly powered by Wordpress and uses Modern Clix, a theme by Rodrigo Galindez.