?
Solved

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

Posted on 2006-11-27
7
Medium Priority
?
454 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 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