Change GridButtonColumn delete image from code behind

5 posts, 0 answers
  1. Iedoc
    Iedoc avatar
    6 posts
    Member since:
    May 2013

    Posted 20 May 2013 Link to this post

    I have a bunch of grids and i need to be able to delete rows, then undelete them if needed. Right now i have a red x for the delete column, which is in a GridButtonColumn, but i would like to change the icon to something that represents an undelete action. I'm able to undelete the row by going through each row in the onitemdatabound function and changing the commandname for the GridButtonColumn to undelete, but i can't seem to change the image. this is what i have for the onitemdatabound right now:
     
    protected void ManageUsersGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        GridDataItem item = e.Item as GridDataItem;
         if (item != null)
         {
            LinkButton button = item["DeleteColumn"].Controls[0] as LinkButton;
            button.CommandName = "Undelete";
        }
    }

    I check to see if the row in in a deleted state in there, but i removed it for simplicity
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 21 May 2013 Link to this post

    Hi,

    I guess you want to change the Image of a GridButtonColumn whose CommandName is Delete. Please take a look into the following code snippet.

    C#:
    Protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            GridDataItem item = e.Item as GridDataItem;
            if (item != null)
            {
                ImageButton button = item["DeleteColumn"].Controls[0] as ImageButton;
                button.ImageUrl = "~/Images/Undel.jpg";
                button.CommandName = "Undelete";
            }
        }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Iedoc
    Iedoc avatar
    6 posts
    Member since:
    May 2013

    Posted 21 May 2013 Link to this post

    Thank you, thats exactly what i was looking for.

    So now i have that working, but i just realized that the confirm dialogue for the delete action will pop up when i click the new icon. is there anyway to display a different confirm dialogue? the confirm dialogue as it is now is defined in the aspx file for the delete column. can i override that in the codebehind?
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 21 May 2013 Link to this post

    Hi,

    Try the following code to add confirm;
    C#:
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
      {
          if (e.CommandName == "Delete")
          {
              GridDataItem item = e.Item as GridDataItem;
              if (item != null)
              {
                  ImageButton button = item["DeleteColumn"].Controls[0] as ImageButton;
                  button.ImageUrl = "../Images/Undel.jpg";
                  button.CommandName = "Undelete";
                 ((GridButtonColumn)grid1.MasterTableView.GetColumnSafe("DeleteColumn")).ConfirmText = "your confirm dialogue text."
              }
          }
      }

    Thanks,
    Princy.
  6. Iedoc
    Iedoc avatar
    6 posts
    Member since:
    May 2013

    Posted 22 May 2013 Link to this post

    Thanks for the reply, but that doesn't work for what i need. That will set the entire columns ConfirmText, not per row. After playing around and researching, i came up with a solution to the problem, which stops the postback when you click on the button, shows a confirm message, and does the postback if you say ok or whatever (that was a big problem i was having was the button would not wait for the confirm dialogue to finish before posting back)

                                     ImageButton delButton = dataItem["DeleteColumn"].Controls[0] as ImageButton;
                            delButton.ImageUrl = "~/Images/Undel.png";
                            delButton.CommandName = "Undelete";
                            delButton.ToolTip = "Undelete";
                            delButton.OnClientClick = "javascript: radconfirm('Restore this item?', undelete, 250, 180, null, 'Restore'); return false;"
                                + "function undelete(arg) {"
                                + "    if(arg) {"
                                + "        var masterTable = $find('" + dataItem.OwnerTableView.ClientID + "');"
                                + "        masterTable.fireCommand('Undelete','" + dataItem.ItemIndex + "');"
                                + "    }"
                                + "    else {"
                                + "        return false;"
                                + "    }"
                                + "}";

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017