Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. dynamically adding buttonfields to gridview

dynamically adding buttonfields to gridview

Scheduled Pinned Locked Moved ASP.NET
csshelpquestion
3 Posts 1 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    g00fyman
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • G g00fyman

      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

      G Offline
      G Offline
      g00fyman
      wrote on last edited by
      #2

      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

      G 1 Reply Last reply
      0
      • G g00fyman

        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

        G Offline
        G Offline
        g00fyman
        wrote on last edited by
        #3

        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("");
        }
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups