datagrid update/cancel command not working

Discussion in 'ASP .Net Web Controls' started by madhu.datla@gmail.com, Sep 15, 2006.

  1. Guest

    Hi,

    I created a page with datagrid where i can edit, update, cancel the
    data. but rather strangely the update and cancel commands are not
    working. Only Edit button works.

    Did any one face the same problem before. Neither
    dgMasterData_CancelCommand() nor dgMasterData_UpdateCommand() is
    getting the control.

    I debugged and found that when I click update command the control goes
    to dgMasterData_EditCommand(), Also e.commandName has value "Edit"
    instead of "Update".

    (I already tried the ultimate solutions: Closing VS.Net and reloading
    the project,deleting the event handlers, and readding etc) ;)

    any pointers? I have a decent amount of time trying to figure out what
    was happening. but no luck..
     
    , Sep 15, 2006
    #1
    1. Advertising

  2. DanG Guest

    I set up my screens like this:

    <asp:TemplateColumn>
    <ItemStyle Wrap="false"></ItemStyle>
    <ItemTemplate>
    <asp:imagebutton runat="server" CommandName="Edit"
    BorderWidth="0" ImageURL="../images/edit.gif" alt="Edit"/>
    <asp:imagebutton runat="server" CommandName="Delete"
    id="btnDelete" BorderWidth="0" ImageURL="../images/delete.gif"
    alt="Delete" />
    </ItemTemplate>

    <EditItemTemplate>
    <asp:imagebutton runat="server" CommandName="Update"
    BorderWidth="0" ImageURL="../images/update.gif" alt="Save"/>
    <asp:imagebutton runat="server" CommandName="Cancel"
    BorderWidth="0" ImageURL="../images/cancel.gif" alt="Cancel"/>
    </EditItemTemplate>

    <FooterTemplate>
    <asp:imagebutton runat="server" CommandName="Insert"
    BorderWidth="0" ImageURL="../images/add.gif" alt="Add"/>
    </FooterTemplate>
    </asp:TemplateColumn>

    And this is the codebehind:

    public void DataGrid_ItemCommand (Object sender,
    DataGridCommandEventArgs e) {
    if (((ImageButton)e.CommandSource).CommandName == "Insert")
    {DataGrid_Insert(sender, e);}
    if (((ImageButton)e.CommandSource).CommandName == "Delete")
    {DataGrid_Delete(sender, e);}
    }

    public void DataGrid_Edit (Object sender, DataGridCommandEventArgs e) {
    DataGrid.EditItemIndex = e.Item.ItemIndex;
    DataGrid.ShowFooter = false;
    }

    public void DataGrid_Cancel (Object sender, DataGridCommandEventArgs e)
    {
    DataGrid.EditItemIndex = -1;
    DataGrid.ShowFooter = true;
    }


    public void DataGrid_Update (Object sender, DataGridCommandEventArgs e)
    {
    DataGrid.EditItemIndex = -1;
    DataGrid.ShowFooter = true;
    }

    public void DataGrid_Insert (Object sender, DataGridCommandEventArgs e)
    {
    DataGrid.EditItemIndex = -1;
    }

    public void DataGrid_Delete (Object sender, DataGridCommandEventArgs e)
    {
    DataGrid.EditItemIndex = -1;
    }

    //Add a "confirm" to the Delete button
    public void DataGrid_ItemCreated(Object sender, DataGridItemEventArgs
    e) {
    ListItemType itemType = e.Item.ItemType;
    if (itemType == ListItemType.Item || itemType ==
    ListItemType.AlternatingItem) {
    ImageButton ib = (ImageButton) e.Item.FindControl("btnDelete");
    if (ib != null) {
    ib.Attributes.Add("onclick", "return confirm('Delete this
    record?');");
    }
    }
    }

    If you're running VB, be sure you haven't lost the "handles" clauses on
    your events. I hate it when that happens!!

    HTH
    Dan
     
    DanG, Sep 15, 2006
    #2
    1. Advertising

  3. Guest

    Thanks Dan and Gaurav,

    events are properly wired and here is the code : let me know if you
    find something wrong here..

    <ASP:DATAGRID ID="dgMasterData" RUNAT="server" SHOWFOOTER="True"
    AUTOGENERATECOLUMNS="False"
    ONUPDATECOMMAND="dgMasterData_UpdateCommand"
    ONCANCELCOMMAND="dgMasterData_CancelCommand">
    <EDITITEMSTYLE CSSCLASS="dgEditItem"></EDITITEMSTYLE>
    <ALTERNATINGITEMSTYLE
    CSSCLASS="dgAlternateItem"></ALTERNATINGITEMSTYLE>
    <ITEMSTYLE CSSCLASS="dgItemStyle"></ITEMSTYLE>
    <HEADERSTYLE CSSCLASS="dgHeader"></HEADERSTYLE>
    <COLUMNS>
    <ASP:TEMPLATECOLUMN VISIBLE="False">
    <ITEMTEMPLATE>
    <ASP:LABEL ID="lblMstrDataID" RUNAT="server" TEXT='<%#
    DataBinder.Eval(Container.DataItem,"mstrDataID")%>'>
    </ASP:LABEL>
    </ITEMTEMPLATE>
    </ASP:TEMPLATECOLUMN>
    <ASP:TEMPLATECOLUMN HEADERTEXT="Description">
    <ITEMTEMPLATE>
    <%# DataBinder.Eval(Container.DataItem,"mstrDataValue")%>
    </ITEMTEMPLATE>
    <FOOTERTEMPLATE>
    <ASP:TEXTBOX ID="tbNewMasterDataValue" RUNAT="server"
    CSSCLASS="tboxClass"></ASP:TEXTBOX>
    </FOOTERTEMPLATE>
    <EDITITEMTEMPLATE>
    <ASP:TEXTBOX ID="tbMasterDataValue" RUNAT="server"
    CSSCLASS="tboxClass" TEXT='<%#
    DataBinder.Eval(Container.DataItem,"mstrDataValue")%>'>
    </ASP:TEXTBOX>
    </EDITITEMTEMPLATE>
    </ASP:TEMPLATECOLUMN>
    <ASP:EDITCOMMANDCOLUMN BUTTONTYPE="PushButton" UPDATETEXT="Update"
    CANCELTEXT="Cancel" EDITTEXT="Edit"></ASP:EDITCOMMANDCOLUMN>
    <ASP:TEMPLATECOLUMN>
    <FOOTERTEMPLATE>
    <ASP:BUTTON ID="btnSaveNewMstrData" RUNAT="server" TEXT="Add New"
    ONMOUSEOVER="btn_FocusOn(this)"
    ONMOUSEOUT="btn_FocusOff(this)" CSSCLASS="mfbuttonCellText"
    COMMANDNAME="Add New"></ASP:BUTTON>
    </FOOTERTEMPLATE>
    </ASP:TEMPLATECOLUMN>
    </COLUMNS>
    </ASP:DATAGRID>



    private void InitializeComponent()
    {
    this.ddMasterData.SelectedIndexChanged += new
    System.EventHandler(this.ddMasterData_SelectedIndexChanged);
    this.dgMasterData.CancelCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMasterData_CancelCommand);
    this.dgMasterData.EditCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMasterData_EditCommand);
    this.dgMasterData.UpdateCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMasterData_UpdateCommand);
    this.dgMasterData.ItemDataBound += new
    System.Web.UI.WebControls.DataGridItemEventHandler(this.dgMasterData_ItemDataBound);
    this.Load += new System.EventHandler(this.Page_Load);

    }

    private void dgMasterData_EditCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    try
    {
    dgMasterData.EditItemIndex = e.Item.ItemIndex;
    }
    catch(Exception ex)
    {
    Response.Write(ex.Message.ToString());
    }

    }

    public void dgMasterData_CancelCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    dgMasterData.SelectedIndex = -1;
    }

    public void dgMasterData_UpdateCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    .........
    }



    DanG wrote:
    > I set up my screens like this:
    >
    > <asp:TemplateColumn>
    > <ItemStyle Wrap="false"></ItemStyle>
    > <ItemTemplate>
    > <asp:imagebutton runat="server" CommandName="Edit"
    > BorderWidth="0" ImageURL="../images/edit.gif" alt="Edit"/>
    > <asp:imagebutton runat="server" CommandName="Delete"
    > id="btnDelete" BorderWidth="0" ImageURL="../images/delete.gif"
    > alt="Delete" />
    > </ItemTemplate>
    >
    > <EditItemTemplate>
    > <asp:imagebutton runat="server" CommandName="Update"
    > BorderWidth="0" ImageURL="../images/update.gif" alt="Save"/>
    > <asp:imagebutton runat="server" CommandName="Cancel"
    > BorderWidth="0" ImageURL="../images/cancel.gif" alt="Cancel"/>
    > </EditItemTemplate>
    >
    > <FooterTemplate>
    > <asp:imagebutton runat="server" CommandName="Insert"
    > BorderWidth="0" ImageURL="../images/add.gif" alt="Add"/>
    > </FooterTemplate>
    > </asp:TemplateColumn>
    >
    > And this is the codebehind:
    >
    > public void DataGrid_ItemCommand (Object sender,
    > DataGridCommandEventArgs e) {
    > if (((ImageButton)e.CommandSource).CommandName == "Insert")
    > {DataGrid_Insert(sender, e);}
    > if (((ImageButton)e.CommandSource).CommandName == "Delete")
    > {DataGrid_Delete(sender, e);}
    > }
    >
    > public void DataGrid_Edit (Object sender, DataGridCommandEventArgs e) {
    > DataGrid.EditItemIndex = e.Item.ItemIndex;
    > DataGrid.ShowFooter = false;
    > }
    >
    > public void DataGrid_Cancel (Object sender, DataGridCommandEventArgs e)
    > {
    > DataGrid.EditItemIndex = -1;
    > DataGrid.ShowFooter = true;
    > }
    >
    >
    > public void DataGrid_Update (Object sender, DataGridCommandEventArgs e)
    > {
    > DataGrid.EditItemIndex = -1;
    > DataGrid.ShowFooter = true;
    > }
    >
    > public void DataGrid_Insert (Object sender, DataGridCommandEventArgs e)
    > {
    > DataGrid.EditItemIndex = -1;
    > }
    >
    > public void DataGrid_Delete (Object sender, DataGridCommandEventArgs e)
    > {
    > DataGrid.EditItemIndex = -1;
    > }
    >
    > //Add a "confirm" to the Delete button
    > public void DataGrid_ItemCreated(Object sender, DataGridItemEventArgs
    > e) {
    > ListItemType itemType = e.Item.ItemType;
    > if (itemType == ListItemType.Item || itemType ==
    > ListItemType.AlternatingItem) {
    > ImageButton ib = (ImageButton) e.Item.FindControl("btnDelete");
    > if (ib != null) {
    > ib.Attributes.Add("onclick", "return confirm('Delete this
    > record?');");
    > }
    > }
    > }
    >
    > If you're running VB, be sure you haven't lost the "handles" clauses on
    > your events. I hate it when that happens!!
    >
    > HTH
    > Dan
     
    , Sep 15, 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. =?Utf-8?B?U3RldmUgS2FsbGFs?=

    CssClass on DataGrid Edit, Update & Cancel buttons

    =?Utf-8?B?U3RldmUgS2FsbGFs?=, Jul 6, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    732
    =?Utf-8?B?U3RldmUgS2FsbGFs?=
    Jul 6, 2004
  2. Lucas Tam
    Replies:
    1
    Views:
    475
    Lucas Tam
    Jul 30, 2004
  3. Replies:
    2
    Views:
    565
  4. Replies:
    0
    Views:
    1,144
  5. Julia B
    Replies:
    2
    Views:
    332
    Julia B
    Apr 11, 2006
Loading...

Share This Page