RowEditing命令不开火

本文关键字:开火 命令 RowEditing | 更新日期: 2023-09-27 18:02:50

我有一个gridview,在footertemplate中添加行,工作良好。我想在网格中添加RowEditing和RowDeleting。我添加了RowEditing方法,但当我单击"编辑"按钮时,添加行显示在页脚中,好像它是"添加行"命令。当我创建一个网格的原型时,我有一个用于编辑的列,它是一个命令字段。当您单击该按钮时,行将改变,以便所有字段都是可编辑的。当字段是ItemTemplate字段时,我该如何做到这一点?

这是我的标记:

<asp:GridView ID="MappingGridView" runat="server" AllowSorting="True" AutoGenerateColumns="False" Caption="Enrollment Mapping Information" CaptionAlign="Top" 
    CssClass="grid" HorizontalAlign="Left" ShowFooter="True" AllowPaging="True" PageSize="4" ShowHeaderWhenEmpty="true" OnPageIndexChanging="MappingGridView_PageIndexChanging" 
     OnRowDataBound="MappingGridView_RowDataBound" OnRowCommand="MappingGridView_RowCommand" Width="1051px" OnRowEditing="MappingGridView_RowEditing">
    <Columns>
         <asp:TemplateField HeaderText="MappingID" SortExpression="mgvMappingID">
            <ItemTemplate>
                <asp:Label ID="mgvLblMappingID" runat="server" Text='<%# Bind("EnrollmentMappingID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="SectionID">
            <EditItemTemplate>
                <asp:TextBox ID="mgvDdlSectionID" runat="server" Text='<%# Bind("SectionID") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="mgvLblSectionID" runat="server" Text='<%# Bind("SectionID") %>'></asp:Label>
            </ItemTemplate>
             <FooterTemplate>
                <asp:TextBox ID="mgvInsertSectionID" runat="server" ReadOnly="true" Text="" Width="90%"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="SchoologyCourseID" SortExpression="mgvSchoologyCourseID">
            <EditItemTemplate>
                <asp:TextBox ID="mgvEditSchoologyCourseID" runat="server" Text='<%# Bind("SchoologyCourseID") %>'></asp:TextBox>
                 <asp:RequiredFieldValidator ID="ReqFieldValEditSchoologyCourseID" ControlToValidate="mgvEditSchoologyCourseID" runat="server" 
                     ErrorMessage="Required field." ValidationGroup="MappingGrid" Display="Dynamic"
                     CssClass="message-error">
                 </asp:RequiredFieldValidator>
                 <asp:RegularExpressionValidator ID="RegExpValEditSchoologyCourseID" ControlToValidate="mgvEditSchoologyCourseID" runat="server" 
                     ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^'d+$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="mgvLblSchoologyCourseID" runat="server" Text='<%# Bind("SchoologyCourseID") %>'></asp:Label>
            </ItemTemplate>
             <FooterTemplate>
                <asp:TextBox ID="mgvInsertSchoologyCourseID" runat="server" Width="90%"></asp:TextBox>
                 <asp:RequiredFieldValidator ID="ReqFieldValSchoologyCourseID" ControlToValidate="mgvInsertSchoologyCourseID" runat="server" 
                     ErrorMessage="Required field." ValidationGroup="MappingGrid" Display="Dynamic"
                     CssClass="message-error">
                 </asp:RequiredFieldValidator>
                 <asp:RegularExpressionValidator ID="RegExpValSchoologyCourseID" ControlToValidate="mgvInsertSchoologyCourseID" runat="server" 
                     ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^'d+$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>
            </FooterTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="CECityActivityID" SortExpression="mgvCECityActivityID">
            <EditItemTemplate>
                <asp:TextBox ID="mgvEditCECityActivityID" runat="server" Text='<%# Bind("CECityActivityID") %>'></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegExpValEditCECityID" ControlToValidate="mgvEditCECityActivityID" runat="server" 
                     ErrorMessage="Enter 0-9, A-F, and hyphens. Maximum length is 50." ValidationGroup="MappingGrid" ValidationExpression="^[0-9A-Fa-f-]{0,50}$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="mgvLblCECityActivityID" runat="server" Text='<%# Bind("CECityActivityID") %>'></asp:Label>
            </ItemTemplate>
             <FooterTemplate>
                <asp:TextBox ID="mgvInsertCECityActivityID" runat="server" Width="90%"></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegExpValCECityID" ControlToValidate="mgvInsertCECityActivityID" runat="server" 
                     ErrorMessage="Enter only 0-9, A-F, and hyphens; maximum length is 50." ValidationGroup="MappingGrid" ValidationExpression="^[0-9A-Fa-f-]{0,50}$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Is Active" SortExpression="mgvIsActive">
            <EditItemTemplate>
                <asp:DropDownList ID="mgvEditIsActive" runat="server" Text='<%# Bind("IsActive") %>'>
                    <asp:ListItem>Yes</asp:ListItem>
                    <asp:ListItem>No</asp:ListItem>
                </asp:DropDownList>                
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="mgvLblIsActive" runat="server" Text='<%# Bind("IsActive") %>'></asp:Label>
            </ItemTemplate>
             <FooterTemplate>
                <asp:DropDownList ID="mgvInsertIsActive" runat="server" Width="90%">
                    <asp:ListItem Selected="True">Yes</asp:ListItem>
                    <asp:ListItem>No</asp:ListItem>
                </asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="Max. Enrollment">
            <EditItemTemplate>
                <asp:TextBox ID="mgvEditMaxEnrollment" runat="server" Text='<%# Bind("MaxEnrollment") %>'></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegExpValEditMaxEnroll" ControlToValidate="mgvEditMaxEnrollment" runat="server" 
                     ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^'d+$" 
                     Display="Dynamic" CssClass="message-error">
                </asp:RegularExpressionValidator>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="mgvLblMaxEnrollment" runat="server" Text='<%# Bind("MaxEnrollment") %>'></asp:Label>
            </ItemTemplate>
             <FooterTemplate>
                <asp:TextBox ID="mgvInsertMaxEnrollment" runat="server" Width="90%"></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegExpValMaxEnroll" ControlToValidate="mgvInsertMaxEnrollment" runat="server" 
                     ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^'d+$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>
            </FooterTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="Action" ShowHeader="False">
            <EditItemTemplate>
                <asp:Button ID="mgvImgUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:Button>
                &nbsp;<asp:Button ID="mgvImgCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:Button>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Button ID="mgvEditButton" runat="server" CausesValidation="True" ValidationGroup="MappingGrid" CommandName="Edit" Text="Edit" Width="40%"></asp:Button>
                <asp:Button ID="mgvDeleteButton" runat="server" CausesValidation="false" CommandName="Delete" Text="Delete" Width="40%"></asp:Button>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
            <FooterTemplate>
                <asp:Button ID="mgvBtnAdd" runat="server" CommandName="Add" Text="Add Mapping" Width="90%" CausesValidation="true" ValidationGroup="MappingGrid" />
            </FooterTemplate>
        </asp:TemplateField>
     </Columns>
     <EmptyDataTemplate>
        <tr> 
            <td colspan="11" style="text-align:center;">
                No mapping data was found for this section. Mappings can be added to this section by clicking the 'Add Mapping' Button.
            </td>
        </tr>
        <tr>
            <td></td>
            <td><asp:TextBox ID="mgvInsertSectionID" runat="server" ReadOnly="true" Width="90%"></asp:TextBox></td>
            <td><asp:TextBox ID="mgvInsertSchoologyCourseID" runat="server" Width="90%"></asp:TextBox>
                <asp:RequiredFieldValidator ID="ReqFieldValSchoologyCourseIDEmpty" ControlToValidate="mgvInsertSchoologyCourseID" runat="server" 
                     ErrorMessage="Required field." ValidationGroup="MappingGrid" Display="Dynamic"
                     CssClass="message-error">
                 </asp:RequiredFieldValidator>
                 <asp:RegularExpressionValidator ID="RegExpValSchoologyCourseIDEmpty" ControlToValidate="mgvInsertSchoologyCourseID" runat="server" 
                     ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^'d+$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>
            </td>
            <td><asp:TextBox ID="mgvInsertCECityActivityID" runat="server" Width="90%"></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegExpValCECityIDEmpty" ControlToValidate="mgvInsertCECityActivityID" runat="server" 
                     ErrorMessage="Enter only 0-9, A-F, and hyphens; maximum length is 50." ValidationGroup="MappingGrid" ValidationExpression="^[0-9A-Fa-f-]{0,50}$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>                  
            </td>
            <td><asp:DropDownList ID="mgvInsertIsActive" runat="server" Width="90%">
                <asp:ListItem Selected="True">Yes</asp:ListItem>
                <asp:ListItem>No</asp:ListItem>
                </asp:DropDownList></td>
            <td><asp:TextBox ID="mgvInsertMaxEnrollment" runat="server" Width="90%"></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegExpValMaxEnrollEmpty" ControlToValidate="mgvInsertMaxEnrollment" runat="server" 
                     ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^'d+$" 
                     Display="Dynamic" CssClass="message-error">
                 </asp:RegularExpressionValidator>
            </td>
            <td><asp:Button ID="mgvBtnAddEmpty" runat="server" CommandName="Add" Text="Add Mapping" Width="90%" CausesValidation="true" 
                ValidationGroup="MappingGrid" />
            </td>
        </tr>
     </EmptyDataTemplate>
 </asp:GridView>

