Test Menu

  • Commentary
  • Deals
  • Coupons

Mar 15, 2012

Reset Web Form - ASP.NET VB

Short simple procedure to clear out the textboxes and reset the dropdowns on a page.  Pass in the Page.Controls for example.



    Private Sub ResetForm(ByVal cc As ControlCollection)
        Try
            For Each c As Control In cc
                If TypeOf c Is TextBox Then
                    CType(c, TextBox).Text = String.Empty
                ElseIf TypeOf c Is DropDownList Then
                    CType(c, DropDownList).SelectedIndex = 0
                Else
                    If c.HasControls Then
                        ResetForm(c.Controls)
                    End If
                End If
            Next
        Catch ex As Exception
               ' log an error
        End Try
    End Sub

Feb 14, 2012

Simple jQuery Print Friendly Page


The following script can be used to take the content from a section of a page, in this case a div, and put the content in a new simplified window.  This is one way to exclude banners and graphics or deal with page layouts that don't  print well.  In the first section, on document ready, the link (or whatever element you like) is selected by the id "linkPrintFriendly" and assigned a function to execute when the element is clicked.  I've also made the cursor a pointer when hovering over the element.  The "printfriendly" function basically grabs html from an element on the page (with the class "theelementtoprint") and puts it in a new window that is opened.  In this example, I'm also hiding a table that I don't want to show in the new window.

I found a very useful example showing the code at http://jsfiddle.net

  <script type="text/javascript">
        $(document).ready(function () {
            $("#lnkPrintFriendly").click(printfriendly);
            $("#lnkPrintFriendly").css("cursor", "pointer");
        });

        function printfriendly() {

            //open a new window or tab
            var w = window.open();

            //include only the content of the center div or rightnocenter div
            var html = $(".theelementtoprint").html();

            //provide a title
            w.document.title = 'Page Title';

            //put the html into the new page
            $(w.document.body).html(html);

            //hide the table
            $(w.document).find("#tblFunctions").hide();

        }
    </script>

Jan 26, 2012

An Overloaded ASMX

Recently, I wanted to create an overloaded function to use in a web service.  In one of the methods, an ID will be accepted as a parameter to query records matching the ID.  In the other method, no ID is passed in and all records are returned.  Sure, I could have have given my methods different names, but what would I learn from that?  This was for an asmx.

Two areas need to be modified to have overloaded web methods.

  1. At the beginning of the service, change the web service binding from this:<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> to this: <WebServiceBinding(ConformsTo:=WsiProfiles.None)>
  2. Give the web method  a message name like this: <WebMethod(MessageName:="WithID")

Dec 14, 2011

Deploying Crystal Reports for Visual Studio 2010

If you're writing an application that uses Crystal Reports for Visual Studio 2010, the steps for deploying the application and getting the report to work are not obvious.  The following steps helped me with deployment.
  1. Download and install the Crystal Reports for Visual Studio 2010 Run-time on the server where application will be deployed.  

  2. Download is herehttp://www.businessobjects.com/jump/xi/crvs2010/us2_default.asp 
  1. Copy C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ASP.NETClientFiles
  2. Paste to \Inetpub\wwwroot\aspnet_client\system_web\2_0_50727 (or the asp_client folder of the default website).
These steps seem very simple, but for a while I was running the report and getting a blank window.  I noticed a javascript error "bobj is undefined".  That was because I hadn't done steps 2 and 3.  Some searches led me an answer here: http://bytes.com/topic/net/answers/823323-crystal-report-error-bobj-undefined

Crystal Reports - Text or Text Number With Three Decimal Places


This is a Crystal Report formula field.  The requirement is to display the string 'No Data' when the value is null and if it isn't null show the number with three decimal places.To display 'No Data' both parts of the if must be converted to text.

If NumericText ({SupplierStatistic.pHMin}) Then 
    ToText(ToNumber({SupplierStatistic.pHMin}),3)
Else
    'No Data'

Nov 29, 2011

Averages with Linq

Here are two different ways to get the average using Linq.  These examples use a generic list of a class that has a Temperature  property:


Dim avgTemperature = (From c In lst Where c.Temperature IsNot Nothing Select c.Temperature).Average()


Dim avgTemperature = From t In lst Where t.Temperature IsNot Nothing Aggregate Temperature In lst Into Average(t.Temperature)

Nov 28, 2011

Concatenating Static Text to an Eval Expression

This wasn't obvious to me even though it seemed basic.  Full credit goes to the original post on stackoverflow.

I was using a datalist and wanted to concatenate the text "Collection Type: " before the Eval expression.  The only solution I could find was to use a format string.  I favored this solution instead of using the code-behind.

<asp:Label ID="CollectionTypeLabel" runat="server" text='<%#DataBinder.Eval(Container.DataItem, "CollectionType", "Collection Type: {0}")%>' />