OnItemCommand not firing on postback.

    <asp:DataGrid ID="dgTodaysSpecials" runat="server" AutoGenerateColumns="False" CssClass="dg"
        AlternatingItemStyle-CssClass="dgalternatingitemstyle" HeaderStyle-CssClass="dgheaderstyle"
        GridLines="None" OnItemCommand="TodaysSpecials_ItemCommand">
        <AlternatingItemStyle CssClass="dgalternatingitemstyle"></AlternatingItemStyle>
        <Columns>
            <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:ImageButton runat="server" ImageUrl="~/Images/MB_0000_calendar.png" ID="imgbtnRemoveItem"
                        CommandName="cmdRemoveItem" />
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:BoundColumn DataField="ItemTypeName" HeaderText="Type"></asp:BoundColumn>
            <asp:BoundColumn DataField="ItemName" HeaderText="Name"></asp:BoundColumn>
            <asp:BoundColumn DataField="ItemDescription" HeaderText="Description"></asp:BoundColumn>
            <asp:BoundColumn DataField="ItemPrice" HeaderText="Price" DataFormatString="{0:C2}">
            </asp:BoundColumn>
        </Columns>
        <HeaderStyle CssClass="dgheaderstyle"></HeaderStyle>
    </asp:DataGrid>

Open in new window


    Protected Sub TodaysSpecials_ItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles dgTodaysSpecials.ItemCommand
        If e.CommandName = "cmdRemoveItem" Then
            Dim i As Integer = e.Item.ItemIndex

        End If
    End Sub

Open in new window


Everything looks right?
What am I missing?
Also, how can I assign data [like a SQL Row ID] to the postback when the image button is clicked?

Jason
jsmithrAsked:
Who is Participating?
 
jsmithrAuthor Commented:
So here is what I came up with. I dont think its the DataGrid Item Command, but it seems to be working. Thoughts?

            <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:ImageButton runat="server" ImageUrl="~/Images/MB_0000_calendar.png" ID="imgbtnRemoveItem"
                        CommandName="cmdRemoveItem" OnCommand="TodaysSpecials_ItemCommand" CommandArgument='<%# Eval("SpecialsID") %>' />
                </ItemTemplate>
            </asp:TemplateColumn>

Open in new window


    Protected Sub TodaysSpecials_ItemCommand(ByVal source As Object, ByVal e As CommandEventArgs)
        If e.CommandName = "cmdRemoveItem" Then
            Dim str As String = e.CommandArgument
        End If
    End Sub

Open in new window

0
 
CodeCruiserCommented:
Did you step through the code?

To add row id, use the CommandArguments property

<asp:ImageButton runat="server" ImageUrl="~/Images/MB_0000_calendar.png" ID="imgbtnRemoveItem"
                        CommandName="cmdRemoveItem" CommandArgument='<%# Eval("ID") %>' />
0
 
jsmithrAuthor Commented:
Thanks. I thought command argument was how it would be done.

Yes I stepped through the code. When I click the button the event never fires in code behind.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
CodeCruiserCommented:
Do you have anything in form tag which may prevent event firing such as action property set to something else? Can you show full markup of page(aspx)?
0
 
jsmithrAuthor Commented:
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Main.Master"
    CodeBehind="Default.aspx.vb" Inherits="FSPIDailySpecials._Default" EnableEventValidation="false" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <link type="text/css" href="Styles/south-street/jquery-ui-1.8.16.custom.css" rel="Stylesheet" />
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="Scripts/jquery-ui-1.8.16.custom.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#ContentPlaceHolder1_txbDate").datepicker({
                //showOn: "button",
                //buttonImage: "Images/MB_0000_calendar.png",
                //buttonImageOnly: true,
                //buttonText: "Pick A Date"
                //minDate: 30,
                //maxDate: 30
            });
        });
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="interactivediv">
        <asp:TextBox ID="txbDate" runat="server" AutoPostBack="true"></asp:TextBox>
    </div>
    <asp:DataGrid ID="dgTodaysSpecials" runat="server" AutoGenerateColumns="False" CssClass="dg"
        AlternatingItemStyle-CssClass="dgalternatingitemstyle" HeaderStyle-CssClass="dgheaderstyle"
        GridLines="None" OnItemCommand="TodaysSpecials_ItemCommand">
        <AlternatingItemStyle CssClass="dgalternatingitemstyle"></AlternatingItemStyle>
        <Columns>
            <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:ImageButton runat="server" ImageUrl="~/Images/MB_0000_calendar.png" ID="imgbtnRemoveItem"
                        CommandName="cmdRemoveItem" />
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:BoundColumn DataField="ItemTypeName" HeaderText="Type"></asp:BoundColumn>
            <asp:BoundColumn DataField="ItemName" HeaderText="Name"></asp:BoundColumn>
            <asp:BoundColumn DataField="ItemDescription" HeaderText="Description"></asp:BoundColumn>
            <asp:BoundColumn DataField="ItemPrice" HeaderText="Price" DataFormatString="{0:C2}">
            </asp:BoundColumn>
        </Columns>
        <HeaderStyle CssClass="dgheaderstyle"></HeaderStyle>
    </asp:DataGrid>
</asp:Content>

Open in new window


Imports Net40WebGlobal
Imports System.Data.SqlClient

