We help IT Professionals succeed at work.

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

dba123 asked
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.

Watch Question

Saqib KhanSenior Developer

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


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.  


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

I believe within Datagrid/Repeater you can not use any Server Side Controls:( .
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


thanks.  The DataGrid has been and is definitely being bound on check of IsPostBack
Unlock this solution and get a sample of our free trial.
(No credit card required)

Closed, 500 points refunded.
Community Support Moderator

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.