Tagged with c#
Saving server control properties to ViewState with custom attributes
Use custom attributes to take the tedious work out of creating server control properties that persist to ViewState. Declare a normal property and use an Attribute to tag it for storage to ViewState. See how to use reflection to see which properties need to be persisted.Published in Server controls on Wednesday, August 13th, 2008
Generic method return types
Generics aren't just great for creating lists, they can also be used to return strongly typed information from a method. This example shows how to use a generic return type to parse any type of strongly typed primitive from a string.Published in .NET on Monday, May 5th, 2008
One more thing to love about .NET generics: events with custom event args
Lists are a killer app for generics in .NET 2.0 but one of my favourite uses that gets less attention is declaring event handlers without having to create a custom delegeate. Imagine a situation where you want to pass a piece of custom information to the event handler so it can decide whether to cancel [...]Published in .NET on Saturday, April 26th, 2008
Objects, event handlers and "this" in javascript
One recurring problem I’ve had when using a function from a custom javascript object as an event handler is that when the event handler is called the “this” property of the function no longer references the object it originally belonged to. To show you what I mean, take a look at this simple javascript object. [...]Published in Javascript on Saturday, April 26th, 2008
Filtering DataView by DateTime
Just a little code snippet to remind me how to do date comparisons in a DataView using DataView.RowFilter. It needs # characters around the date and you seem to need to generate the date string using InvariantCulture: DataTable table = GetList(); table.DefaultView.RowFilter = string.Format("date = #{0}#", DateTime.Today.ToString(CultureInfo.InvariantCulture)); this.GridView1.DataSource = table.DefaultView; this.GridView1.DataBind();Published in .NET on Saturday, April 5th, 2008
Accessing the contents of a URL through C#
Here’s a quick code snippet for accessing the contents of a url through C#. It’s nice and straight forward. You just need to use the HttpWebRequest and HttpWebResponse System.Net classes to request the url and then a StreamReader to grab the text from the response’s stream. The code should look something like this: HttpWebRequest request [...]Published in .NET on Sunday, August 19th, 2007
What type are you really?
I had an interesting problem the other day. I had a situation where someone could pass a type to my method which could be potentially be a nullable type and I needed to know what the real type actually was. It turned out to be pretty easy to find out. The Type type has a [...]Published in .NET on Friday, May 4th, 2007
UserControl template areas
ASP.NET smartie pants Scott Guthrie has linked to a really interesting article about supporting templated areas in ASP.NET controls. This is aimed at people doing user controls, but the same thing will work if you want to have a templated area in your own custom control. I’ve created a little sample that shows how to [...]Published in Server controls on Saturday, June 10th, 2006
Pausing page rendering at a certain point
I don’t know if this will be useful to anyone else but I thought this was kind of a neat trick for messing around with ASP.NET page rendering for debugging purposes. My problem was I wanted to stop the rendering of a page at a certain point so I could see how the scripts were [...]Published in ASP.NET, Javascript, Server controls on Friday, April 7th, 2006
Using the ASP.NET 2.0 Web Resource Attribute
ASP.NET 2.0 has a really nice feature that lets you compile site or component resources like javascript files into your assembly file instead of deploying these shared files to the wwwroot/aspnet_client directory like in ASP.NET 1.x. This makes your component/website much easier to deploy because it means you can keep everything you need in one [...]Published in ASP.NET, Server controls on Wednesday, January 11th, 2006
Where have the web project files gone?
I’m using ASP.NET 2.0 today to actually try to do something. I’ve opened it up before and had a bit of a look around, but this is the first time I’ve actually needed to make a new project and write a bit of code. I’m trying to breath deeply and tell myself that change isn’t [...]Published in ASP.NET on Tuesday, October 11th, 2005
Refactoring tool that looks at C# and aspx files
Saw an interesting looking refactoring tool called DevAdvantage that looks like it’s a cross between FXCop and Resharper. I haven’t actually taken a look at it yet, but I really liked the fact you could set rules for things in aspx files. I think the fact that aspx files aren’t really treated like “real code” [...]Published in ASP.NET on Saturday, October 1st, 2005
Accessing embedded resource files
To get a resource from the current assembly: Stream stream = this.GetType().Assembly().GetManifestResourceStream("namespace.file"); To get all the names of all the resources in your current assembly: string[] resources = this.GetType().Assembly.GetManifestResourceNames(); foreach(string resourceName in resources) { Debug.WriteLine(resourceName); }Published in .NET on Tuesday, March 1st, 2005
Formatting currency
Found a rather nifty trick for formatting double values into currency strings. It’s really simple! You just specify “C” as an argument to the ToString method and it automatically generates a currency string that’s in the right format for whatever your local settings are. value.ToString("C"); There’s lots of other stuff you can do with the [...]Published in .NET on Tuesday, January 25th, 2005
Handling SelectedIndexChange for a DropDownList in a Repeater
Came across an interesting problem yesterday trying to handle the SelectedIndexChanged event for a DropDownList that was inside a Repeater. I found a wonderful article on Databinding that gave me most the information I needed, but their method of handling events just didn’t work for me. My first problem was that I originally did the [...]Published in ASP.NET, Javascript on Tuesday, January 25th, 2005
Sending email in ASP.NET
Sending mail in ASP.NET is simple! You just create a message and then call the SmtpMail’s static send method: MailMessage message = new MailMessage();message.To = "me@email.com";message.From = "me@email.com";message.Subject = "An email from helen";message.Body = "Hello Helen. From Helen.";SmtpMail.Send(message); If you need to talk to a non-local server, that’s not really much more difficult. You just [...]Published in ASP.NET on Wednesday, December 8th, 2004
Treating a mapped drive as local
I’ve been having to keep my NUnit tests on my local drive because the network drive the rest of my project lives on doesn’t have enough permissions to run them. This is bad because I can’t keep the tests with the code they’re testing and I keep forgetting to back them up. The PC I’m [...]Published in Sysadmin on Monday, November 22nd, 2004
C# syntax for creating an abstract property
public abstract int GetX { get;}Published in .NET on Wednesday, November 17th, 2004
protected internal access modifier
Here’s how you create a property (though it could be variable, method or I think even class) that can be see by things that subclass the object it belongs to and things that are in the same package: protected internal object myProperty { get { return m_property; } set { m_property = value; } } [...]Published in .NET on Wednesday, November 10th, 2004
Loading and saving registry information in C#
Saving info to the registry for the currently logged in user: RegistryKey key = Registry.CurrentUser.CreateSubKey("MyRegistryKey"); key.SetValue("MyKeyValue", "Some value"); Loading the same info from the registry: RegistryKey key = Registry.CurrentUser.CreateSubKey("MyRegistryKey"); if(key != null) { string keyValue = (string) key .GetValue("MyKeyValue"); } Reference: RegistryKey Class MSDN documentationPublished in .NET on Monday, November 8th, 2004
Writing text files in C#
My quick-start file writing example for those who go months between needing to write a file. StreamWriter writer = new StreamWriter(filename, false); writer.Write("some text"); writer.Close(); I remember it being harder than this.. Reference: Streamwriter documentationPublished in .NET on Friday, November 5th, 2004
Defining the entry point for a C# application
This is the C# equivalent of the Main(String[] args) method in Java: [STAThread]static void Main() { Application.Run(new MyStartupClass());} This code usually goes in the class that defines your first windows form.Published in .NET on Friday, November 5th, 2004
Formatting an XML string in C#
Here’s a nifty trick if you’ve got a chunk of XML you want to format nicely: XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(xmlStringVariable); MemoryStream memoryStream = new MemoryStream(10240); xmldoc.Save(memoryStream); string formattedXml = Encoding.ASCII.GetString(memoryStream.GetBuffer()); memoryStream.Close();Published in .NET on Friday, November 5th, 2004
An advantage of C# properties
Just found a good reason to use properties in C# rather than public variables: they’re much easier to debug. When I was getting an unexpected value in the variable, I put a break point in the property and was able to really easily find out where it was being set.Published in .NET on Thursday, November 4th, 2004
Raising custom events in C#
First you need to declare an event in the class that’s going to raise the event. This is just like declaring a normal variable: public event EventHandler MyEvent; Then you need a method that’s going to be called to raise the event. This goes into the same class that you just declared the event in. [...]Published in .NET on Wednesday, November 3rd, 2004






