RowEditing命令没有触发

时间:2014-05-12 15:39:55

标签: c# gridview roweditor

我有一个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方法,页脚行会显示插入操作。

如何获取“编辑”按钮以使单击的行可编辑?

更新信息Page_Load 这是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");
        }
    }

感谢。

2 个答案:

答案 0 :(得分:0)

只是一个猜测,你是在每次回发上数据绑定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();
}

答案 1 :(得分:0)

我纠正了这个问题...

在我的标记中,我设置了Edit Button CausesValidation = true和ValidationGroup-MappingGrid,这是插件验证在页脚中的内容。要更正,我将CausesValidation设置为false并删除ValidationGroup。

我在“更新按钮”字段中设置了这些值。

立即工作!

感谢。