loads of useful information, examples and tutorials pertaining to web development utilizing asp.net, c#, vb, css, xhtml, javascript, sql, xml, ajax and everything else...

 

C-Sharpener.com - Programming is Easy!  Learn Asp.Net & C# in just days, Guaranteed!

Simplify setting your DropDownLists to a specific value

by naspinski 1/5/2009 6:28:00 AM

An extension to quickly and easily set your DropDowns while avoiding errors

Sometimes you want to set your DropDownList to a value that may or may *not* be in the DropDownList, but setting that can be a bit tricky as it will often error out. This extension will avoid that and give you a simple way to set your DropDownLists wihtout all the extra code:
public static void Set(this DropDownList ddl, string findByVal)
{ // attempts to set a DDL to the 'findByVal'
  try { ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(findByVal)); }
  catch { };
}

Now just call it from your DropDownList:
SomeDropDownList.Set("Some String Value");

And it will attempt to set your DropDownList to that value, but *not* error out if it doesn't exist.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

asp.net | c#

Simple Dynamic Sorting Headers for GridView using indication arrows

by naspinski 1/3/2009 5:20:00 AM

A simple centralized way to make a nice flipping indicator (arrow) of which way you are sorting on a GridView

When sorting with gridviews, it is nice to have an indicator of which direction you are sorting on which field like the use at Yahoo! Autos. To copy this idea, I simply made 3 Css classes 'sort', which is a base class with no backgorund image, 'up' which has an up arrow, and 'down' which has a down arrow in it. I made sure to put the backgorund image in just once, and push the text away so it can show through. Also, since these will be applied to the Header element of the GridView, you have to make sure you declase '.class a':
.up a, .down a, .sort a { display:block; padding:0 4px 0 15px; }
.up a, .down a { color:#8F5F00; }
.sort a:hover { background:#ffcc66; }
.up a { background:url(../images/up.gif) left no-repeat; }
.up a:hover { background:url(../images/up.gif) left no-repeat #ffcc66; }
.down a { background:url(../images/down.gif) left no-repeat }
.down a:hover { background:url(../images/down.gif) left no-repeat #ffcc66; }

Now, I made a function in a helper Class that will take in the same arguments that a GridView Sorting event throws, that way the transition will be easy.
// This is used to flip the sorting arrow up/down
// Base Css class is 'sort', the Ascending Css Class is 'up' and Descending is 'down'
public static void GVSort(object sender, GridViewSortEventArgs e)
{ // call on sort and sets the sorted field to the proper Css Class, while setting all others to the base class
  string BASE = "sort";
  string UP = "up";
  string DOWN = "down";
  GridView g = (GridView)sender;
  for (int i = 0; i < g.Columns.Count; i++)
  {
    var c = g.Columns[i];
    c.HeaderStyle.CssClass = c.HeaderStyle.CssClass.Replace(UP, BASE).Replace(DOWN, BASE);
    if (c.SortExpression.Equals(e.SortExpression))
    {
      c.HeaderStyle.CssClass =
        e.SortDirection.Equals(System.Web.UI.WebControls.SortDirection.Ascending) ?
          c.HeaderStyle.CssClass.Replace(BASE, UP).Replace(DOWN, UP) :
          c.HeaderStyle.CssClass.Replace(BASE, DOWN).Replace(UP, DOWN);
    }
  }
}

Now say I had this in a class utils.cs I would just call it one the GridView Sorting event:
protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
  sao.GVSort(sender, e);
}

And there you have it! You can call that from every Gridview in your application that you want to look similar - code only once!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

asp.net | c# | css

Convert DateTime to Military Time

by naspinski 12/23/2008 4:58:00 AM

Good ol' military time


To continue on my posts about extend existing classes (I have been doing these a lot at my new job), here is a simple extension to make DateTime.ToMilitaryString() to output a military time such as: 21 DEC 08 : 16:22:34 - it's very simple actually:
public static string ToMilitaryString(this DateTime dt)
{
  string time = dt.ToString("dd MMM yy : hh:mm:ss");
  return time.Substring(0,12) + dt.Hour + time.Substring(14,6);
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

c# | steal some code

Insert commas into your currency values

by naspinski 12/21/2008 2:02:00 PM

Make your currency more readable


Last post I showed how to limit the decimal places of a double and decimal, but when you display a long number that represents currency, this is not what you want your user to see: $1123443322.25, you want it nicely formatted like this: $1,123,443,322.25 - much easier to read!

To do that, just extend an existing class, I am going to use string, but you could easily make this into a number as well.
public static string ToMoney(this string s)
{
  int period = s.IndexOf(".");
  if (period == -1) return s;
  else
  {
    string right_side = s.Substring(period, s.Length - period);
    string left_side = s.Substring(0, period);
    while (left_side.Length > 3)
    {
      right_side = "," + left_side.Substring(left_side.Length - 3, 3) + right_side;
      left_side = left_side.Substring(0, left_side.Length - 3);
    }
    return left_side + right_side;
  }
}

Now just use this as you would any other method of string:
string long_money = 1123443322.25;
string money = long_money.ToMoney();

money now equals 1,123,443,322.25. This would be easy to change in to working with double, int, etc.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

c#