Solved

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

Posted on 2006-11-27
7
453 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
[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
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:ibost
ID: 18024966
in the datagrid specify the handler:

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

Expert Comment

by:ibost
ID: 18024981
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
ID: 18056806
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 10

Expert Comment

by:ibost
ID: 18056884
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
ID: 18057270
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
ID: 18057432
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
ID: 18070671
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 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