Monday, March 28, 2011

How to prompt user if page is idle from last 5 minutes

While going through a interview, a interviewer asked me this question which made me to think over it.
The question was:

I have a web page and i want that, if a user not perform any operation (enter text or select a item from dropdownList or any other such) for last 5 minutes on that page then he should be prompted by a message.

i answered the question but he was not looking satisfied. Later when i worked over it and got to know that my answer was correct but its was not optimized.

so below is an optimized solution for such problem:

$(window).bind( 'mousemove mouseclick keydown mousewheel ...more listeners...', idle );
function idle( e )
{
  if ( window.idleTimeout )
  {
    clearTimeout( window.idleTimeout );
  }
  window.idleTimeout = setTimeout( userIdle, 300000 );
}
function userIdle()
{
  //either do stuff here or trigger the "idle" event
  $(window).trigger('idle');
}


in above example we are using jquery to binding all required event listeners with a timer to trigger our function which will prompt user that your are idle on page form last 5 minutes.

For more ASP.NET interview questions click here

Solution By: Rajesh Rolen

Read More

Implementing two interfaces which define same function

Lets say we have two interfaces named "Intr1", "Iintr2" and both the interfaces contains a function named "myFunction()".

now lets say, we have a class named "myClass" and we are implementing both the interfaces in that class. so now because of rule of implementation, we will have to override/define all the function of interface "Intr1" and "Intr2" in class.

now the twist comes to story. as we have a function with same name "myFunction()" in both the interfaces, how will we provide functionality to it in class.

below is an code snippet of how to provide functionality to function in such situation:
interface Intr1
{
    void myFunction();
}

interface Intr2
{
    void myFunciton();
}

class myClass: Intr1,Intr2 
{
    public void Intr1.myFunction()
    {
   // code here functionality for "myFunciton" of interface "Intr1"
    }

    public void Intr2.myFunction()
    {
 // code here functionality for "myFunciton" of interface "Intr2"
    }
}



Solution By:Rajesh Rolen

Read More

Create thumbnail in c#

Many of times we requires to create a thumbnails images or we can say we wants to create a new image from existing one with different height and width. to do so below code will help you:

First of all create an object of image class and pass image url in it:


System.Drawing.Image objImage = System.Drawing.Image.FromFile(strFilename) ;


Now create thumbnail:

System.Drawing.Image.GetThumbnailImageAbort callback ;
callback = null ;
int shtWidth=180;
int shtHeight=130;
System.Drawing.Image objThumbnail = objImage.GetThumbnailImage(shtWidth , shtHeight, callback, System.IntPtr.Zero) ;  


Now save newly created thumbnail image to required format:


objThumbnail.Save("thumbfilename", System.Drawing.Imaging.ImageFormat.Jpeg ) ;



Solution By: Rajesh Rolen

Read More

Tuesday, March 15, 2011

Insert item in listbox at top using javascript

many of times we have requirement to add a item at top of listbox using javascript (add item at index 0).


       var txtl = document.getElementById("<%= txtvalue.ClientID %>");
        var lst = document.getElementById("<%=mylistbox.ClientID %>");
        if(txtl.value != "")
        {
            var opt = document.createElement("Option");
            opt.text =txtl.value;
            opt.value = txtl.value;
            lst.options.add(opt,0);  // here while adding new item to listbox we can pass its index number. 
        }



Solution By: Rajesh Rolen





Read More
Powered By Blogger · Designed By Seo Blogger Templates