This is a migrated thread and some comments may be shown as answers.

Change GridButtonColumn delete image from code behind

4 Answers 247 Views
Button
This is a migrated thread and some comments may be shown as answers.
Iedoc
Top achievements
Rank 1
Iedoc asked on 20 May 2013, 08:34 PM
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

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 21 May 2013, 10:56 AM
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.
0
Iedoc
Top achievements
Rank 1
answered on 21 May 2013, 02:21 PM
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?
0
Princy
Top achievements
Rank 2
answered on 22 May 2013, 04:05 AM
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.
0
Iedoc
Top achievements
Rank 1
answered on 22 May 2013, 08:33 PM
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;"
                            + "    }"
                            + "}";

Tags
Button
Asked by
Iedoc
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Iedoc
Top achievements
Rank 1
Share this question
or