Solved

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

Posted on 2006-11-27
7
448 Views
Last Modified: 2008-02-01
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?

0
Comment
Question by:seckel
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:ibost
Comment Utility
in the datagrid specify the handler:

<asp:datagrid OnEditCommand="dgQuestions_EditCommand" ... >
0
 
LVL 10

Expert Comment

by:ibost
Comment Utility
Incidentally here is an excellent tutorial on pretty much anything you want to know about datagrids:
http://aspnet.4guysfromrolla.com/articles/040502-1.aspx

0
 

Author Comment

by:seckel
Comment Utility
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
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 10

Expert Comment

by:ibost
Comment Utility
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".

0
 

Author Comment

by:seckel
Comment Utility
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?

0
 
LVL 10

Accepted Solution

by:
ibost earned 500 total points
Comment Utility
ok here's something you can try - a little different that the direction you're going right now.

Only caveat is I think you'll need to change the PushButton to a LinkButton:
<asp:ButtonColumn Text="Edit" ButtonType="LinkButton" ...

Add this code to you your ItemDataBound (in addition to whatever else you need in there):

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

   '  Make sure it's not the pager, header, or footer
   Dim itemType As ListItemType = e.Item.ItemType
   If ((itemType = ListItemType.Pager) Or _
      (itemType = ListItemType.Header) Or _
      (itemType = ListItemType.Footer)) Then
      Return
   Else
      Dim button As LinkButton = _
         CType(e.Item.Cells(0).Controls(0), LinkButton)
      e.Item.Attributes("onclick") = _
         Page.GetPostBackClientHyperlink(button, "")
   End If
End Sub

This assumed the button was in the first cell, but you can replace Cells(0) with whatever the location is of the buttoncolumn.

Basically, this is a tricky way of forcing the "Edit" button to be clicked when the user clicks anywhere in the row.  It gets a reference to the actual LinkButton and then sets an onclick handler for the entire datagrid row.  
0
 

Author Comment

by:seckel
Comment Utility
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.  
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now