MuNiRs

Just another WordPress.com weblog

Archive for the ‘Uncategorized’ Category

Going Mouseless – Implementing Keyboard Shortcuts in ASP.NET 2.0 Using Javascript

Posted by Irfan Munir on November 14, 2008

Going Mouseless – Implementing Keyboard Shortcuts in ASP.NET 2.0 Using Javascript
Keyboard shortcuts improve productivity by accomplishing tasks more quickly and without much effort. In applications, where the user has to select from a variety of actions to perform, keyboard shortcuts can save on time and effort. If you have used the new YahooMail or Gmail, you will be quiet familiar with these shortcuts. In this article, we will explore how to implement shortcuts and use it in our ASP.NET applications. Please note that this solution is Internet Explorer(IE) specific.
I was recently implementing a solution where users needed the ability to use shortcuts in ASP.NET applications, similar to what we see in Gmail and Yahoo mail. The requirement was as follows:
Problem Scenario
The user visits a screen which contains a series of links (linkbuttons). The user is then required to click on one of the links to navigate to a different page to perform some action (For eg: filling forms). After performing the action, the user returns back to the screen. The process is repeated by the user, quiet a number of times. Now if you visualize, the user has to use the mouse to click one of the links and then shift back to the keyboard to enter data. Then use the mouse again to go back to the main screen. What a pain, especially if someone is involved in data entry operations!!
Solution
The solution was simple. The solution was to introduce Keyboard shortcuts into the application. The user could use a shortcut instead of the mouse, to click the linkbutton and perform an action. So let us say for example, when the user clicks ‘H’, the click event of the Home button gets fired and the user navigates to the Home page.
We will see how to implement this feature in this article. It can be done easily with just a few lines of code. Follow these steps:
1. Create an ASP.NET website. Add a master page to the application. Also create 4 pages that will be using this master page: Default.aspx, Page1.aspx, Page2.aspx and Page3.aspx. If you are unfamiliar with MasterPages, I recommend you to read my article : http://www.dotnetcurry.com/ShowArticle.aspx?ID=80
2. In the default.aspx, drag and drop 3 link buttons to the form. Rename these linkbuttons as Page 1, Page 2 and Page 3 respectively.
The page would look like this:
<asp:Content ID=”Content1″ ContentPlaceHolderID=”ContentPlaceHolder1″ Runat=”Server”>
<div style=”width: 100%; height: 154px;”>
<asp:LinkButton ID=”lbPage1″ runat=”server” PostBackUrl=”Page1.aspx”>Page 1</asp:LinkButton><br />
<br />
<asp:LinkButton ID=”lbPage2″ runat=”server” PostBackUrl=”Page2.aspx”>Page 2</asp:LinkButton><br />
<br />
<asp:LinkButton ID=”lbPage3″ runat=”server” PostBackUrl=”Page3.aspx”>Page 3</asp:LinkButton><br />
<br />
</div>
</asp:Content>
3. In the code behind of default.aspx, add the following code to the Page_Load event
C#
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), “Shortcut”, “document.attachEvent (‘onkeyup’,ShortcutKeys);”, true);
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
ClientScript.RegisterClientScriptBlock(Me.GetType(), “Shortcut”, “document.attachEvent (‘onkeyup’,ShortcutKeys);”, True)
End Sub
The ClientScript.RegisterClientScriptBlock registers the client script block to the top of the rendered page using a type, key, and script literal. A client script is uniquely identified by its key and its type.
4. Add a javascript file to the project called shortcut.js. Add the following code to the javascript file.
function ShortcutKeys()
{
//alert(event.keyCode);
// 1 Pressed For Page1
if (event.keyCode == 49)
{            document.getElementById(‘ctl00_ContentPlaceHolder1_lbPage1′).click();
}
// 2 Pressed For Page2
if (event.keyCode == 50)
{
document.getElementById(‘ctl00_ContentPlaceHolder1_lbPage2′).click();
}
// 3 Pressed For Page3
if (event.keyCode == 51)
{                      document.getElementById(‘ctl00_ContentPlaceHolder1_lbPage3′).click();        }
}
The javascript, depending on the keycode, invokes the click event of the element, in our case the link button. The element is obtained by using document.getElementById( ‘<%=Control.ClientID%>’).
Note: While using Master Pages, you need to refer to the control using the control’s ClientID. I have directly used the ID generated.
5. Now refer to the javascript in the MasterPage.
<head runat=”server”>
<title>Untitled Page</title>
<script type=”text/javascript” language=”javascript” src=”shortcut.js”> </script>
</head>
The keyboard shortcut functionality has been added to the Default.aspx. Before we go ahead and test it, let us make the other pages too ‘mouseless’.
6. Add the keyboard functionality in each of the pages (Page1, 2 and 3). To do so, add a button called btnHome to each of the pages. Set the PostBackUrl property of btnHome to ‘Default.aspx’.
7. In the shortcut.js, add another method to handle the event for the Home button using the keyboard shortcut “H”
function HomeKey()
{
if(event.keyCode == 72)
{        document.getElementById(‘ctl00_ContentPlaceHolder1_btnHome’).click();
}
}
8. Now register the newly added Client script in each of your pages as shown below :
C#
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), “Home”, “document.attachEvent(‘onkeyup’,HomeKey);”, true);
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
ClientScript.RegisterClientScriptBlock(Me.GetType(), “Home“, “document.attachEvent (‘onkeyup’, HomeKey);”, True)
End Sub
9. There you are!!. Now all the pages in your application contain keyboard shortcuts.
Run Default.aspx. Test the pages by pressing 1, 2 or 3 to navigate to Page1 , Page2 or Page 3 respectively. Similarly press “H” on each of these pages to go back to the Default.aspx page.
The usage of keyboard shortcut, in this article has been kept as simple as possible. However once you understand how to use this effectively, the possibilities are endless. Gmail and Yahoomail are two good examples where keyboard shortcuts are being used to improve user interactiveness with the application.
You can download the source code of this article over here.
I hope this article was useful and I thank you for viewing it.

