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

A simple way to add a 'View All' sort option to your DataSource in Asp.Net

by naspinski 12/17/2008 7:58:00 AM

Just use a little SQL trick to simplify and add usability to your DataSource

Often times I have integer fields in my database that serve as categories. These categories are very helpful when sorting for the users, butit is sometimes tough to write a simple, one-line query to output ALL of them; I figured an extremely simple way to do this. This may be a little hard to explain, but it is very useful.

For this example, I am going to use a LINQDataSource

<asp:LinqDataSource ID="ldsCases" runat="server" ContextTypeName="dbDataContext" TableName="Cases" Where="Category == @Category">

   <WhereParameters>

     <asp:SessionParameter Name="Category" SessionField="Cat" Type="Int32" />

   </WhereParameters>

</asp:LinqDataSource>


This is using a Session variable to work with the DataSource which will be set the the integer value that corresponds to the category I want to find. Now this will work just fine if we are trying to just look at a single category, but what if we want to look at all of them? It is really quite simple, now, just add one more OR (||) statement to our SQL and set the Session variable to 0 any time we want to call everything:

<asp:LinqDataSource ID="ldsCases" runat="server" ContextTypeName="dbDataContext" TableName="Cases" Where="(Category == @Category) || ((Category * @Category) == 0)">

   <WhereParameters>

     <asp:SessionParameter Name="Category" SessionField="Cat" Type="Int32" />

   </WhereParameters>

</asp:LinqDataSource>

Be the first to rate this post

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

Tags: ,

asp.net | sql

No applicable method 'Contains' exists in type 'String'

by naspinski 11/18/2008 10:38:00 AM

Solution for this annoying error while using a LinqDataSource

Recently, I was making a seemingly harmless LinqDataSource and I came across this error. I was using the simple (I thought):
Where="title.Contains(@title)"

This error kept popping up and confusing me... Contains sure does exist in String, so why the error?

Turns out that it is trying to run Contains on a NULL - and where does that NULL come from? A tricky little property in your ControlParameter that defaults to true, and you have to manually change it to false:
ConvertEmptyStringToNull="false"

And that should cause your Contains to work now, because every string Contains a String.Empty.

Currently rated 5.0 by 1 people

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

Tags:

asp.net