GridView 页脚中的按钮未触发

Button in GridView Footer not firing

我的 GridViews 页脚中的按钮在单击时没有触发。没有错误,没有代码被执行,它所做的只是刷新,页面上的所有内容都与单击按钮之前完全一样。连调试都不执行

  1. 正常声明代码隐藏

    页面语言="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="RetailCompetition.aspx.vb" 继承="Company.Applications.Production.UI.RetailCompetition"

事件

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. 和按钮

                            <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>
    

有人能看出有什么不对吗?

ASPX 文件更新

<%@ 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>

看完后我有两个可能的建议给你 -

1) 检查您绑定网格视图的位置 -

应该在里面 - !IsPostBack

例如:

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

2) 是否在某处禁用了 ViewState?

编辑 - 问题已解决

在您的 asp:GridView 中,需要有一个 onrow 命令来触发代码隐藏中的命令。将此添加到 .aspx 页面

中的 asp:gridview 标记

onrowcommand="dgvRetailCompetitions_RowCommand"