Search code examples
asp.netvb.netbuttonaspxgridview

Button in GridView Footer not firing


My button in my GridViews footer is not firing when clicked. No errors, no code gets executed, all it does is a refresh and everything on the page remains exactly as it were before the button click. Debugging doesn't even execute.

  1. Declared code behind as per normal

    Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="RetailCompetition.aspx.vb" Inherits="Company.Applications.Production.UI.RetailCompetition"

Event

Protected Sub dgvRetailCompetitions_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

    Try
        If e.CommandName = "Insert" AndAlso Page.IsValid Then
            Dim _refYear As Integer = (ddlYears.SelectedValue.ToString())
            Dim _rewardDescription As String = (CType(dgvRetailCompetitions.FooterRow.FindControl("txtRewardsDescription"), TextBox)).Text
            Dim _categoryACondition As Integer = (CType(dgvRetailCompetitions.FooterRow.FindControl("txtCatAConditions"), TextBox)).Text
            Dim _categoryBCondition As Integer = (CType(dgvRetailCompetitions.FooterRow.FindControl("txtCatBConditions"), TextBox)).Text
            Dim _categoryCCondition As Integer = (CType(dgvRetailCompetitions.FooterRow.FindControl("txtCatCConditions"), TextBox)).Text

            Company.Applications.Production.BusinessAccess.RetailCompetitionBusinessAccess.InsertNewRetailCompetitionRecord(_refYear,
                                                                                                                                   _rewardDescription,
                                                                                                                                   _categoryACondition,
                                                                                                                                   _categoryBCondition, _categoryCCondition)
        End If
    Catch ex As Exception
        InformationBox.ShowErrorMessage("Failed")
    End Try

End Sub
  1. And the button

                            <asp:TemplateField HeaderText="Ref. Year" SortExpression="ReferenceYear" ItemStyle-HorizontalAlign="Center">
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("RefYear") %>' />
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("RefYear") %>'></asp:Label>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:Button ID="btnInsertRecord" runat="server" CommandName="Insert" Text="Insert Record" ValidationGroup="Insert" CssClass="buttonBlue"/>
                                </FooterTemplate>
                            </asp:TemplateField>
    

Can anybody see anything wrong?

ASPX File UPDATE

<%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="RetailCompetition.aspx.vb"
    Inherits="Company.Applications.ProductionEngine.UI.RetailCompetition" %>

<%@ Register Src="~/GenericControls/UC_GridViewSort.ascx" TagName="SortExtender"
    TagPrefix="uc" %>
<%@ Register Src="~/GenericControls/UC_InformationMessageBox.ascx" TagName="InformationBox"
    TagPrefix="uc" %>
