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="T op" />
:
</asp:datagrid>
Private Sub dgQuestions_EditCommand(By Val source As Object, ByVal e As System.Web.UI.WebControls. DataGridCo mmandEvent Args) 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. DataGridIt emEventArg s) 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("onm ouseover", "this.style.backgroundColo r='#9CF'")
e.Item.Attributes.Add("onm ouseout", "this.style.backgroundColo r='#FFC'")
Case ListItemType.AlternatingIt em
'------------------------- ---------- ---------- ------
' Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
'------------------------- ---------- ---------- ------
e.Item.Attributes.Add("onm ouseover", "this.style.backgroundColo r='#9CF'")
e.Item.Attributes.Add("onm ouseout", "this.style.backgroundColo r='#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("cur sor") = "hand"
'------------------------- ---------- ---------- ------
' Add the OnClick Alert MessageBox to a particular Cell (Column) of DataGrid
'------------------------- ---------- ---------- ------
e.Item.Cells(1).Attributes .Add("oncl ick", "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?
<asp:datagrid>
:
<asp:ButtonColumn Text="Edit" ButtonType="PushButton" CommandName="Edit" ItemStyle-VerticalAlign="T
:
</asp:datagrid>
Private Sub dgQuestions_EditCommand(By
' 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(
Select Case (e.Item.ItemType)
Case ListItemType.Item
'-------------------------
' Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
'-------------------------
e.Item.Attributes.Add("onm
e.Item.Attributes.Add("onm
Case ListItemType.AlternatingIt
'-------------------------
' Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
'-------------------------
e.Item.Attributes.Add("onm
e.Item.Attributes.Add("onm
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("cur
'-------------------------
' Add the OnClick Alert MessageBox to a particular Cell (Column) of DataGrid
'-------------------------
e.Item.Cells(1).Attributes
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?
Incidentally here is an excellent tutorial on pretty much anything you want to know about datagrids:
http://aspnet.4guysfromrolla.com/articles/040502-1.aspx
http://aspnet.4guysfromrolla.com/articles/040502-1.aspx
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("oncl ick", "dgQuestions_EditCommand() ;") '<-- Error message is object expected
I tried using:
e.Item.Cells(1).Attributes
Unless I'm misunderstanding something you don't need any javascript.
You have this handler defined already:
Private Sub dgQuestions_EditCommand(By Val source As Object, ByVal e As System.Web.UI.WebControls. DataGridCo mmandEvent Args) 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 _EditComma nd" ... >
The dgQuestions.EditCommand event will fire when any button in the grid is clicked with a commandname of "Edit".
You have this handler defined already:
Private Sub dgQuestions_EditCommand(By
' 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
The dgQuestions.EditCommand event will fire when any button in the grid is clicked with a commandname of "Edit".
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("onm ouseover", "this.style.backgroundColo r='#9CF'")
e.Item.Attributes.Add("onm ouseout", "this.style.backgroundColo r='#FFC'")
Case ListItemType.AlternatingIt em
'------------------------- ---------- ---------- ------
' Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
'------------------------- ---------- ---------- ------
e.Item.Attributes.Add("onm ouseover", "this.style.backgroundColo r='#9CF'")
e.Item.Attributes.Add("onm ouseout", "this.style.backgroundColo r='#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("cur sor") = "hand"
I also learned that I can execute an onclick event using the following JavaScript:
e.Item.Cells(1).Attributes .Add("oncl ick", "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?
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("onm
e.Item.Attributes.Add("onm
Case ListItemType.AlternatingIt
'-------------------------
' Add the OnMouseOver and OnMouseOut method to the Row of DataGrid
'-------------------------
e.Item.Attributes.Add("onm
e.Item.Attributes.Add("onm
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("cur
I also learned that I can execute an onclick event using the following JavaScript:
e.Item.Cells(1).Attributes
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
<asp:datagrid OnEditCommand="dgQuestions