Public Class _Default
    Inherits System.Web.UI.Page

    Dim objDataBase As New clsDataBase

    Private Sub _Default_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        ' AddHandler TodaysSpecials_ItemCommand(), dgTodaysSpecials.ItemCommand
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dtDate As DateTime

        If IsPostBack = True Then
            dtDate = txbDate.Text
        Else
            txbDate.Text = Date.Today.ToShortDateString
            dtDate = Date.Today
        End If

        dgTodaysSpecials.DataSource = GetTodaysSpecials(dtDate)
        dgTodaysSpecials.DataBind()

    End Sub

    Public Function GetTodaysSpecials(ByVal dtDate As DateTime) As DataTable

        Dim liSQLParameters As New List(Of SqlParameter)
        Dim pDate = New SqlParameter("@Date", System.Data.SqlDbType.Date)
        pDate.Value = dtDate
        liSQLParameters.Add(pDate)
        Dim dtbl As DataTable = objDataBase.getSQL("GetSpecials", liSQLParameters, modAppConfig.FSPIDailySpecialsConnectionString)
        Return dtbl

    End Function

    'Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgTodaysSpecials.ItemCommand
    '    ' e.Item is the row of the table where the button was clicked.
    '    Dim productID As String
    '    productID = e.Item.Cells(1).Text
    '    If (e.CommandName = "Delete") Then
    '        ' Add code here to add the productID item to the cart.
    '    End If
    '    dgTodaysSpecials.DataBind()
    'End Sub

    Protected Sub TodaysSpecials_ItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles dgTodaysSpecials.ItemCommand
        If e.CommandName = "cmdRemoveItem" Then
            Dim i As Integer = e.Item.ItemIndex

        End If
    End Sub

End Class

Open in new window

0
 
CodeCruiserCommented:
And the master page?
0
 
jsmithrAuthor Commented:
<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Main.master.vb" Inherits="FSPIDailySpecials.Main" %>

<%@ Register src="UserControls/ucMainNavigation.ascx" tagname="ucMainNavigation" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>
        <asp:Literal ID="litHeadTitle" runat="server"></asp:Literal></title>
    <link rel="Stylesheet" type="text/css" href="Styles/Global.css" />
    <link rel="stylesheet" type="text/css" href="Styles/Layout.css" />
    <!--[if lt IE 9]><script src="Scripts/html5.js"></script><![endif]-->
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div class="pagewrapper">
        <header class="siteheader">
            <div class="headerleft">
                <asp:ImageButton ID="imgbtnCompanyLogo" runat="server" ImageUrl="~/CompanyLogos/fsfclogo.png"
                    ToolTip="Home" ImageAlign="Left" BorderStyle="None" />
                <span>Welcome,</span><br />
                <asp:Label ID="lblFullName" runat="server">[Jason]</asp:Label>
            </div>
            <div class="appandtitle">
                <h1>
                    <asp:Literal ID="litAppTitle" runat="server"></asp:Literal></h1>
                <h2>
                    <asp:Literal ID="litPageTitle" runat="server"></asp:Literal></h2>
            </div>
        </header>
        <div class="navandcontentwrapper">
            <nav class="sitenav">
                <uc1:ucMainNavigation ID="ucMainNavigation1" runat="server" />
            </nav>
            <section class="sitecontent">
                
                <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                </asp:ContentPlaceHolder>
                <hr class="cleaner" />
            </section>
        </div>
        <footer class="sitefooter">
            <p>
                <span style="color: #666;">App Ver:</span>
                <asp:Label ID="lblAppVersion" runat="server"></asp:Label>
                <span style="color: #666;">| Temp Ver:</span>
                <asp:Label ID="lblTemplateVersion" runat="server"></asp:Label>
            </p>
            <asp:Label ID="lblLastMod" runat="server" CssClass="moddate"></asp:Label>
            <hr class="cleaner" />
        </footer>
    </div>
    </form>
</body>
</html>

Open in new window


Public Class Main
    Inherits System.Web.UI.MasterPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' AuthenticateUser()
        PopulatePageProperties()
    End Sub

    Public Sub PopulatePageProperties()
        lblAppVersion.Text = GetAppVersion()
        lblTemplateVersion.Text = GetTemplateVersion()
        lblLastMod.Text = "<span style=""color: #666;"">Modified:</span> " & GetLastMod()
        litPageTitle.Text = GetPageTitleText()
        litAppTitle.Text = GetAppTitle()
        litHeadTitle.Text = GetPageTitleText()
    End Sub

End Class

Open in new window

0
 
CodeCruiserCommented:
Don't see any problem really. Where did you put the breakpoint when checking it?
0
 
jsmithrAuthor Commented:
    Protected Sub TodaysSpecials_ItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles dgTodaysSpecials.ItemCommand        If e.CommandName = "cmdRemoveItem" Then            Dim i As Integer = e.Item.ItemIndex        End If    End Sub

Open in new window


I put the breakpoint on the If Statement.
Jason
0
 
gopaltaydeCommented:
Instead of ImageButton add  <asp:ButtonField CommandName="Cancel" HeaderText="Add" ShowHeader="True"
                    Text="Button" /> then the corresponding ItemCommand event will get fired.
0
 
jsmithrAuthor Commented:
gopaltayde,

Will I be able to use an Image as the Button?

Jason
0
 
jsmithrAuthor Commented:
Well, like i said. this is what i came up with, and it is still working as desired. Sorry I couldnt accept the other solutions.
0
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.

All Courses

From novice to tech pro — start learning today.