Tuesday, November 15, 2016

Compare the difference between two List

List<int> lst1 = new List<int>();
            lst1.Add(100);
            lst1.Add(110);
            lst1.Add(120);
            lst1.Add(130);

List<int> lst2 = new List<int>();          
            lst2.Add(110);
            lst2.Add(120);
            lst2.Add(130);
            lst2.Add(140);

////IEnumerable<int> difference = lst2.Except(lst1);
            ////if (difference.Any())
            ////{
            ////    var lstFinal = difference.ToList();
            ////}


List<int> difference = lst2.Except(lst1).ToList();

Above list (difference) will contain only 140.

Thursday, September 15, 2011

Getting Previous Page's control that caused postback in current page

First checking normal postback or crosspage postback:
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
       // Do Something}

Previous Page Aspx:
  <asp:LinkButton ID="LnkID" runat="server" PostBackUrl="" Text="Link to New Page" />

We must use "PostBackUrl"  property here

Current Page CS:
page_load
{
    if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
    {
         Control LnkCtrl = GetPostBackControl(this.Page);
         string ctrlID = LnkCtr.ID;
    }
}

private Control GetPostBackControl(Page page)
        {
            Control control = null;

            string ctrlname = page.Request.Params.Get("__EVENTTARGET");
            if (ctrlname != null && ctrlname != string.Empty)
            {
                control = page.PreviousPage.FindControl(ctrlname);
            }
            else
            {
                foreach (string ctl in page.Request.Form)
                {
                    Control c = page.FindControl(ctl);
                    if (c is System.Web.UI.WebControls.Button)
                    {
                        control = c;
                        break;
                    }
                }
            }
            return control;
        }

GridView Validation on edit update action

Use below JavaScript function in .aspx

function ValidateGrid(e)
        {               
            var cnt = 0;
            var grid = document.getElementById('<%= gvActionItems.ClientID %>');
            for(i=1;i<grid.rows.length;i++)
                {               
                    if(grid.rows[i].cells[4].children[0].id == e.id)
                    {                       
                        if(grid.rows[i].cells[0].children[0].value == "")
                        {
                            alert("Please add Action Item");
                            cnt = 1;
                            return false;
                        }
                        if(grid.rows[i].cells[3].children[0].value == "")
                        {
                            alert("Please enter a Target Date");
                            cnt = 1;
                            return false;
                        }                     
                    }                 
                }
            if(cnt > 0)
                return false;
            else
                return true;
     }

In .cs file under GridView's RowDataBound Event put this code
protected void gvActionItems_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[4].HasControls())
                {
                    LinkButton lnkbtnDelete = ((LinkButton)e.Row.Cells[4].Controls[0]);
                    lnkbtnDelete.Attributes.Add("id", "LnEdtUpdt");
                    lnkbtnDelete.Attributes.Add("onclick", "return ValidateGrid(this);");
                }
            }
        }

Tuesday, August 9, 2011

JavaScript Validation for TextBox (mm/dd/yyyy)

Call this function on Button OnClientClick
function ValidateDate()
    {
        var txtSDate = document.getElementById('<%=txtSDate.ClientID%>');
        var txtEndDate = document.getElementById('<%=txtEndDate.ClientID%>');
       
        if(txtSDate.value != '')
        {
            if (isDate(txtSDate.value)==false){
            txtSDate.focus();           
            return false;
            }
        }
       
        if(txtEndDate.value != '')
        {
            if (isDate(txtEndDate.value)==false){
            txtEndDate.focus();           
            return false;
            }
        }     
     }

Now Attach this functions in a JS file and attach to aspx:
--------------------------------Under JS File----------------------------------------------------------
function isInteger(s){
    var i;
    for (i = 0; i < s.length; i++){  
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){  
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
        if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){

    var dtCh= "/";
    var minYear=1900;
    var maxYear=2100;

    var daysInMonth = DaysArray(12)
    var pos1=dtStr.indexOf(dtCh)
    var pos2=dtStr.indexOf(dtCh,pos1+1)
    var strMonth=dtStr.substring(0,pos1)
    var strDay=dtStr.substring(pos1+1,pos2)
    var strYear=dtStr.substring(pos2+1)
    strYr=strYear
    if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
    if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
    }
    month=parseInt(strMonth)
    day=parseInt(strDay)
    year=parseInt(strYr)
    if (pos1==-1 || pos2==-1){
        alert("The date format should be : mm/dd/yyyy")
        return false
    }
    if (strMonth.length<1 || month<1 || month>12){
        alert("Please enter a valid month")
        return false
    }
    if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
        alert("Please enter a valid day")
        return false
    }
    if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
        alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
        return false
    }
    if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
        alert("Please enter a valid date")
        return false
    }
return true
}
-----------------------------------------------------------------------------------------------------------




Monday, July 18, 2011

Making Image Background to transparent

Use Photofiltre for making image background to tranparent.
  1. load the image
  2. go to Image > Transparent Color...
  3. choose the color (white), set the desired tolerance with the slider, click OK
  4. go to File > Save as... and save the image as .PNG

Wednesday, June 1, 2011

Wrapping Text Line in a Label

Add this Style to Label control:
Style="font-family: Verdana; word-wrap: break-word; white-space: pre-wrap;
                            white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap;"


Thursday, January 6, 2011

A simple JavaScript Email Validation

This function will validate the e-mail address
function ValidateEmail(emailid)
   {
        
        var RegExEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
        if (!RegExEmail.test(emailid.value))
        {
            //alert("The E-mail address entered is not valid!");
            return true;
        }
        else
        {            
            return false;
        }
       
   }