• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1323
  • Last Modified:

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

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.

1 Solution
Saqib KhanSenior DeveloperCommented:
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
dba123Author Commented:
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.  
dba123Author Commented:
In fact you must use ItemCommand, it makes no sense NOT to.  If you don't you get errors.
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Saqib KhanSenior DeveloperCommented:
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
dba123Author Commented:
thanks.  The DataGrid has been and is definitely being bound on check of IsPostBack
dba123Author Commented:
I ended up using a LinkButton instead.  You can't do what I'm trying to do with an ImageButton for whatever reason.
Closed, 500 points refunded.
Community Support Moderator
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now