dynamically adding buttonfields to gridview
-
Hi all, Happy New Year :) Can someone please help me with the following scenario. I do not like how the default setting is to have the select and delete button together in a grid view row so I am extending gridview (for other reasons) and am dynamically adding my own fields, select at 0, delete at columns.count - 1. Whenever I add OnClientClick code to the delete button the button is just posting back and not firing a delete command.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);ButtonField select = new ButtonField(); select.CommandName = "Select"; select.ButtonType = ButtonType.Image; select.ImageUrl = Page.ClientScript.GetWebResourceUrl(GetType(), EDIT\_ICON); Columns.Insert(0, select); ButtonField delete = new ButtonField(); delete.CommandName = "Delete"; delete.ButtonType = ButtonType.Image; delete.ImageUrl = Page.ClientScript.GetWebResourceUrl(GetType(), DELETE\_ICON); Columns.Add(delete); } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (Page.IsPostBack == false) { foreach (GridViewRow row in Rows) { if (row.RowType == DataControlRowType.DataRow) { ImageButton select = row.Cells\[0\].Controls\[0\] as ImageButton; select.ToolTip = "Modify"; select.AlternateText = "Modify"; select.CssClass = "select-button"; ImageButton delete = row.Cells\[row.Cells.Count - 1\].Controls\[0\] as ImageButton; // the following line causes button to not fire a delete command //delete.OnClientClick = "return confirm('Are you sure you want to delete this item?\\\\n\\\\nClick OK to delete this item or Cancel to keep it.');"; delete.ToolTip = "Delete"; delete.AlternateText = "Delete"; delete.CssClass = "delete-button"; } } } }
Thanks, Maurice
-
Hi all, Happy New Year :) Can someone please help me with the following scenario. I do not like how the default setting is to have the select and delete button together in a grid view row so I am extending gridview (for other reasons) and am dynamically adding my own fields, select at 0, delete at columns.count - 1. Whenever I add OnClientClick code to the delete button the button is just posting back and not firing a delete command.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);ButtonField select = new ButtonField(); select.CommandName = "Select"; select.ButtonType = ButtonType.Image; select.ImageUrl = Page.ClientScript.GetWebResourceUrl(GetType(), EDIT\_ICON); Columns.Insert(0, select); ButtonField delete = new ButtonField(); delete.CommandName = "Delete"; delete.ButtonType = ButtonType.Image; delete.ImageUrl = Page.ClientScript.GetWebResourceUrl(GetType(), DELETE\_ICON); Columns.Add(delete); } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (Page.IsPostBack == false) { foreach (GridViewRow row in Rows) { if (row.RowType == DataControlRowType.DataRow) { ImageButton select = row.Cells\[0\].Controls\[0\] as ImageButton; select.ToolTip = "Modify"; select.AlternateText = "Modify"; select.CssClass = "select-button"; ImageButton delete = row.Cells\[row.Cells.Count - 1\].Controls\[0\] as ImageButton; // the following line causes button to not fire a delete command //delete.OnClientClick = "return confirm('Are you sure you want to delete this item?\\\\n\\\\nClick OK to delete this item or Cancel to keep it.');"; delete.ToolTip = "Delete"; delete.AlternateText = "Delete"; delete.CssClass = "delete-button"; } } } }
Thanks, Maurice
OK, I have worked out why, should have found it earlier. The OnClientClick is being output like this when I add my own client click code:
onclick="return confirm('Are you sure you want to delete this item?\n\nClick OK to delete this item or Cancel to keep it.');javascript:__doPostBack('OrganicGridView1','Delete$0')"
So my new question is, what event do I need to hook to so I can output something like this. I could do it client side but I would rather not.
onclick="if(confirm('Are you sure you want to delete this item?\n\nClick OK to delete this item or Cancel to keep it.')) { javascript:__doPostBack('OrganicGridView1','Delete$0') }"
Thanks Maurice
-
OK, I have worked out why, should have found it earlier. The OnClientClick is being output like this when I add my own client click code:
onclick="return confirm('Are you sure you want to delete this item?\n\nClick OK to delete this item or Cancel to keep it.');javascript:__doPostBack('OrganicGridView1','Delete$0')"
So my new question is, what event do I need to hook to so I can output something like this. I could do it client side but I would rather not.
onclick="if(confirm('Are you sure you want to delete this item?\n\nClick OK to delete this item or Cancel to keep it.')) { javascript:__doPostBack('OrganicGridView1','Delete$0') }"
Thanks Maurice
ok, I have tried everything I can think of and cannot do it server side. I have tried ButtonFields, TemplateFields, CommandFields and all offer different access to the button but it still renders it's own javascript that I cannot change. So ... I have resolved it with client side code let the gridview to render it out. If anyone requires the code, see below. If anyone has a server side solution please let me know. Thanks, Maurice Client Code:
/* function to handle delete confirmation placement on gridview delete buttons */
function setupDeleteConfirmation() {
var imgs = document.getElementsByTagName('input');
for (var i in imgs) {
if (imgs[i].title == 'Delete' && imgs[i].className == 'delete-button') {
imgs[i].oldClick = imgs[i].onclick;
imgs[i].onclick = function() {
var answer = confirm('Are you sure you want to delete this item?\n\nClick OK to delete this item or Cancel to keep it.');
if (answer == true) { this.oldClick; }
return answer;
};
}
}
}Server Code:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);if (DesignMode == false) { if (Page.ClientScript.IsClientScriptIncludeRegistered("\_GridViewClicks") == false) { Page.ClientScript.RegisterClientScriptInclude("\_GridViewClicks", Page.ClientScript.GetWebResourceUrl(GetType(), GRID\_JS)); } } // other stuff here }
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);writer.WriteLine(" // <!\[CDATA\["); writer.WriteLine(" setupDeleteConfirmation(); // \]\]>"); writer.WriteLine(""); }