Dynamic EditCommandColumn events

Discussion in 'ASP .Net Datagrid Control' started by Bruce E. Bonsall, May 9, 2005.

  1. I have a page that creates multiple datagrids at runtime. I have no problem setting the object's properties and assigning the event handlers. The 'Edit' linkbutton sucessfully puts the appropriate record in edit mode, but the CancelCommand and UpdateCommand do not fire their assigned handlers.

    The 'Cancel' button ignores its assigned handler, and the 'Update' button fires the 'EditCommand' event.

    I've seen this problem posted in numerous places, but without any explanations.

    Can someone please provide enlightenment?

    Thanks in advance!

    -B

    From http://www.developmentnow.com/g/12_0_0_0_0_0/dotnet-framework-aspnet-datagridcontrol.ht

    Posted via DevelopmentNow.com Group
    http://www.developmentnow.com
     
    Bruce E. Bonsall, May 9, 2005
    #1
    1. Advertising

  2. Bruce E. Bonsall

    Elton W Guest

    Hi Bruce,

    What is the datagrid's viewstate, true or false? If it's
    false, the datagrid can't work properly. If it's true, we
    need to look for other reason to cause the problem.

    HTH

    Elton Wang


    >-----Original Message-----
    >I have a page that creates multiple datagrids at

    runtime. I have no problem setting the object's
    properties and assigning the event handlers. The 'Edit'
    linkbutton sucessfully puts the appropriate record in edit
    mode, but the CancelCommand and UpdateCommand do not fire
    their assigned handlers.
    >
    >The 'Cancel' button ignores its assigned handler, and

    the 'Update' button fires the 'EditCommand' event.
    >
    >I've seen this problem posted in numerous places, but

    without any explanations.
    >
    >Can someone please provide enlightenment?
    >
    >Thanks in advance!
    >
    >-B-
    >
    >From http://www.developmentnow.com/g/12_0_0_0_0_0/dotnet-

    framework-aspnet-datagridcontrol.htm
    >
    >Posted via DevelopmentNow.com Groups
    >http://www.developmentnow.com
    >.
    >
     
    Elton W, May 9, 2005
    #2
    1. Advertising

  3. EnableViewState=true

    I have added another button column to each grid and have assigned a
    deleteCommand handler that is working properly.

    To suumarize:

    All controls are being created at runtime, therefore all data is bound
    with each pageload.

    The EditCommand and DeleteCommand event handler are working properly.

    The 'Update' link button triggers the EditCommand handler.

    The 'Cancel' command does cancel out of edit mode, but not by using the
    handler I've assigned it.

    Thanks in advance for your help!



    *** Sent via Developersdex http://www.developersdex.com ***
     
    Bruce Bonsall, May 9, 2005
    #3
  4. Here's the relevant code. Note that errorHandling and non-relevant
    function calls have been removed for (hopefully) easier reading

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    paintPage()
    End Sub

    Private Sub paintPage()
    ' removed code gets the number of grids that need to be created

    ' get rid of existing controls
    tblMain.Rows.Clear()

    drGet = cmdGet.ExecuteReader
    While drGet.Read
    ' create grid
    Dim dg As New DataGrid

    ' set datagrid attributes, including event handlers
    assignDGProperties(dg, drGet("staffHeading"))

    ' fill w/ data
    dg.DataSource = fillGrid(drGet("staffHeading"))
    dg.DataBind()

    ' create new table row
    Dim tr As New TableRow
    Dim tc As New TableCell
    Dim lbl As New Label
    lbl.Text = "<b><u>" & drGet("staffHeading") & "</u></b><br>"

    'add controls to dynamic table
    tc.Controls.Add(lbl)
    tc.Controls.Add(dg)
    tr.Cells.Add(tc)
    tblMain.Rows.Add(tr)
    End While
    End Sub

    Private Sub assignDGProperties(ByVal dg As DataGrid, ByVal heading As
    String)
    AddHandler dg.ItemCreated, AddressOf dg_ItemCreated

    Dim ecc As New EditCommandColumn
    With ecc
    .ButtonType = ButtonColumnType.LinkButton
    .CancelText = "Cancel"
    .EditText = "Edit"
    .UpdateText = "Update"
    End With
    dg.Columns.AddAt(0, ecc)

    AddHandler dg.EditCommand, AddressOf dg_Edit
    AddHandler dg.CancelCommand, AddressOf dg_Cancel
    AddHandler dg.UpdateCommand, AddressOf dg_Update

    Dim d As New ButtonColumn
    With d
    .CommandName = "Delete"
    .Text = "Del"
    End With

    dg.Columns.AddAt(1, d)
    AddHandler dg.DeleteCommand, AddressOf dg_Delete

    ' code removed from here adds bound and template columns

    ' format grid
    With dg
    .Font.Size = FontUnit.Point(8)
    .ID = "dg" & heading
    .Width = Unit.Percentage(100)
    .AllowPaging = False
    .HeaderStyle.Font.Size = New FontUnit(New Unit(8, UnitType.Point))
    .HeaderStyle.Font.Name = "Arial"
    .HeaderStyle.Font.Bold = True
    .HeaderStyle.ForeColor.FromArgb(11, 61, 145)
    .HeaderStyle.BackColor.FromArgb(238, 198, 129)
    .AutoGenerateColumns = False
    .EnableViewState = True
    End With

    End Sub

    Private Function fillGrid(ByVal heading As String)
    ' build datasource and pass back to datagrid
    End Function

    Private Sub dg_ItemCreated(ByVal sender As System.Object, ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs)
    ' code here sizes columns and controls if listitemtype=edititem
    End Sub

    Sub dg_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
    ' called when edit link in clicked in column(0)
    CType(sender, DataGrid).EditItemIndex = e.Item.ItemIndex
    CType(sender, DataGrid).DataSource = fillGrid(Right(CType(sender,
    DataGrid).ID, Len(CType(sender, DataGrid).ID) - 2))
    CType(sender, DataGrid).DataBind()
    End Sub

    Sub dg_Cancel(ByVal sender As Object, ByVal e As
    DataGridCommandEventArgs)
    ' called when cancel link in clicked in column(0)
    CType(sender, DataGrid).EditItemIndex = -1
    CType(sender, DataGrid).DataSource = fillGrid(Right(CType(sender,
    DataGrid).ID, Len(CType(sender, DataGrid).ID) - 2))
    CType(sender, DataGrid).DataBind()
    End Sub

    Sub dg_Delete(ByVal sender As Object, ByVal e As
    DataGridCommandEventArgs)
    ' called when cancel link in clicked in column(0)
    CType(sender, DataGrid).EditItemIndex = -1
    CType(sender, DataGrid).DataSource = fillGrid(Right(CType(sender,
    DataGrid).ID, Len(CType(sender, DataGrid).ID) - 2))
    CType(sender, DataGrid).DataBind()
    End Sub

    Sub dg_Update(ByVal sender As Object, ByVal e As
    DataGridCommandEventArgs)
    'code here posts changes to database
    End Sub


    *** Sent via Developersdex http://www.developersdex.com ***
     
    Bruce Bonsall, May 9, 2005
    #4
  5. After days of grueling search and about a dozen posts to various
    websites, I found a solution to this issue.

    Because the controls had to be recreated on every pageload, the
    editIndex of the grid in question got killed.

    To correct:

    On the initial pageload, I added the following code:
    ViewState("grid_edit_index") = = -1

    In the editCommand event handler, I added:
    ViewState("grid_edit_index") = e.Item.ItemIndex

    In the Cancel, Update, and Delete event handlers, I added
    ViewState("grid_edit_index") = -1

    In the sub where I set datagrid properties, I added:
    dg.EditItemIndex = ViewState("grid_edit_index")

    Badabing, badaboom.

    Credit goes to
    http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net-datagrid/2582/5th-Re
    -Post-with-NO-RESPONSE-FROM-MS


    *** Sent via Developersdex http://www.developersdex.com ***
     
    Bruce Bonsall, May 10, 2005
    #5
  6. Bruce E. Bonsall

    cj Guest

    That's wierd.. I think I did what you said, but now my EDIT button is doing
    the UPDATE feature (but CANCEL and DELETE work)...!

    Will keep looking at it...

    cj

    "Bruce Bonsall" <> wrote in message
    news:...
    >
    > After days of grueling search and about a dozen posts to various
    > websites, I found a solution to this issue.
    >
    > Because the controls had to be recreated on every pageload, the
    > editIndex of the grid in question got killed.
    >
    > To correct:
    >
    > On the initial pageload, I added the following code:
    > ViewState("grid_edit_index") = = -1
    >
    > In the editCommand event handler, I added:
    > ViewState("grid_edit_index") = e.Item.ItemIndex
    >
    > In the Cancel, Update, and Delete event handlers, I added
    > ViewState("grid_edit_index") = -1
    >
    > In the sub where I set datagrid properties, I added:
    > dg.EditItemIndex = ViewState("grid_edit_index")
    >
    > Badabing, badaboom.
    >
    > Credit goes to
    > http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net-datagrid/2582/5th-Re
    > -Post-with-NO-RESPONSE-FROM-MS
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
     
    cj, May 11, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick
    Replies:
    1
    Views:
    473
    Richard Ryerson
    Feb 2, 2004
  2. Richard Ryerson
    Replies:
    0
    Views:
    2,630
    Richard Ryerson
    Jan 28, 2004
  3. Jaime
    Replies:
    3
    Views:
    497
    Jaime
    May 14, 2005
  4. Rick

    DataGrid EditCommandColumn not firing events properly.

    Rick, Jan 28, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    297
    Richard Ryerson
    Feb 2, 2004
  5. Richard Ryerson

    DataGrid EditCommandColumn not firing events properly.

    Richard Ryerson, Jan 28, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    307
    Richard Ryerson
    Jan 28, 2004
Loading...

Share This Page