Skip to main content

[Migrated content. Thread originally posted on 08 February 2012]

I have a web page with gridview. I want to be able to click on any line in the grid, and do some processing on the selected line. How do I do it? I've created a method (shown below), and "onmouseover" etc work, but how do I call a method, or do anything, when a line is clicked on.

Any advice gratefully recieved. Many thanks in advance.

method-id grdSearch_RowCreated public.
***************************************
local-storage section.
procedure division.

invoke grdSearch::Attributes::Add("title", "Click to select"). *> WORKS
invoke grdSearch::Attributes::Add"onmouseover", "this.style.cursor='pointer';this.style.color='white';"). *>WORKS
invoke grdSearch::Attributes::Add("onmouseout", "this.style.color='blue';this.style.background='white';"). *>WORKS

invoke grdSearch::Attributes::Add("onclick", "invoke self::grdSearch_SelectedIndexChanged()"). *> DOES NOTHING

goback.
end method.

[Migrated content. Thread originally posted on 08 February 2012]

I have a web page with gridview. I want to be able to click on any line in the grid, and do some processing on the selected line. How do I do it? I've created a method (shown below), and "onmouseover" etc work, but how do I call a method, or do anything, when a line is clicked on.

Any advice gratefully recieved. Many thanks in advance.

method-id grdSearch_RowCreated public.
***************************************
local-storage section.
procedure division.

invoke grdSearch::Attributes::Add("title", "Click to select"). *> WORKS
invoke grdSearch::Attributes::Add"onmouseover", "this.style.cursor='pointer';this.style.color='white';"). *>WORKS
invoke grdSearch::Attributes::Add("onmouseout", "this.style.color='blue';this.style.background='white';"). *>WORKS

invoke grdSearch::Attributes::Add("onclick", "invoke self::grdSearch_SelectedIndexChanged()"). *> DOES NOTHING

goback.
end method.
Bring up the web page in Design View and click on the GridView control to select it.
Look in the Properties window for the properties associated with the GridView.

If the Properties window is not displayed select View-->Properties Window from the IDE menu.
Click on the lightning bolt (Events) icon in the Properties window.

This will display a list of events that can be triggered on this control.
Look for the SelectedIndexChanged event.

If you double-click on this event a method called [gridname]_SelectedIndexChanged will automatically be added to your code-behind source and will place you in the editor in this method.

Instead of taking the default name by double-clicking you can also type your on method name in the textbox to the right of the event name and press Enter.

This automatically adds all the necessary wiring so that when the selection changes this method will be called.

Add your code to this method.
You can use the property SelectedIndex to determine which row was selected:


       method-id GridView1_SelectedIndexChanged protected.
       procedure division using by value sender as object e as type System.EventArgs.
           if self::GridView1::SelectedIndex = 1
              continue
           end-if
             
       end method.


[Migrated content. Thread originally posted on 08 February 2012]

I have a web page with gridview. I want to be able to click on any line in the grid, and do some processing on the selected line. How do I do it? I've created a method (shown below), and "onmouseover" etc work, but how do I call a method, or do anything, when a line is clicked on.

Any advice gratefully recieved. Many thanks in advance.

method-id grdSearch_RowCreated public.
***************************************
local-storage section.
procedure division.

invoke grdSearch::Attributes::Add("title", "Click to select"). *> WORKS
invoke grdSearch::Attributes::Add"onmouseover", "this.style.cursor='pointer';this.style.color='white';"). *>WORKS
invoke grdSearch::Attributes::Add("onmouseout", "this.style.color='blue';this.style.background='white';"). *>WORKS

invoke grdSearch::Attributes::Add("onclick", "invoke self::grdSearch_SelectedIndexChanged()"). *> DOES NOTHING

goback.
end method.
Thank you Chris. That works if I have AutoGenerateSelectButton="True", and I click on the Select button.

But I am trying to hide the select button with AutoGenerateSelectButton="False" and then make it possible to click anywhere on a line - it looks so much better. I've been advised I need to use the [grdName]_RowCreated method, which seems strange. Logically I would have expected a RowSelected method or similar.

Is there a way to have the click working, to know which line is selected without showing the "Select" button or link?

[Migrated content. Thread originally posted on 08 February 2012]

I have a web page with gridview. I want to be able to click on any line in the grid, and do some processing on the selected line. How do I do it? I've created a method (shown below), and "onmouseover" etc work, but how do I call a method, or do anything, when a line is clicked on.

Any advice gratefully recieved. Many thanks in advance.

method-id grdSearch_RowCreated public.
***************************************
local-storage section.
procedure division.

invoke grdSearch::Attributes::Add("title", "Click to select"). *> WORKS
invoke grdSearch::Attributes::Add"onmouseover", "this.style.cursor='pointer';this.style.color='white';"). *>WORKS
invoke grdSearch::Attributes::Add("onmouseout", "this.style.color='blue';this.style.background='white';"). *>WORKS

invoke grdSearch::Attributes::Add("onclick", "invoke self::grdSearch_SelectedIndexChanged()"). *> DOES NOTHING

goback.
end method.
OK, you are using a GridView, I was thinking ListView, sorry.

I got it to work by adding the following code to the RowCreated event.
I also had to add the following to the Page header in order to avoid a Postback error:

EnableEventValidation="false"

When the row is clicked it will send the Select command to the GridView which will cause the SelectedIndexChanged event to fire. Make sure that you gave the SelectIndexChanged event wired up to a method as I explained in the prior post and then add the following code:



       method-id GridView1_RowCreated protected.
       procedure division using by value sender as object e as type System.Web.UI.WebControls.GridViewRowEventArgs.
       
           if (e::Row::RowType = type DataControlRowType::DataRow)
               set e::Row::Attributes::Item("onmouseover") to "this.style.cursor='pointer';this.style.textDecoration='underline';"
               set e::Row::Attributes::Item("onmouseout") to "this.style.textDecoration='none';"
               set e::Row::Attributes::Item("onclick") to self::ClientScript::GetPostBackClientHyperlink(self::GridView1, "Select$" & e::Row::RowIndex, false)
            end-if   
   
   
       end method.

[Migrated content. Thread originally posted on 08 February 2012]

I have a web page with gridview. I want to be able to click on any line in the grid, and do some processing on the selected line. How do I do it? I've created a method (shown below), and "onmouseover" etc work, but how do I call a method, or do anything, when a line is clicked on.

Any advice gratefully recieved. Many thanks in advance.

method-id grdSearch_RowCreated public.
***************************************
local-storage section.
procedure division.

invoke grdSearch::Attributes::Add("title", "Click to select"). *> WORKS
invoke grdSearch::Attributes::Add"onmouseover", "this.style.cursor='pointer';this.style.color='white';"). *>WORKS
invoke grdSearch::Attributes::Add("onmouseout", "this.style.color='blue';this.style.background='white';"). *>WORKS

invoke grdSearch::Attributes::Add("onclick", "invoke self::grdSearch_SelectedIndexChanged()"). *> DOES NOTHING

goback.
end method.
Thank you so much. That works a treat.
Phil