下面是我的代码:

protected void MappingGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        MappingGridView.EditIndex = e.NewEditIndex;
        populateEnrollMappingGrid();
    }

当我单击编辑按钮时,不会调用RowEditing方法,并且页脚行显示插入操作。

我如何获得编辑按钮,使行单击可编辑?

UPDATED INFORMATION 这是Page_Load方法:

 protected void Page_Load(object sender, EventArgs e)
    {          
        try
        {
            if (!IsPostBack)
            {
                //Get the Environment Setting to determine the database to access
                txtBoxEnvironment.Text = CurrentEnvironment;
                DAL.setCurrentEnvironment(CurrentEnvironment);
                //Get All Section information from database
                CSectionInfo.getData();
                //Get All Mapping informatio from database
                CEnrollMappingInfo.getData();
                //Populate the Mapping grid
                MappingGridView.DataSource = CEnrollMappingInfo.getInitialData();
                //Populate the Drop Down Section List
                populateDropDownSectionList(CEnrollMappingInfo.SelectedSectionID);
                //Grid must be bound after the Dropdown List is set so the value in the footer can be determined
                MappingGridView.DataBind();
                //Populate the Section grid
                SectionGridView.DataSource = CSectionInfo.getDataUsingSectionID(CEnrollMappingInfo.SelectedSectionID);
                //Get Course information from database
                populateCourseGrid();
                //Populate the Drop Down Course List
                populateDropDownCourseList(CSectionInfo.SelectedCourseID);
                //Grid must be bound after the Dropdown List is set so the value in the footer can be determined
                SectionGridView.DataBind();
            }
        }
        catch (Exception ex)
        {
            logger.ErrorException(ex.Message, ex);
            Response.Redirect("~/Error.aspx");
        }
    }

谢谢。

RowEditing命令不开火

只是猜测,您将在每次回发时绑定GridView

您的Page_Load类似于:

protected void Page_Load(object sender, EventArgs e)
{           
    populateEnrollMappingGrid(); // a method where you assign the DataSource and call grid.DataBind()
}

使用IsPostBack属性:

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

我纠正了这个问题…

在我的标记中,我设置了编辑按钮CausesValidation=true和ValidationGroup-MappingGrid,这是在页脚插入验证的内容。为了纠正错误,我将CausesValidation设置为false,并删除了ValidationGroup。

我在Update Button字段设置了这些值。

工作现在!

谢谢。