e.CommandName not being evaluated when ImageButton is clicked in my DataGrid

Posted on 2007-10-03
Last Modified: 2010-08-05
I'm trying to figure out why this existing delete code isn't being called.

1        Private Sub dgPhones_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgPhones.ItemCommand
2            Dim allowDelete As Integer = 100
4            Select Case e.CommandName
6                Case "Edit"
7                    dpPhoneType.ClearSelection()
8                    dpPrimary.ClearSelection()
9                    EditData(CInt(dgPhones.DataKeys(e.Item.ItemIndex)))
10                   pnlEditPhone.Visible = True
11                   btnAddPhone.Visible = False
12                   BindLeadPhones()
13                   pnlPhones.Height = New System.Web.UI.WebControls.Unit(180)
14               Case "Delete"
15                   allowDelete = DeletePhone(CInt(dgPhones.DataKeys(e.Item.ItemIndex)))
16                   If allowDelete = 100 Then
17                       BindLeadPhones()
18                   Else
19                       Response.Write("<script language=Javascript>")
20                       Response.Write("alert('You must make another phone number the Primary phone before you delete.')")
21                       Response.Write("</script>")
22                   End If
23           End Select
25       End Sub
The problem is, previously this was working fine but the change I made was to use an actual ImageButton in an ItemTemplate rather than a stupid ButtonColumn or whatever that they had in this DatGrid Previously because first of it's easier to work with and second, I needed to fire off an OnClick event as well.

Anyway, here's my new ImageButton for the delete button defined in my DataGrid:
1    <asp:TemplateColumn HeaderText="Primary">
2          <ItemStyle Wrap="False" HorizontalAlign="Left" CssClass="datagridcell" />
3                <ItemTemplate>
4                      <nobr>
5                      <asp:ImageButton ID="btnDelete" ImageUrl="/Images/icons/redx.gif" CommandName="Delete" runat="server" />
6                      </nobr>
7                </ItemTemplate>
8          </asp:TemplateColumn
So when I step through and debug, it never gets back into the Select Case e.CommandName for my Delete button and not sure why.  I do have a JavaScript confirmation coming up asking "Are you sure you want to delete" when the user clicks the button and if I click OK in that JavaScript confirmation, the page refreshes but it's like it never returns to evaluate the e.CommandName in order to carry out the delete.  It just goes back to PageLoad again at the beginning after I click OK in my JavaScript Confirmation box instead of continueing onto the Select Case in my dgPhones_ItemCommand method....why is that?

Any help or ideas would be greatly appreciated.

Question by:dba123
    LVL 23

    Expert Comment

    did you try

    Private Sub dgPhones_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgPhones.Command

    instead of ItemCommand
    LVL 1

    Author Comment

    They've always had ItemCommand.  And that Edit portion works fine and also did the Delete before when I didn't have an ImageBugtton and JavaScript and just had the ButtonCommand in the Grid.  So I don't think it's that.

    I think it has everything to do with my JavaScript confirmation that comes up because when I confirm and click OK, the page does a postback and it's like it starts over and never continues on at the same place in code to continue evaluating the e.CommandName.  
    LVL 1

    Author Comment

    In fact you must use ItemCommand, it makes no sense NOT to.  If you don't you get errors.
    LVL 23

    Expert Comment

    I believe within Datagrid/Repeater you can not use any Server Side Controls:( .
    LVL 2

    Expert Comment

    Hi adil we can use server controls inseide Datagrid

    Dba please make sure that you are binding the datagrid when the page si not posted back

    if not ispostback then

    'function to bind datagrid

    end if
    LVL 1

    Author Comment

    thanks.  The DataGrid has been and is definitely being bound on check of IsPostBack
    LVL 1

    Accepted Solution

    I ended up using a LinkButton instead.  You can't do what I'm trying to do with an ImageButton for whatever reason.
    LVL 1

    Expert Comment

    Closed, 500 points refunded.
    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now