Fun Infused Games  |   Smooth Operator

  Home   |    Archive   |    About
Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact and I will try and recover/find it.

11 Quick and Useful Tips and Tricks
Date 11/11/2008    Tags, C#    (0)

Here are ten eleven tips and tricks I've collected while developing applications. None of these are big enough to warrant their own article, but they are useful just the same. If you have any quick tips to add, I'd love to read about them in the comments below.
  1. Insert <%# Container.DataItemIndex %> in a Gridview to display the current row. If you're displaying this on the screen, add + 1 to it since the container is zero-indexed.

  2. Adding "return false" to the onClick function of an ASP button will prevent it from causing a postback.

    <asp:Button ID="btnMyButton" runat="server" Text="Click Me" OnClientClick="alert('Woot'); return false;" /> 
    When clicked, this will display an alert message without causing the page to postback afterward. You certainly could just use HTML buttons to get this same effect, but sometimes it's nice to have ASP buttons instead, even if just for consistency’s sake.

  3. Get the version number of your app from AssemblyInfo.cs using this code:

    string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
    And if you want to get this information from a component to your web app, use this:

    String version = System.Reflection.Assembly.GetCallingAssembly().GetName().Version.ToString();
  4. When you create an page that uses AJAX update panels and view the source of that page in Internet Explorer, the source you see is the source exactly as it was when the page was initially loaded. Changes made to values or controls on subsequent asynchronous postbacks are not reflected in the source you see. If you want to see how the source really looks after doing AJAX postbacks, use a tool like FireBug or the Visual Studio IDE (much improved for this in 2008).

  5. Add Attributes to a header of a GridView control in the RowDataBound event with the following code:

    if (e.Row.RowType == DataControlRowType.Header)
        for (int i = 0; i < e.Row.Cells.Count - 1; i++)
            e.Row.Cells[i].Attributes.Add("onclick", "alert('Stop clicking me!');");
  6. Get just the name of the page you're at:

    string currentPage = System.IO.Path.GetFileNameWithoutExtension(Request.PhysicalPath);
  7. Get the length of the largest amount of text stored in a database field.

    SELECT TOP (1) DATALENGTH(Summary) AS FieldLength
    FROM MyTable
    ORDER BY FieldLength DESC
  8. To display just the date from the a DateTime field inside a GridView, inside a template column use:

    <asp:Label ID="lblDate" runat="server" Text='&

    This article has been view 1185 times.


No comments for this article.

Add Comments

Name *
  Name the animal in the picture below:

Comment *
Insert Cancel
Things To Click

Video Games (7)  Trivia or Die (3)  SQL (1)  iOS (3)  Game Dev (11)  Advise (14)  PC (1)  World of Chalk (2)  FIN (20)  Abduction Action! (27)  XBLIG (32)  Abduction Action (1)  Nastier (4) (18)  Absurd (2)  Volchaos (11)  Web (19)  Fin (1)  XNA (40)  Rant (50)  Cool (2)  Visual Studio (1)  Trivia Or Die (1)  Xbox (1)  C# (14)  Sports (11)  Design (2)  Development (13)  Hypership (28)  WP7 (8)  VolChaos (1)  Nasty (34)  Abdction Action! (1)