edit update and canel buttons not firing correct event handlers

Discussion in 'ASP .Net Datagrid Control' started by lauralucas@gmail.com, Jul 12, 2006.

  1. Guest

    Hello
    I use a datagrid with databound columns and a "Edit, Update, Cancel"
    button column. I tried this with column created in design time:
    When I click the Edit button, it correctly fires the Edit event which
    calls my EditGrid
    method and puts me in edit mode. So far, so good. But, when I click the
    update button or the cancel button, it fires the Edit event again
    instead of
    firing Update or Cancel events. In other words, no matter what button I
    click, it always puts me in my EditGrid method. I don't know why.

    I'm using Visual Studio 2003 and asp.net 1.1
    I have seen this question posted several times (I even copy pasted the
    question from an already closed thread. why close threads?) but no
    answers.
    I'm very frustrated and angry.

    In other thread, someone answered this: " Remember that you'll need to
    not only rebind the
    controls on every load, but reassign the event handlers. Too, you
    should
    do this in the Page's Init event handler rather than in the Page_Load
    event handler... "

    HOW??? where is the init event handler???


    well, I already tried several workarounds to this, but the behavior
    gets weirder and weirder.
    What I need to know, is: how to assign an event handler to a control
    hat was created dynamically.

    After the MSDN example FAILED TO WORK, I tried this:
    Now, I'm creating the button columns dynamically. Everything works fine
    except when in edit mode.
    on page_load:
    --------------------------------------------------------------------------------------
    ButtonColumn bcEdit;
    ButtonColumn bcDelete;
    ButtonColumn bcUpdate;
    ButtonColumn bcCancel;

    bcEdit = new ButtonColumn();
    bcEdit.Text = "Edit";
    bcEdit.CommandName = "EditEntry";
    EntryDataGrid.Columns.Add(bcEdit);

    bcDelete = new ButtonColumn();
    bcDelete.Text = "Delete";
    bcDelete.CommandName = "DeleteEntry";
    EntryDataGrid.Columns.Add(bcDelete);

    bcUpdate = new ButtonColumn();
    bcUpdate.Text = "Update";
    bcUpdate.CommandName = "UpdateEntry";
    EntryDataGrid.Columns.Add(bcUpdate);

    bcCancel = new ButtonColumn();
    bcCancel.Text = "Cancel";
    bcCancel.CommandName = "CancelEntry";
    EntryDataGrid.Columns.Add(bcCancel);

    doBindData();

    ------------------------------------------------------------------------------------------


    in doBindData() I'm doing this:

    this.sqlConnection1.Open();
    this.sqlCommand1.CommandText = getSqlQuery();

    this.EntryDataGrid.DataSource =
    sqlCommand1.ExecuteReader(CommandBehavior.CloseConnection);
    this.EntryDataGrid.DataBind();


    ---------------------------------------------------------------------------------------------
    then, I catch OnItemCommand like this:

    private void EntryDataGrid_ItemCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    // stop editing
    EntryDataGrid.EditItemIndex = -1;

    switch (e.CommandName)
    {
    case "Insert":
    this.Label1.Text ="inserting";
    //doBindData();
    break;

    case "UpdateEntry":
    this.Label1.Text ="updating";
    //doBindData();
    break;

    case "CancelEntry":
    this.Label1.Text ="canceling";
    //doBindData();
    break;

    case "EditEntry":
    this.Label1.Text ="editing";

    EntryDataGrid.EditItemIndex = e.Item.ItemIndex;
    doBindData();
    break;
    }
    }

    first: if I dont do a doBindData on page load when Page.IsPostBack is
    true, nothing works, no event gets called at all. So, I have to
    doBindData twice when clicking edit, once in page_load and once in
    EntryDataGrid_ItemCommand.

    notice I have all four buttons already created all the time.

    after edit mode is on, all buttons, when clicked, have e.CommandName ==
    "CancelEntry", including edit button (that is still visible. remember i
    created the 4 buttons dynamically).

    I noticed the code being executed for every button changes: when not in
    edit mode it reads:

    javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl0','') for edit
    javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl1','') for delete
    javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl2','') for update
    javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl3','') for cancel

    but when the row is in edit mode, this changes to:

    javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl29','') for edit
    javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl30','') for delete
    javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl31','') for update
    javascript:__doPostBack('EntryDataGrid$_ctl2$_ctl32','') for cancel

    I don't know if that has anything to do.

    JEEZ, I NEED THIS TO WORK, is there any working example of a datagrid
    with this edit update cancel column?!?!?!?!
    , Jul 12, 2006
    #1
    1. Advertising

  2. Guest

    I have new info on this =)

    just now, I created my four button columns in design mode, and the
    problem occured as usual: when in edit mode, the four buttons triggered
    an incorrect event.

    And then, I placed the four columns AT THE BEGINNING of the table, and
    everithing worked fine. The order of the controls was being altered
    when new controls were created on edit mode (the textboxes etc). So, if
    the button columns are at the beginning, their indexes are not modified
    by the new controls added after them.

    Is this a bug?
    , Jul 13, 2006
    #2
    1. Advertising

  3. Guest

    here is exactly what is happening: (posted originally here:
    http://groups.google.com/group/micr...ce0da4961530?q=update&rnum=6#8fecce0da4961530
    this was posted in 2003)

    ----

    I have looked at all the examples and written a datagrid page with
    update
    features, but the update doesn't work. Basically, it never executes
    the
    datagrid_UpdateCommand(...) method

    When I click on the 'Edit' button on any row, it does indeed call my
    DataGrid_EditCommand(...) method, and it correctly switches to edit
    mode on
    the row. It also replaces the 'Edit' button with 'Update' and 'Cancel'
    buttons.

    But when I'm done editing and click the 'Update' button, it just calls
    the
    DataGrid_EditCommand method again (instead of the
    DataGrid_UpdateCommand
    method). Additionally, if I click on the 'Cancel' button, it calls the
    DataGrid_DeleteCommand instead of the DataGrid_CancelCommand

    It is behaving as if it is stuck in "positional" mode. On initial
    display,
    'Edit' is the first button, and 'Delete' is the 2nd button (there is no
    Update and Cancel yet). When I switch into Edit mode, then Edit is
    replaced
    with Update and Cancel, and now Update is the 1st button (and
    apparently
    mapping to the original 1st button ... Edit); Cancel is now the 2nd
    button,
    and apparently mapping to the original 2nd button (Delete).

    Any ideas?


    ---

    well as for now, I found a workaround: create the four columns in
    design mode, at the beginning of the table, so their positions won't
    change.

    I still can't update the row: I cant access the control with the new
    user input. It's like the control is not there.

    // The value is in the 5th column.
    TableCell theCell = e.Item.Cells[4];

    // The TextBox is the 0th element of the Controls collection. <---
    WRONG, FALSE

    int cant = theCell.Controls.Count; //cant equals zero
    String t = theCell.Text; //t contains old text, not new text typed in
    editing mode.


    any help?
    , Jul 13, 2006
    #3
    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. Boban Dragojlovic

    Dynamic Buttons and Event Handlers, revisited

    Boban Dragojlovic, Jun 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,862
    Boban Dragojlovic
    Jun 24, 2003
  2. Dood
    Replies:
    1
    Views:
    612
  3. Mike Hofer

    Server-side event handlers firing twice

    Mike Hofer, Mar 14, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    447
    Mike Hofer
    Mar 14, 2006
  4. Nessuno

    Buttons and Event Handlers

    Nessuno, Feb 21, 2010, in forum: Java
    Replies:
    7
    Views:
    384
  5. Frank Walsh

    DATAGRID EVENT HANDLERS - NOT FIRING?

    Frank Walsh, May 6, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    109
    Scott M.
    May 7, 2005
Loading...

Share This Page