Link to home
Start Free TrialLog in
Avatar of seckel
seckel

asked on

How do I execute a datagrid edit button using an onclick event?

I have a datagrid with an edit button:

<asp:datagrid>
     :
   <asp:ButtonColumn Text="Edit" ButtonType="PushButton" CommandName="Edit" ItemStyle-VerticalAlign="Top" />
     :
</asp:datagrid>

Private Sub dgQuestions_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgQuestions.EditCommand
       '  Fill page controls with data from the database
End Sub
=======================================
I use the following code to change the color of the row that the user currently has their mouse on and to execute a onclick event:

Private Sub dgQuestions_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgQuestions.ItemDataBound

        Select Case (e.Item.ItemType)
            Case ListItemType.Item
                '---------------------------------------------------
                ' Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
                '---------------------------------------------------
                e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#9CF'")
                e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFC'")
            Case ListItemType.AlternatingItem
                '---------------------------------------------------
                ' Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
                '---------------------------------------------------
                e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#9CF'")
                e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFF'")
        End Select
 
        '---------------------------------------------------
        ' Change the Mouse Cursor of a particular Cell (Column) of DataGrid
        ' (Or you may do it for a whole Row of DataGrid :)
        '---------------------------------------------------
        e.Item.Cells(1).Style("cursor") = "hand"

        '---------------------------------------------------
        ' Add the OnClick Alert MessageBox to a particular Cell (Column) of DataGrid
        '---------------------------------------------------
        e.Item.Cells(1).Attributes.Add("onclick", "alert('You click at ID: " & e.Item.Cells(0).Text & "!');")

 End Sub
==========================================

The last statement allows the user to click on the cell and a messagebox will return the ID the cell is associated with.  My question is how can I click on the cell and execute the build-in EDIT button logic?

Avatar of ibost
ibost

in the datagrid specify the handler:

<asp:datagrid OnEditCommand="dgQuestions_EditCommand" ... >
Incidentally here is an excellent tutorial on pretty much anything you want to know about datagrids:
http://aspnet.4guysfromrolla.com/articles/040502-1.aspx

Avatar of seckel

ASKER

I specified the handler as you suggested, but what is the javascript to call the editcommand method?


I tried using:
e.Item.Cells(1).Attributes.Add("onclick", "dgQuestions_EditCommand();")  '<--   Error message is object expected
Unless I'm misunderstanding something you don't need any javascript.

You have this handler defined already:
Private Sub dgQuestions_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgQuestions.EditCommand
       '  Fill page controls with data from the database
End Sub

[I assume, for brevity, you just commented out the actual code in there]

In the datagrid you have the button column defined with the reserved "edit" keyword
CommandName="Edit"

So now in the datagrid you put this [I further assume you have other necessary attributes like ID and runat=server]:
<asp:datagrid OnEditCommand="dgQuestions_EditCommand" ... >

The dgQuestions.EditCommand event will fire when any button in the grid is clicked with a commandname of "Edit".

Avatar of seckel

ASKER

Please allow me to clarify what I need...

I have is a datagrid that has an edit button.  The edit button works fine, it makes a panel visible that contains textboxes for editing the data fields.

I learned that I can highlight each row using the following JavaScript:

Select Case (e.Item.ItemType)
            Case ListItemType.Item
                '---------------------------------------------------
                ' Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
                '---------------------------------------------------
                e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#9CF'")
                e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFC'")
            Case ListItemType.AlternatingItem
                '---------------------------------------------------
                ' Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
                '---------------------------------------------------
                e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#9CF'")
                e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFF'")
        End Select
 
        '---------------------------------------------------
        ' Change the Mouse Cursor of a particular Cell (Column) of DataGrid
        ' (Or you may do it for a whole Row of DataGrid :)
        '---------------------------------------------------
        e.Item.Cells(1).Style("cursor") = "hand"

I also learned that I can execute an onclick event using the following JavaScript:

  e.Item.Cells(1).Attributes.Add("onclick", "alert('You click at ID: " & e.Item.Cells(0).Text & "!');")


What I would like to have happen is that the user moves the mouse over a row, clicks anywhere on the row, and have the EditCommand method execute.  Is this possible?

ASKER CERTIFIED SOLUTION
Avatar of ibost
ibost

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of seckel

ASKER

I could not get this to work the way I need it too.  I am going to abandon trying to get this solution for this question.  ibost will get full points for effort provided.  Thank you.