Attention: We have retired the ASP.NET Community Blogs. Learn more >

Contents tagged with Tips Tricks

  • What version of Adobe Flash are you running?

    If you have ever done any customer support, you probably have a tool box full of different tools and utilities to help you get to the bottom of any customer issue that you run into.  I found another great tool the other day to help with investigation into Adobe Flash issues.

    This simple Adobe flash file will display the customers current Adobe Flash version:

    http://www.adobe.com/swf/software/flash/about/flashAbout_info_small.swf

    In Internet Explorer 8 I get this:

    image

    In Google Chrome I get:

    image

    Simple but yet so powerful.  So mighty customer support representative, make sure you have this tool on your utility belt.

    Technorati Tags: ,,
  • How to disable an ASP.NET linkbutton when clicked

    Scenario: User clicks a LinkButton in your ASP.NET page and you want to disable it immediately using javascript so that the user cannot accidentally click it again. 

    I wrote about disabling a regular submit button here: How to disable an ASP.NET button when clicked.  But the method described in the other blog post does not work for disabling a LinkButton.  This is because the Post Back Event Reference is called using a snippet of javascript from within the href of the anchor tag:

    <a id="MyContrl_MyButton" href="javascript:__doPostBack('MyContrl$MyButton','')">My Button</a>

    If you try to add an onclick event to disable the button, even though the button will become disabled, the href will still be allowed to be clicked multiple times (causing duplicate form submissions).  To get around this, in addition to disabling the button in the onclick javascript, you can set the href to “#” to prevent it from doing anything on the page.  You can add this to the LinkButton from your code behind like this:

    MyButton.Attributes.Add("onclick", "this.href='#';this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(MyButton, "").ToString());

    This code adds javascript to set the href to “#” and then disable the button in the onclick event of the LinkButton by appending to the Attributes collection of the ASP.NET LinkButton control.  Then the Post Back Event Reference for the button is called right after disabling the button.  Make sure you add the Post Back Event Reference to the onclick because now that you are changing the anchor href, the button still needs to perform the original postback.

    With the code above now the button onclick event will look something like this:

    onclick="this.href='#';this.disabled=true;__doPostBack('MyContrl$MyButton','');"

    The anchor href is set to “#”, the linkbutton is disabled, AND then the button post back method is called.

    Technorati Tags:

  • Batch file that monitors the number of files in a directory

    Sometimes the fastest and easiest way to complete a task is a good old MS-DOS batch (.bat) file.  A friend recently needed to get a quick fix in place for their production machine.

    “I need to whip up a batch file that monitors the number of files in a directory.  Basically, I want to see if there are 100 files in a directory and then start and stop a service to do some quick cleanup.”

    The For command can be used to loop through the files in a folder and get a count and then do something based on that number.

    @ECHO OFF

    SETLOCAL
    SETLOCAL ENABLEDELAYEDEXPANSION

    SET count=0
    for %%o IN (D:\temp\*.*) DO (
          echo %%o
          SET /A count=count + 1
    )

    echo %count%

    IF "%count%"=="100" ECHO NET STOP MyApp

    ENDLOCAL ENABLEDELAYEDEXPANSION
    ENDLOCAL

    Technorati Tags: ,


  • How to prevent scrollbar jump in Firefox and Chrome

    On Firefox and Chrome if the content of a page is not longer than the window, then the right scrollbar is not displayed, and likewise if the content of the page is longer than the window then the right scrollbar is displayed.  This makes perfect sense except that for websites that are center justified (which is most websites these days) then the center content will appear to jump to the left when switching from a page with content that is shorter than the window to a page with content that is longer than the window.  This jump can be a bit annoying since the header shift makes your eyes think something may have changed in the header (at least it does for me) so a rescan of the header occurs.

    The best way to explain this is by an example.  The Vaasnet home page is a short page with all of the content fitting within a single window (or above the fold):

    image

    Now switch to the Vaasnet Features page (which has content that is longer than the window or pushes below the fold) and the right scroll bar appears.  This causes the entire page to shift to the left which looks somewhat not esthetically pleasing.  It is hard to show in screen shots but it is much more apparent when browsing the site and the shift in the header and navigation is apparent and somewhat irritating.

    image

    There is a simple css fix to correct this problem.  Just add the following style to your site:

    html {overflow-y:scroll;}

    Now the vertical scrollbar will always show on the right and you have eliminated the jump in the page content.  (Notice the vertical scroll bar on the right hand side of the Vaasnet home page now.)

    image



  • How to show white space in Visual Studio

    In Microsoft Word I usually have the paragraph symbol turned on to Show Hidden Formatting Symbols (white space) so that I can clearly see spaces, tabs, and other white space characters. 

    image

    image

    Up until recently I had no idea that Visual Studio had the same option to display white space.  Jeff Atwood’s recent blog post Whitespace: The Silent Killer had a screen shot on some code showing white space characters, so I hunted around in the Visual Studio toolbars for a little bit and found the option under the Edit menu.

    Edit > Advanced > View White Space

    image 

    image

    You can also use Ctrl+R, Ctrl+W to toggle it on and off.

     



  • How to quickly format a C# class file – Ctrl-K, Ctrl-D

    In C# as you are typing in a class file the indenting of your code can easily get out of alignment making the code hard to read.  Where does your class, if block, or method begin and end??

    image

    You can manually go and fix each line but there is a much simpler way to correct the indenting.  You can use Ctrl-K, Ctrl-D to format the entire document.  Now your code will be reformatted to the correct indenting for your page.

    image

    Couple things to note:

    • If there are errors in the structure of your code (missing closing brackets, missing semi-colon, etc.) then this will not work.  This will still work with other errors such as undeclared variables.
    • If you are working on legacy code (for example fixing a bug) and there is a different indenting structure, it is good practice to stick with the legacy indenting and not modify the indenting of the entire file just because you fixed a bug in one line of code.  You should try to minimize your code change to only what was necessary to fix the bug (don’t reformat the entire file).  The next person that does a diff on your check-in with the previous check-in will thank you.
    • I also leave the Visual Studio indentation settings alone and stick with the defaults.  If you have a different indentation style than a code file that was previously checked, this process could also cause the subsequent diff to show lots of red. 
    • You can use Ctrl-K, Ctrl-F on smaller blocks of text, so if you have a strange indenting style then highlighting only your change can correct the indentation on the selected lines and not the entire file.
    • These can be found under the Edit > Advanced Menu:
      • image
    • Cutting and Pasting is also another way to get the content formatted quickly:
      • Ctrl-A, Ctrl-X, Ctrl-V


  • Use Ctrl+. (period) to trigger Visual Studio’s automatic add using statement context menu

    When you type class names that are missing the correct namespace reference, Visual Studio will help you out by giving you a context menu that you can use to automatically add the correct using statement to the top of your class.

    For instance, in the example below, the Utility class exists in a different namespace that is not currently referenced in the class.  Visual Studio gives you the red squiggly to show there is an error but there also is a small little line at the end of the class name:

    image

    You can take your mouse and hover just over that line and you will get a little drop-down context menu:

    image

    Which is a very nice feature where Visual Studio will automatically add the using statement to the top of your file (or insert the full reference):

    image

    The feature to add the using statement to your class is great in itself but getting your mouse into just the right spot to click that little line is pretty tricky (and also can break your development train of thought). 

    As the tool tip says, you can hit Shift+Alt+F10 to get the context menu but Ctrl+. (period) will also pop up that menu and it is a much easier key combination to use.



  • Parse a non-standard date string into a System.DateTime object

    The other day I had a date that was in the form of yyyyMMdd and I needed to parse that string into a System.DateTime object.  Just trying to parse the string into a System.DateTime object would not work:

    String dateString = "20091016";
    DateTime d = DateTime.Parse(dateString);

    The above code results in an exception:

    FormatException: String was not recognized as a valid DateTime.

    I was guaranteed to have the date string in the form of yyyyMMdd so my initial thought was to use Substring to break it into the individual year, month, and day parts and create a new System.DateTime object from those pieces. 

    But then I discovered that you can use the DateTime.ParseExact method and a System.Globalization.DateTimeFormatInfo object to specify the pattern for the date that is being parsed. 

    Here is how I was able to parse a non-standard date string into a System.DateTime.

    System.Globalization.DateTimeFormatInfo di;
    di
    = new System.Globalization.DateTimeFormatInfo();
    di.FullDateTimePattern = "yyyyMMdd";

    String dateString = "20091016";
    DateTime d = DateTime.ParseExact(dateString, "F", di);

    By the way, this is also a great way to parse a credit card expiration date that is in the form of MMyy to a System.DateTime.  Just use a pattern of MMyy for the FullDateTimePattern property.



  • osql.exe and unicode files – how to save your sql scripts with encoding

    osql.exe is a great application for running sql scripts in a batch.  I use a batch file to execute multiple sql scripts that I use to rebuild my current application database from scratch.  When developing a brand new application, deploying a database in this way makes it really easy to recreate the database just like it will be created on Day 1 when you build out the Production environment. This requires scripting out all of your sql objects and then also having a way to execute all of those sql scripts easily.  That is where osql.exe comes in handy.

    But osql.exe does have one issue that I ran into this week where it does not like UTF-8 (codepage 65001) or UTF-7 (codepage 65000) encoded files.  And sometimes you need to include unicode characters in your sql scripts.  At first I thought osql just did not support unicode but that is not the case… it just does not like the UTF-8 or UTF-7 encoding. 

    Trying to run a UTF-8 (codepage 65001) or UTF-7 (codepage 65000) encoded file with osql.exe will give you errors such as:

    Incorrect syntax near '+'.
    Incorrect syntax near ' '.
    Incorrect syntax near 'ï'.

    Saving the same file with Unicode Encoding (codepage 1200) will work just fine.  Here is how to save sql scripts in Microsoft SQL Server Management Studio with a particular encoding (you can also use this method to see what type of encoding the file is saved in in the first place).  One other thing to note is that Visual Studio has this same type of Save As… functionality.

    From the Microsoft SQL Server Management Studio (or Visual Studio) File menu choose Save [FILENAME] As…

    image

    Then when the Save File As dialog appears you will see a little black arrow (inverted triangle) as part of the Save button. 

    image

    Clicking the just the inverted triangle portion of the button will give you a menu.

    image

    Choosing the Save with Encoding… option will then present you with an Advanced Save Options dialog.

    image

    image

    Here is where you can specify the encoding to use for the file.  For osql.exe make sure you specify either Western European (Windows) – Codepage 1252 or Unicode – Codepage 1200.  Do not select UTF-8 (codepage 65001) or UTF-7 (codepage 65000) or osql.exe will give errors when trying to parse the file.



  • PureText is Pure Gold!

    PureText from Steve Miller is a very simple and extremely useful utility that makes the Windows-V hot key combination paste non-formatted text instead of formatted text. 

    image

    I use it all over the place and I make sure it is installed and set to run at startup on all of my machines including my wife’s laptop.

    From the PureText website:

    PureText is basically equivalent to opening Notepad, doing a PASTE, followed by a SELECT-ALL, and then a COPY.  The benefit of PureText is performing all these actions with a single Hot-Key and having the result pasted into the current window automatically.

    By the way, guess how I pasted in the text above that I copied from the PureText website?  :)

     

    Technorati Tags: