June 25th, 2008 Luke
For a recent project, I had to deal with a fair number of controls on a page that all needed to be disabled at once. This is the function that I wrote to do so:
void DisableControls(List types, ControlCollection controls) {
foreach(Control control in controls) {
if(types.Contains(control.GetType())) {
((WebControl)control).Enabled = false;
}
}
}
Posted in ASP.Net, Code, csharp, snippet | No Comments »
February 20th, 2008 Luke
Have you ever been working on a project and needed a relative path for something?
Under C# itself you could use something like ~/path/to/foo, but that won’t work on a website - the tilde doesn’t always get expanded.
However, there is a nicer way. If you use Page.ResolveClientUrl(), you can easily resolve files from their ASP.net path of ~/path/to/foo to a relative path like folder/path/to/foo, or even path/to/foo, depending on where the page that is making the call to Page.ResolveClientUrl() is located. Here’s an example of how to use the code:
string myPath = Page.ResolveClientUrl("~/admin/pages.aspx");
If that code had been called from the file located at ~/admin/page2.aspx, myPath would be “pages.aspx”. If it had been called from ~/foo/main/default.aspx, myPath would be “../../admin/pages.aspx”.
Posted in csharp, snippet | No Comments »
January 31st, 2008 Luke
For a recent project, I needed to output integers to show the change in two values.
When an integer is negative, the negative sign(’-') is automatically added to it - but when you output an integer that is positive, there is no positive sign(’+') added to the beginning. I wanted to display the positive sign as well, so that users could easily tell when the values were positive vs. negative.
If you pass this format string into String.Format(), you will receive numbers with the proper signs applied:
"{0:+0;-0;0}"
Here are some examples:
String.Format("{0:+0;-0;0}",22); // returns "+22"
String.Format("{0:+0;-0;0}",-22); // returns "-22"
String.Format("{0:+0;-0;0}",0); // returns "0"
If you’re looking for a little more information on things you can do with String.Format, check out Kathy Kam’s .Net Format String 101 post.
Posted in csharp, format, snippet | No Comments »
January 25th, 2008 Luke
Have you ever encountered a situation where you’re pulling data out of tables that is supposed to be numeric, but for some reason occasionally has NULLs inside of it?
Handling those NULLs inside of your application is a pain - wouldn’t it be nicer if there was just a way to force those NULLs to become 0s?
As it turns out, there is. And it’s a pretty simple SQL query, making use of the CASE statement:
SELECT case WHEN column_value is null then 0 else column_value AS column_value FROM table WHERE condition
And that’s all there is to it. When there’s a null value retrieved you’ll get a 0, and when there isn’t you’ll get the value of the column.
Posted in SQL, snippet | No Comments »
January 4th, 2008 Luke
I’m working on a project right now that involves generating PDF reports from a C# client app.
One thing that we thought would be cool is if, after generating the PDF, it was automatically opened in whatever PDF reader the user had on their computer - and as it turns out, there is a simple one-liner that opens the file using whatever application is associated with it on the host machine:
System.Diagnostics.Process.Start(fileName);
Whether you’re passing a PDF, HTML file, or anything else, that call will open it up in the program that the host computer has associated with it.
Posted in PDF, clientapp, csharp, generic, snippet | No Comments »