Dynamic template for a delete button in gridview

Discussion in 'ASP .Net' started by =?Utf-8?B?TmF0ZURhd2c=?=, Dec 8, 2005.

  1. Ok, I’ve noticed a few gridview problems floating around the forum. Everyone
    wants to do a java confirmation box when a user clicks the delete button.
    Fair enough, basic user design rules state that you should always confirm a
    delete action. There is also a consensus that the best way to do this is a
    template item. Many users also want to replace the text link buttons with
    images. This too is a basic design issue and the consensus on the forums is
    to use a command field, it can also be done with a template item but it takes
    a little more coding. Ok, no problem right? So now I have two command field
    items, one for edit and one for select. In addition I have a template item
    for my delete command. They all work just fine while they are in the .aspx
    but now here is my dilemma. I need to be able to do the same thing but in the
    VB code behind page. I managed to get the command field items working like so:

    Dim selectfield As New CommandField()
    selectfield.ButtonType = ButtonType.Image
    selectfield.SelectImageUrl = "~/Images/select.gif"
    selectfield.ShowSelectButton = "True"

    GridView.Columns.Add(selectfield)


    But a template item is proving to be a bit more of a chore than one would
    think. Here is the code that I have so far:


    Sub BuildDelete(ByVal ctl As Control)
    Dim acess As IParserAccessor = ctl
    Dim del As ImageButton = New ImageButton
    del.TemplateControl = Me
    del.ID = "DelBut"
    AddHandler del.DataBinding, AddressOf DataBindDelete
    acess.AddParsedSubObject(del)
    End Sub

    Sub DataBindDelete(ByVal sender As Object, ByVal args As EventArgs)
    Dim button As ImageButton = CType(sender, ImageButton)
    button.OnClientClick = "return confirm('Are you sure you want to delete
    this record?');"
    button.ImageUrl = "~/Images/recycle.gif"
    button.CommandName = "Delete"
    button.AlternateText = "Delete"
    End Sub


    The following code section is a work arround for a previous problem I had,
    and I’m not too sure if this could be part of the problem now or if I’m just
    masking what the real problem is. When the page goes through a post back
    event i.e. column sort, or pageing the delete buttons will disapear but the
    column remains. So, I use this code to remove the old column and re-insert
    delete buttons when the GridView loads.


    Private Sub GridView_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles GridView.Load
    Dim deletefield As New TemplateField()
    deletefield.ItemTemplate = New CompiledBindableTemplateBuilder(AddressOf
    BuildDelete, Nothing)

    If Page.IsPostBack = True Then
    GridView.Columns.RemoveAt(0)
    End If

    GridView.Columns.Insert(0, deletefield)
    End Sub


    I could sure use, and appreciate, any help or suggestions anyone is willing
    to offer.

    Thanks, Nathan Rover
     
    =?Utf-8?B?TmF0ZURhd2c=?=, Dec 8, 2005
    #1
    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?TmF0ZURhd2c=?=

    Dynamic template for a delete button in gridview

    =?Utf-8?B?TmF0ZURhd2c=?=, Dec 10, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    11,254
    Jason
    Aug 15, 2006
  2. Miguel Dias Moura
    Replies:
    1
    Views:
    3,365
    =?Utf-8?B?UGF1bCBaYW5iYWth?=
    Apr 5, 2006
  3. keithb
    Replies:
    3
    Views:
    34,322
    pianocomposer
    Dec 12, 2008
  4. AMT2K5
    Replies:
    0
    Views:
    377
    AMT2K5
    Mar 6, 2007
  5. jijis

    item template and edit delete button

    jijis, Mar 26, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    169
    jijis
    Mar 26, 2005
Loading...

Share This Page