?
Solved

Handling Datagrid Clickevent with Shiftkey

Posted on 2005-04-01
6
Medium Priority
?
227 Views
Last Modified: 2010-08-05
Hi experts,
in my ASP.net page I have datagrid, I would like to find out wehter user has pressed "Shift" or "CTRL" key when clicked on any of the row in Datagrid.

so far I have following in my c# code behind...

private void resultItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    DataGridItem dgi=e.Item;
    if(dgi.ItemType == ListItemType.Item ||
       dgi.ItemType == ListItemType.AlternatingItem ||
       dgi.ItemType == ListItemType.SelectedItem)
    {
      dgi.Attributes.Add("onClick", "selectRows()");
   }

}

now javascript function selectRow gives me keycode when I press any regular keys but not any when I have press "SHIFT" or "CTRL"

following is my Javascript "selectRow()" function
function selectRow(){
   alert(event.keyCode);

}

Thank you so much in Advance...

Nirav
0
Comment
Question by:nil1st
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 2

Expert Comment

by:neil_richards
ID: 13684554
Not sure if this is what you want, but

event.ctrlKey
event.shiftKey

will at least tell you if either of the keys were being pressed.
0
 
LVL 2

Expert Comment

by:neil_richards
ID: 13684632
After a little more playing around I've found that if you use

dgi.Attributes.Add("onkeypress", "selectRow()");

then it reflects keycodes properly.  

For example, hold down 'a' and click the datagrid and it will alert 65, hold down shift+a and it will alert 97.  event.shiftKey will be 'true' only if you're actually holding the shift key..

Hope that's what you're looking for.  
0
 
LVL 1

Author Comment

by:nil1st
ID: 13686720
Neil,
I tried that but it does not work with Shift or Ctrl key itself, may be little more explination would help...

what I am trying to achive here is when a row is selected e.g. lets say row 2 is selected now I want to press Shift Key and Press Left Mouse button on 6th row so grid would change the color for row 2 to 6. In another word it would give effect of it has selected row 2 to 6...

now if user press Ctrl and Click on row 9th then it should leave row 2 to 6 highlighed but now it should highligh row 9 to...


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
neil_richards earned 200 total points
ID: 13687128
Sorry, I thought you were interested in the total keycode.  In the case you're describing, use dgi.Attributes.Add("onClick", "selectRows()"); and check event.ctrlKey and event.shiftKey.  It definitely tells you if they have been pressed.  

Also, perhaps it would be better to call

dgi.Attributes.Add("onClick", "selectRows()");

in the page_load than in resultItemCreated.
0
 
LVL 1

Author Comment

by:nil1st
ID: 13696674
Neil,
That worked, and I didnt even have to put attribute statment in the page_load, it's working great...
however is there a perticuler reason you told me to put this attributes in page_load????

Thank you

Nirav
0
 
LVL 2

Expert Comment

by:neil_richards
ID: 13697292
My reasoning was because it may not be captured by the viewstate (not sure), although thinking about it again, ResultItemCreated is going to be called everytime you databind so it's not really an issue.  You should be able to attach selectRows against the whole datagrid (which is what I did while looking for the answer).  That seemed to work well, but it doesn't really matter.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question