Tuesday, December 29, 2015

retrieve querystring in javascript

function getQueryString(key)
{
    var vars = [], parm, parms, rawQueryString;

    var queryStringI;
    queryStringI = window.location.href.indexOf('?');

    //only extract if we find parameters
    if (queryStringI > -1)
    {
        var rawQueryString = window.location.href.slice(queryStringI + 1);
        parms = rawQueryString.split('&');
        for (var i = 0; i < parms.length; i++)
        {
            parm = parms[i].split('=');
            vars.push(parm[0]);
            vars[parm[0]] = unescape(parm[1]);
        }
    }

    if (typeof (key) === "undefined")
    {
        return vars;
    }
    else
    {
        return vars[key];
    }
}

Find and replace control characters in a string

function findControlChars (vInput)
{
    var pattern = /[\u0000-\u001F]/g;
    var matches = vInput.match(pattern);
    if (matches)
    {
        return true;
    }
    
    return false;
}

function replaceControlChars (vInput, vReplaceChar)
{
    var replaceChar = "";
    if (!(typeof vReplaceChar === "undefined"))
    {
        replaceChar = vReplaceChar;
    }

    var wrkfield = vInput.replace(/[\u0000-\u001F]/g, replaceChar);
   
    return wrkfield;
}

set trim function for strings

       function setTrim()
        {
            if (typeof String.prototype.trim !== 'function')
            {
                String.prototype.trim = function()
                {
                    return this.replace(/^\s+|\s+$/g, '');
                }
            }
        }

Friday, October 23, 2015

Display RawHTML in browser using Javascript

[code]
function htmlEntities(str)
{
    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}
[/code]

Monday, September 28, 2015

Javascript- validate start and ending dates

[code]
        function validateDates()
        {
            var startDate = document.getElementById("txtStartDate");
            var endDate = document.getElementById("txtEndDate");

            if (startDate.value === "" && endDate.value === "")
            {
                alert("Please enter start and end dates.");
                startDate.focus();
                return false;
            }

            if (startDate.value === "")
            {
                alert("Please enter start date.");
                startDate.focus();
                return false;
            }

            if (endDate.value === "")
            {
                alert("Please enter end date.");
                endDate.focus();
                return false;
            }

            //Validate that the fromDate is not greather than toDate
            var oFDate = new Date(startDate.value);
            var oTDate = new Date(endDate.value);
            if (oFDate > oTDate)
            {
                alert("Please ensure that the End Date is greater than or equal to the Start Date.");
                startDate.focus();
                return false;
            }

            return true;
        }

[/code]

Create Excel file in .net

I needed to create an excel file programmatically. These are open source routines, CreateExcel.vb, that handles this work. It utilize to Microsoft DocFormat.OpenXML library under the covers to create the actual file. So you have to add the appropriate dll to your references.

this codeproject has the code for this.
http://www.codeproject.com/Articles/692092/A-free-Export-to-Excel-Csharp-class-using-OpenXML

developers site:
http://mikesknowledgebase.azurewebsites.net/pages/CSharp/ExportToExcel.htm#

Friday, September 25, 2015

Create xml file from excel

..\Visual Studio 2013\Projects\CreateClaimsXMLFromExcel\CreateClaimsXMLFromExcel

Parsing flat files - fixed format and csv files

    Found an open source GenericParser routines.

http://www.codeproject.com/Articles/11698/A-Portable-and-Efficient-Generic-Parser-for-Flat-F

Tuesday, September 8, 2015

Getting first and last day of previous month


[code]
    Private Sub CalculateDefaultDates()
        Dim wrkDate As Date
        Dim baseDate As Date
        baseDate = Now

        'get first of the last month
        wrkDate = baseDate.AddMonths(-1)

        wrkDate = New DateTime(wrkDate.Year, wrkDate.Month, 1)
        Dim sDate As String
        sDate = wrkDate.ToShortDateString()
        txtStartDate.Text = sDate

        'get the last day of the previous month
        wrkDate = baseDate
        wrkDate = New DateTime(wrkDate.Year, wrkDate.Month, 1)

        wrkDate = wrkDate.AddDays(-1)

        sDate = wrkDate.ToShortDateString()
        txtEndDate.Text = sDate


    End Sub
[/code]


another example:

firstDay = new DateTime(Today.Year, Today.Month, 1);
//first day of next month minus one day
lastDay = firstDay.AddMonths(1).AddDays(-1);
//See: http://msdn.microsoft.com/en-us/library/system.datetime.addmonths.aspx

Tuesday, August 18, 2015

Adding Hidden Fields to Gridview

   Often times you want to display data for users to see and allow them to select it.  But you really want to return a key value and not the data you have displayed for them. So the question is how do  you get the hidden key value. The defined way is the use the 'datakeynames' parameter on the gridview and get the hidden values using the code-behind.

   However, what do you do if you wish to access the key value from javascript?  Well, then you have to set up your gridview to store the data but not display it.  I have found a couple of ways to do this:

1)  add a template field with a label in it and set the 'style' to display:none.
[code]
                   <asp:TemplateField >
                        <ItemTemplate>
                            <asp:Label id="lblUK" runat="server" text='<%# Eval("uniquekey")%>' Style="display:none" ></asp:Label>                        
                        </ItemTemplate>
                    </asp:TemplateField>
[/code]

2)  Add a bound field, and set the class or itemstyle-cssClass to a value and set that value in your style sheet with the display:none parm.
[code]
                   <asp:BoundField DataField="uniquekey" HeaderText=""
                        ItemStyle-CssClass="hideGridColumn" >    
                    </asp:BoundField>

    <style type="text/css">
        .hideGridColumn
        {
            display: none;
        }
    </style>



to access from javascript:

       function getSelectedRow(row)
        {
            var ctlGridView = row.parentNode.parentNode;

            var Code = row.cells[0].innerHTML;
            var Type = row.cells[1].innerHTML;
            var uk = row.cells[4].innerText;
         
            row.style.backgroundColor = "red";

            alert("Code:" + Code + " Type:" + Type + " uk:" + uk);
            return true;
        }

in the code behind add onclick value to call this function:

   Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        'set the mouseover values
        For Each row As GridViewRow In GridView1.Rows
            If row.RowType = DataControlRowType.DataRow Then
                row.Attributes("onmouseover") = "this.style.fontWeight='bold';"
                row.Attributes("onmouseout") = "this.style.fontWeight='normal';"
                row.Attributes("onclick") = "getSelectedRow(this);"

            End If
        Next

        MyBase.Render(writer)
    End Sub

[/code]

Now both of the methods have the actual data in the HTML source, so if it is sensitive data, you would not want to do this. But if the data is not sensitive, it is a good way to allow access to hidden values from javascript.