Posted in Uncategorized | 1 Comment »

Posted by Irfan Munir on July 7, 2008

Posted in Uncategorized | Leave a Comment »

Start of SSAS

Posted by Irfan Munir on June 2, 2008

 

Being an implementer of custom data warehouse and query engine ; i just required an erge to learn and develop the SSAS . In post a head i will suppose to post some basic concpets regrading SSAS.

Any help will be appreciable

Posted in Uncategorized | Leave a Comment »

Sql Server Login fro sa enable

Posted by Irfan Munir on May 28, 2008

http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=1292357&SiteID=1

open SQL Server Management Studio Express and log in as a local administrator with a trusted connection…

navigate to the Security->Logins node of the Object Browser and select the “sa” login… rx click->Properties…. in the “Status” tab verify the “Login: Enabled” property has been set..

 

Posted in Uncategorized | 1 Comment »

Avoid casting to improve code performance .. Boxing UnBoxing

Posted by Irfan Munir on May 21, 2008

     
 

Introduction:


Question to Mr. C# Corner:
Hello. Mr. C# Corner. I’m Developer, I want to know why you advice me to use Generic instead of Casting Objects and what is the meaning of a Generic?


Answer: One of the important issues in the code development is the [Coding Quality], because your Coding Quality seperates you from other developers. So let us see what are the basics of the coding quality:

  • Application Performance.
  • Reducing of Casting Object-based data structures.
  • Reuse data processing Algorithms (code).

  • Question to Mr. C# Corner! You say “reduce Casting” of Objects. So what about casting Objects when using Collections (Boxing in general)?
    Answer: Ooh…I would like to see the following example which explains the advantage of using generics and how it is more efficient than using non-generic code and how does this matter in coding quality.
  •  

    Getting Started

    What is a Generic: Generics are a new feature in version 2.0 of the C# language and the common language runtime (CLR).


  • Generics introduce you to the .NET Framework the concept of type parameters, which makes it possible to design classes, and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code.
  • Generics allow you to define type-safe data structures, without committing to actual data types.
  • Let us see Simple Example about Using and benefits of Generics:

    Let us see the difference between a non-generic class and a generic class with your eyes only:

    In Figure 1:

    Tip!! :
    ArrayList is a highly convenient collection class that can be used without modification to store any reference or value type.

    Disadvantage of Casting (Boxing)

     

  • Any reference or value type that is added to an ArrayList is implicitly upcast to the Object. If the items are value types, they must be boxed when added to the list, and unboxed when they are retrieved. Both the casting and the boxing and un-boxing operations degrade the code performance; the effect of boxing and un-boxing can be quite significant in scenarios where you must iterate over large collections of data.
  • The other limitation is lack of compile-time type checking; since an ArrayList casts everything to Object.
  • So if you think about generalizing you collection class, what you will do in C# 1.1?
    You could avoid the dangers of generalized code in the .NET Framework base class library collection classes by writing your own type specific collections. Of course, since such a class is not reusable for more than one data type, you lose the benefits of generalization, and you have to rewrite the class for each type that will be stored.
    In Figure 2:

    Let us see what is happen in C# 2.0 to solve these problems:

    What ArrayList and other similar classes really need is:

     

  • A way for client code to specify, on a per-instance basis, the particular data type that they intend to use. That would eliminate the need for the upcast to T:System.Object and would also make it possible for the compiler to do type checking. In other words, ArrayList needs a type parameter. That is precisely what generics provide. In the generic List<T> collection, in the N:System.Collections.Generic namespace.
  • static void GenericTest()

    {

    // The .NET Framework 2.0 way to create a list

    List<int> list1 = new List<int>();

     

    // No boxing, no casting:

    list1.Add(3);

     

    // Compile-time error:

    // list1.Add(“It is raining in Redmond.”);

    }

    For example using a generic type parameter T you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operations.

    // Declare the generic class

    public class GenericList<T>

    {

    void Add(T input)

    {

     

    }

    }

    class TestGenericList

    {

    private class ExampleClass

    {

     

    }

     

    static void Main()

    {

    // Declare a list of type int

    GenericList<int> list1 = new GenericList<int>();

     

    // Declare a list of type string

    GenericList<string> list2 = new GenericList<string>();

     

    // Declare a list of type ExampleClass

    GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();

    }

    }

    Get Started:

    The boxing and Un-Boxing:

    What is boxing and Un-Boxing in C#?

    Boxing a value type packages it inside an instance of the Object reference type.

    Boxing Example:

    (a) most used Boxing:

    int i = 10;

    object Obj = i;

    (b) It also possible to perform the boxing explicitly as in the following:

    int i = 10;

    // Casting the value type [ int ] to Object Reference type boxing

    object Obj = (object)i;

    How Boxing performed? 

    1- Declare a value-type variable [i] as int

    int i = 10;

    2- then applies the boxing operation on the variable [i]

    object Obj = i;

    3- The result of this statement is creating an object reference Obj on the stack.


  •  

    4- This Object references a value of the type int, on the heap. This value is a copy of the value-type value assigned to the variable [i] which equal 10.

  • What are the Benefits of this concept?To allow the value type to be stored on the garbage collected heap.

     

  • Un-Boxing Example:

    Obj = 10;

    // Unboxing extracts the value type from the object Ref Type

    i = (int)Obj;

    ! Disadvantage of Boxing

    Boxing and un-Boxing are expensive processes because of the following:

     

     

  • when a value type is boxed, an entirely new instance object must be allocated and constructed.
  • The cast required for un-boxing is also expensive computationally.

  • Generic is better than using Object based Solution to reducing Object casting Let us see how?
  • With using Boxing:

     



    readonly
    int m_Size;

    int m_StackPointer = 0;

    object[] m_Items;

     

     

    public void Push(object item)

    {

    if (m_StackPointer >= m_Size)

    throw new StackOverflowException();

    m_Items[m_StackPointer] = item;

    m_StackPointer++;

    }

    With using Generic:

    readonly int m_Size;

    int m_StackPointer = 0;

    T[] m_Items;

    public void Push(T item)

    {

    if (m_StackPointer >= m_Size)

    throw new StackOverflowException();

    m_Items[m_StackPointer] = item;

    m_StackPointer++;

    }

     

     

     

     

         

    Posted in Uncategorized | Comments Off

    links

    Posted by Irfan Munir on December 23, 2007

    specialization is for insects. http://www.agiledata.org/essays/mappingObjects.html
    http://www.hacknot.info/hacknot/action/showEntry?eid=48

    http://www.codeproject.com/KB/audio-video/TalkingClipboard.aspx

    A Day in the Life Software Developer

    http://www.princetonreview.com/cte/profiles/dayInLife.asp?careerID=145

    Posted in Url_Links | 8 Comments »

    Posted by Irfan Munir on May 23, 2007

    Posted in Uncategorized | Leave a Comment »

    MuNiRs

    Posted by Irfan Munir on March 8, 2007

    http://aspadvice.com/blogs/crystal/

    Posted in Uncategorized | Leave a Comment »

    Hello world!

    Posted by Irfan Munir on March 8, 2007

    Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

    Posted in Uncategorized | 1 Comment »