<%@ Register Src="~/GenericControls/UC_GridFilter.ascx" TagName="GridFilter" TagPrefix="uc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:UpdatePanel ID="MainContentUpdatePanel" runat="server">
        <ContentTemplate>
            <h2>
                <asp:Label ID="lblTitle" Text="Retail" runat="server" meta:resourcekey="lblTitle" />
            </h2>
            <br />
            <asp:Panel ID="panelRetailCompetitions" runat="server" Width="100%">
                <div class="panels" style="padding-top: 0px; padding-bottom: 0px">
                    <h4 style="font-weight: bold">
                        <asp:Label ID="lblSectionTitle" runat="server" Text="Competition View" meta:resourceKey="lblSectionTitle" />
                    </h4>
                    <div class="panelContents" style="text-align: left;">
                        <asp:Table ID="tblRetailCompetitions" runat="server" Width="100%" CellSpacing="10">
                            <asp:TableRow>
                                <asp:TableCell>
                                    Select Year
                                    <asp:DropDownList ID="ddlYears" runat="server" EnableViewState="true">
                                        <asp:ListItem Enabled="true" Value="-"></asp:ListItem>
                                        <asp:ListItem Text="2018" Value="2018"></asp:ListItem>
                                        <asp:ListItem Text="2019" Value="2019"></asp:ListItem>
                                        <asp:ListItem Text="2020" Value="2020"></asp:ListItem>
                                    </asp:DropDownList>
                                </asp:TableCell>
                            </asp:TableRow>
                            <asp:TableRow>
                                <asp:TableCell>
                                    <asp:Button ID="btnCopyToSelectedYear" runat="server" Text="Copy To Selected Year"
                                        Width="145px" CssClass="buttonBlue" />
                                </asp:TableCell>
                            </asp:TableRow>
                            <asp:TableRow>
                                <asp:TableCell>
                                    <uc:GridFilter ID="GridFilter" runat="server" EnableViewState="true" />
                                </asp:TableCell>
                            </asp:TableRow>
                        </asp:Table>
                        <asp:GridView ID="dgvRetailCompetitions" runat="server" AutoGenerateColumns="False"
                            CellPadding="5" ForeColor="#333333" GridLines="Horizontal" BorderColor="#00728F"
                            BorderStyle="Solid" BorderWidth="2px" EmptyDataText="No Competitions Found!"
                            EmptyDataRowStyle-ForeColor="Red" EmptyDataRowStyle-HorizontalAlign="Center"
                            Width="100%" HorizontalAlign="Center" ShowHeaderWhenEmpty="True" AllowPaging="True"
                            PageSize="20" DataKeyNames="RewardDescription" AllowSorting="True" ShowFooter="True">
                            <AlternatingRowStyle BackColor="White" Width="70%" HorizontalAlign="Center" />
                            <Columns>
                                <asp:CommandField ShowEditButton="true" EditText="Edit" UpdateText="Update" CancelText="Cancel" />
                                <asp:CommandField ShowDeleteButton="true" DeleteText="Delete" />
                                <asp:TemplateField HeaderText="Ref. Year" SortExpression="ReferenceYear" ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("RefYear") %>' />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("RefYear") %>'></asp:Label>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:Button ID="btnInsertRecord" runat="server" CommandName="Insert" Text="Insert Record"
                                            ValidationGroup="Insert" CssClass="buttonBlue" />
                                    </FooterTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Reward Description" SortExpression="RewardDescription">
                                    <ItemTemplate>
                                        <asp:Label ID="lblRewardsDescription" runat="server" Text='<%# Eval("RewardDescription") %>' />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtRewardsDescription" runat="server" MaxLength="50" Text='<%# Eval("RewardDescription") %>'
                                            Width="200px" />
                                        <asp:CompareValidator ID="txtRewardsDescription_String" runat="server" ControlToValidate="txtRewardsDescription"
                                            Display="Dynamic" ErrorMessage="'Target' must be a string" ForeColor="Red" Operator="DataTypeCheck"
                                            SetFocusOnError="true" Type="String" />
                                    </EditItemTemplate>
                                    <ItemStyle HorizontalAlign="Center" />
                                    <FooterTemplate>
                                        <asp:TextBox ID="txtRewardsDescription" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="rfvRewardsDescription" runat="server" ControlToValidate="txtRewardsDescription"
                                            ErrorMessage="Reward Description Required" Display="Dynamic" ValidationGroup="Insert"></asp:RequiredFieldValidator>
                                    </FooterTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Cat. A Conditions" SortExpression="CategoryA_Conditions">
                                    <ItemTemplate>
                                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("CategoryA_Conditions") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:Label ID="Label3" runat="server" Text='<%# Eval("CategoryA_Conditions") %>'></asp:Label>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:TextBox ID="txtCatAConditions" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="rfvCatAConditions" runat="server" ControlToValidate="txtCatAConditions"
                                            ErrorMessage="Cat. A Conditions Required" Display="Dynamic" ValidationGroup="Insert"></asp:RequiredFieldValidator>
                                    </FooterTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Cat. B Conditions" SortExpression="CategoryB_Conditions">
                                    <ItemTemplate>
                                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("CategoryB_Conditions") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:Label ID="Label4" runat="server" Text='<%# Eval("CategoryB_Conditions") %>'></asp:Label>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:TextBox ID="txtCatBConditions" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="rfvCatBConditions" runat="server" ControlToValidate="txtCatBConditions"
                                            ErrorMessage="Cat. B Conditions Required" Display="Dynamic" ValidationGroup="Insert"></asp:RequiredFieldValidator>
                                    </FooterTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Cat. C Conditions" SortExpression="CategoryC_Conditions">
                                    <ItemTemplate>
                                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("CategoryC_Conditions") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:Label ID="Label5" runat="server" Text='<%# Eval("CategoryC_Conditions") %>'></asp:Label>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:TextBox ID="txtCatCConditions" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="rfvCatCConditions" runat="server" ControlToValidate="txtCatCConditions"
                                            ErrorMessage="Cat. C Conditions Required" Display="Dynamic" ValidationGroup="Insert"></asp:RequiredFieldValidator>
                                    </FooterTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <EditRowStyle BackColor="#4fb3cf" />
                            <EmptyDataRowStyle ForeColor="Red" HorizontalAlign="Center" />
                            <FooterStyle BackColor="#FBAD23" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
                            <HeaderStyle BackColor="#FBAD23" Font-Bold="True" ForeColor="Black" />
                            <PagerStyle BackColor="#FBAD23" Font-Bold="true" ForeColor="Black" HorizontalAlign="Center" />
                            <RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" />
                            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                            <SortedAscendingCellStyle BackColor="#F5F7FB" />
                            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                            <SortedDescendingCellStyle BackColor="#E9EBEF" />
                            <SortedDescendingHeaderStyle BackColor="#4870BE" />
                        </asp:GridView>
                    </div>
                </div>
                <asp:ValidationSummary ID="validationSummary" runat="server" ValidationGroup="Insert"
                    ForeColor="Red" />
            </asp:Panel>
            <uc:SortExtender ID="dgvRetailCompetitions_SortExtender" runat="server" EnableViewState="true"
                GridViewSortExpression="RewardDescription" GridViewSortDirection="Ascending" />
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <uc:InformationBox ID="InformationBox" runat="server" EnableViewState="true" />
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

Solution

  • After taking a look I have two possible suggestions for you -

    1) Check where you are binding your grid view -

    It should be inside - !IsPostBack

    Eg:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            grid.DataBind();
        }
    }
    

    2) Have you disabled ViewState somewhere?

    EDIT - Issue Resolved

    In your asp:GridView there needs to be a onrowcommand to trigger the command in codebehind. Add this to your asp:gridview tag in the .aspx page

    onrowcommand="dgvRetailCompetitions_RowCommand"