Solved

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

Posted on 2006-11-27
7
451 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
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
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.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 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