Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Row click event in Datagrid

Posted on 2006-07-19
10
Medium Priority
?
5,284 Views
Last Modified: 2007-11-27
How to implement row click or row select event  for a data grid in ASP.NET using java script?

0
Comment
Question by:pradeepsudharsan
[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
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17138067
Here is how I do it:

Step #1:  Define the function:

function ClickRow(e)
{
  var target = event.srcElement;
  while (target.tagName != "TR")
    target = target.parentNode;
   
  var x;
  var selectedIndex = document.getElementById("hiddenSelectedRow").value;

  if (selectedIndex.length > 0)    
  {
    var previous = target.parentNode.rows[selectedIndex];
   
    previous.className = previous.className.replace(" selected","");
  };
 
  target.className += " selected";
 
  document.getElementById("hiddenSelectedRow").value = target.rowIndex - 1;

}

There is a hidden element 'hiddenSelectedRow', which persists the selected row for the data grid.

Step #2:  

Attach the handler to the onclick event for each <tr> element:

function registerClientSideDataGrid(id)
{
   var table = document.getElementById(id);
  var cols = table.getElementsByTagName("col");
 
  //debugger;
 
  if (table.tBodies.length > 1)
  {
    for (var i = 0; i < table.tBodies[1].rows.length; i++)
    {
      var row = table.tBodies[1].rows[i];
    row.onclick = ClickRow;    
    } //for
  }      
}

Bob
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 17138068
Hi pradeepsudharsan,

In your ItemDataBound event, use:

e.item.attributes.add("onclick","doSomething(this);")

And then code the javascript function doSomething accordingly.

Tim Cottee
0
 
LVL 7

Author Comment

by:pradeepsudharsan
ID: 17138200
Hi TheLearnedOne ,
thanks for your help.
can you tell me how to attach onclick event for each <tr> element and how to get the the <tr> element
from the data grid using java script.

Regards
Pradeep



0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Author Comment

by:pradeepsudharsan
ID: 17138235
Is hiddenSelectedRow created automatically or i have to create it.
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 17138239
If you want to fire SelectedItemIndex on row click then you can do something like following in ItemDataBound event. Additionally, you can change color of background when user hovers over row.

e.Item.Attributes.Add("OnMouseOver","if(this.style.backgroundColor != '#669999')this.style.backgroundColor='LightBlue';this.style.cursor='hand';");
e.Item.Attributes.Add("OnMouseOut","if(this.style.backgroundColor != '#669999')this.style.backgroundColor ='White';");
string strID = ( (LinkButton) e.Item.Cells[5].Controls[0] ).ClientID.Replace("__","$_") ; // Replace 5 with number of Select linkbutton column
e.Item.Attributes.Add("OnClick","javascript:__doPostBack('" + strID  + "');" );

-tushar
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17138265
1) There is the HTML tab to drag/drop a hidden element to the page.

2) To get the entire thing started, add this script block to the end of the page:

<script language="Javascript">
   registerClientSideDataGrid('DataGrid1');
</script>

3) This assumes that the ID for the DataGrid is 'DataGrid1'.

4) The attachment process is here in registerClientSideDataGrid:

    row.onclick = ClickRow;

Bob
0
 
LVL 7

Author Comment

by:pradeepsudharsan
ID: 17143651
hi TheLearnedOne ,

<script language="Javascript">
   registerClientSideDataGrid('DataGrid1');
</script>

I call this code after data binding in the server side using Page.RegisterStartUpScript Function
But nothing happens.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17151076
Does it run?  Can you debug the Javascript?  I can't look over your shoulder and see the exact problem.

Bob
0
 
LVL 7

Author Comment

by:pradeepsudharsan
ID: 17151552
Hi  TheLearnedOne ,

<script language="Javascript">
   registerClientSideDataGrid('DataGrid1');
</script>

I call this code after data binding in the server side using Page.RegisterStartUpScript Function.
But the code inside the  registerClientSideDataGrid('DataGrid1') function does not work.
So i change the logic as follows.


function registerClientSideDataGrid(id)
  {
  debugger;
  alert("test1");
      var table = document.getElementById(id);
      var cols = table.getElementsByTagName("col");
 
            //debugger;
 
            /*if (table.tBodies.length > 1)
            {
                        for (var i = 0; i < table.tBodies[1].rows.length; i++)
                        {
                              var row = table.tBodies[1].rows[i];
                              row.onclick = ClickRow;    
                        } //for
            }  */
            var i=0;
            if(table.rows.length>1)
            {
            alert("test2"+table.rows.length);
               for(i=1;i<table.rows.length;i++)
               {
             
               alert("test3"+i);
                   var row=table.rows[i];
                   row.onclick = ClickRow;  
                   
               }
               
            }  
  }

Now my problem is ,
i could not understand the logic in
function ClickRow(e)
      {  }    function.

also i want to store the clicked row's particular column values in hidden fields.
i.e the exact problem is ,if i click the particular row ,i want to store some column values of that row in some hidden fields in client side.
Can u plz help me?


regards
pradeep

0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 17152333
Pradeep,

1) This is code from a custom web control for a client-side DataGrid that renders its own HTML, so that explains the table.tbodies.length check.

2) Are you getting the alert:   alert("test1");

3) Would comments help?

function ClickRow(e)
{

  // Find the row (<tr>) element, even if the cell was clicked.
  var target = event.srcElement;
  while (target.tagName != "TR")
    target = target.parentNode;

  // Get the hidden element that stores the previous selection
  var selectedIndex = document.getElementById("hiddenSelectedRow").value;

  // Check to see if there was a previous row click
  if (selectedIndex.length > 0)    
  {
    // Get the row element from the table.
    var previous = target.parentNode.rows[selectedIndex];
   
    // De-select the previously highlighted row.
    previous.className = previous.className.replace(" selected","");
  };
 
  // Highlight the newly selected row a valid CSS style name
  target.className += " selected";
 
  // Store the selected row in the hidden element.
  document.getElementById("hiddenSelectedRow").value = target.rowIndex - 1;

}

Bob
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

688 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