包含多个Gridview RowEditing事件的页面未触发

时间:2015-11-18 13:43:31

标签: asp.net .net gridview datagridview

网页使用.Net framework 4.5设计。网页包含3个网格视图,如下所示:

enter image description here

Gridview的代码如下:

<asp:GridView ID="grdADetails" runat="server" AutoGenerateColumns="False" CssClass="tGrid" GridLines="Horizontal" ShowFooter="True" Width="100%" OnRowCancelingEdit="grdADetails_RowCancelingEdit" OnRowCommand="grdADetails_RowCommand" OnRowDataBound="grdADetails_RowDataBound" OnRowDeleting="grdADetails_RowDeleting" OnRowEditing="grdADetails_RowEditing" OnRowUpdating="grdADetails_RowUpdating">
                    <Columns>
                                                <asp:TemplateField HeaderText="Mode Of Transport">
                                                    <EditItemTemplate>
                                                        <asp:DropDownList ID="DrpTMode" runat="server" CssClass="trvGridCmbBox" Width="90%">
                                                        </asp:DropDownList>
                                                    </EditItemTemplate>
                                                    <FooterTemplate>
                                                        <table border="0" cellpadding="0" cellspacing="0" style="WIDTH: 100%">
                                                            <tr>
                                                                <td class="tGridTotal" style="Height : 25px;"></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="GridEditArea" style="Height : 25px;">
                                                                    <asp:DropDownList ID="DrpNTMode" runat="server" CssClass="trvGridCmbBox" Font-Size="8pt" Width="98%">
                                                                    </asp:DropDownList>
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </FooterTemplate>
                                                    <HeaderTemplate>
                                                        <asp:Label ID="lblTMode" runat="server" Text="<%$ Resources:EResources, lblModeOfT %>" Width="100%"></asp:Label>
                                                    </HeaderTemplate>
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblESubTypeCode" runat="server" Text='<%# Bind("ESubTypeCode") %>' Visible="False"></asp:Label>
                                                        <asp:Label ID="lblTMode" runat="server" Text='<%# Bind("TMode") %>'></asp:Label>
                                                    </ItemTemplate>
                                                    <FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                                    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="False" />
                                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="12%" />
                                                </asp:TemplateField>
                        //For Date part using Ajax Control
                        <asp:TemplateField HeaderText="Departure On">
                                                    <EditItemTemplate>
                                                        <asp:TextBox ID="txtDeptOn" runat="server" CssClass="trvGridTextBox" Text='<%# Bind("DeptOn") %>' Width="90%"></asp:TextBox>
                                                        <ajaxToolkit:CalendarExtender ID="ceDeptOn" runat="server" Format="MM/dd/yyyy" TargetControlID="txtDeptOn" Enabled="true" Animated="true" EnabledOnClient="true" EnableViewState="true" FirstDayOfWeek="Default"/>
                                                    </EditItemTemplate>
                                                    <FooterTemplate>
                                                        <table border="0" cellpadding="0" cellspacing="0" style="WIDTH: 100%">
                                                            <tr>
                                                                <td class="tGridTotal" style="Height : 25px;"></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="GridEditArea" style="Height : 25px;">
                                                                    <asp:TextBox ID="txtNewDeptOn" runat="server" CssClass="trvGridTextBox" Width="100%"></asp:TextBox>
                                                                    <ajaxToolkit:CalendarExtender ID="ceNewDeptOn" runat="server" Enabled="true" Format="MM/dd/yyyy" TargetControlID="txtNewDeptOn" Animated="true" EnabledOnClient="true" EnableViewState="true" FirstDayOfWeek="Default"/>
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </FooterTemplate>
                                                    <HeaderTemplate>
                                                        <asp:Label ID="lblDepartureOn" runat="server" Text="<%$ Resources:EASEResources, lblDeparOn %>" Width="100%"></asp:Label>
                                                    </HeaderTemplate>
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblDeptOn" runat="server" Text='<%# Bind("DeptOn") %>'></asp:Label>
                                                    </ItemTemplate>
                                                    <FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                                    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10%" />
                                                </asp:TemplateField>
                        //Same as above for remaining columns
                        //For Edit button column
                        <asp:TemplateField HeaderText="Edit">
                                                    <EditItemTemplate>
                                                        <asp:ImageButton ID="ImgUpdate" runat="server" CommandName="Update" ImageUrl="~/PL/images/save.png" />
                                                        <asp:ImageButton ID="ImageButton4" runat="server" CommandName="Cancel" ImageUrl="~/PL/images/remove1.png" />
                                                    </EditItemTemplate>
                                                    <FooterTemplate>
                                                        <table border="0" cellpadding="0" cellspacing="0" style="WIDTH: 100%">
                                                            <tr>
                                                                <td class="tGridTotal" style="height: 25px"></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="GridEditArea" aria-orientation="vertical" style="height: 25px">
                                                                    <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" CommandName="AddNew" CssClass="trvGridTextBox" ImageUrl="~/PL/images/save.png" />
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </FooterTemplate>
                                                    <HeaderTemplate>
                                                        <asp:Label ID="lblEdit" runat="server" Text="<%$ Resources:EResources, lblEdit %>" Width="100%"></asp:Label>
                                                    </HeaderTemplate>
                                                    <ItemTemplate>
                                                        <asp:ImageButton ID="ImageButton2" runat="server" CommandName="Edit" ImageUrl="~/PL/images/edit.png" />
                                                    </ItemTemplate>
                                                    <FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                                    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="4%" />
                                                </asp:TemplateField>

以类似格式创建剩余的两个网格视图。

<asp:GridView ID="GrdTDetail" runat="server" AutoGenerateColumns="False" CssClass="travelGrid" GridLines="Horizontal" ShowFooter="True" Width="100%" OnRowCancelingEdit="GrdTDetail_RowCancelingEdit" OnRowCommand="GrdTDetail_RowCommand" OnRowDataBound="GrdTDetail_RowDataBound" OnRowDeleting="GrdTDetail_RowDeleting" OnRowEditing="GrdTDetail_RowEditing" OnRowUpdating="GrdTDetail_RowUpdating">
               <Columns>
            //Other Column Code
            //Edit Column Code.
            <asp:TemplateField HeaderText="Edit">
                                                 <EditItemTemplate>
                                                        <asp:ImageButton ID="ImageButton3" runat="server" CausesValidation="False" CommandName="Update" ImageUrl="~/PL/images/save.png" />
                                                        <asp:ImageButton ID="ImageButton4" runat="server" CausesValidation="False" CommandName="Cancel" ImageUrl="~/PL/images/remove1.png" />
                                                 </EditItemTemplate>
                                                    <FooterTemplate>
                                                        <table border="0" style="WIDTH: 100%">
                                                            <tr>
                                                                <td class="travelGridTotal" style="Height : 25px;"></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="GridEditArea" style="Height : 25px;">
                                                                    <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" CommandName="AddNewACC" CssClass="trvGridTextBox" ImageUrl="~/PL/images/save.png" />
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </FooterTemplate>
                                                    <HeaderTemplate>
                                                        <asp:Label ID="lblEdit" runat="server" Text="<%$ Resources:EResources, lblEdit %>" Width="100%"></asp:Label>
                                                    </HeaderTemplate>
                                                    <ItemTemplate>
                                                        <asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False" CommandName="Edit" ImageUrl="~/PL/images/edit.png" />
                                                    </ItemTemplate>
                                                    <FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                                    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="4%" />
                                                </asp:TemplateField>
        </Columns>
                <FooterStyle CssClass="GridFooter" />
                <HeaderStyle CssClass="travelGridhead" /
</asp:GridView>

//第3个Gridview代码

<asp:GridView ID="GrdOtherExp" runat="server" AutoGenerateColumns="False" CellPadding="0" CssClass="travelGrid" GridLines="Horizontal" ShowFooter="True" Width="100%" OnRowCancelingEdit="GrdOtherExp_RowCancelingEdit" OnRowCommand="GrdOtherExp_RowCommand" OnRowDataBound="GrdOtherExp_RowDataBound" OnRowDeleting="GrdOtherExp_RowDeleting" OnRowEditing="GrdOtherExp_RowEditing" OnRowUpdating="GrdOtherExp_RowUpdating">
                 <Columns>
        //Other Column Code
        //Edit Column Code
        <asp:TemplateField HeaderText="Edit">
                  <EditItemTemplate>
                        <asp:ImageButton ID="ImageButton3" runat="server" CausesValidation="False" CommandName="Update" ImageUrl="~/PL/images/save.png" />
                         <asp:ImageButton ID="ImageButton4" runat="server" CausesValidation="False" CommandName="Cancel" ImageUrl="~/PL/images/close.png" />
                   </EditItemTemplate>
         <FooterTemplate>
               <table border="0" cellpadding="0" cellspacing="0" style="WIDTH: 100%">
                <tr>
                     <td class="travelGridTotal"></td>
                </tr>
                <tr>
                     <td class="GridEditArea">                                                   <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" CommandName="AddNewOExp" CssClass="trvGridTextBox" ImageUrl="~/PL/images/save.png" Height="16px" Width="16px" />
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </FooterTemplate>
                                                    <HeaderTemplate>
                                                        <asp:Label ID="lblEdit" runat="server" Text="<%$ Resources:EResources, lblEdit %>" Width="100%"></asp:Label>
                                                    </HeaderTemplate>
                                                    <ItemTemplate>
                                                        <asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False" CommandName="Edit" ImageUrl="~/PL/images/edit.png" />
                                                    </ItemTemplate>
                                                    <FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                                    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="2%" />
                                                </asp:TemplateField>
        </Columns>
                <FooterStyle CssClass="GridFooter" />
                <HeaderStyle CssClass="travelGridhead" /
</asp:GridView>

每个网格视图都有自己的 RowEditing,RowDataBound,RowUpdating,RowCancelEditing 事件。 Gridview功能如下:

  1. 用户可以从页脚行输入数据并使用具有
    的保存按钮 命令名称为“AddNew”将输入的数据保存到项目模板中 行格式。 RowCommand事件代码如下:

    protected void grdADetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {             
         DataTable dtADetails = (DataTable)ViewState["dtAccDetails"];
         ViewState["dtADetails"] = dtADetails;
         if (e.CommandName.Equals("AddNew"))
         {
             //function to add data
             //function to check required fields
          }
    
     }
    
  2. 用户可以在RowEding事件上编辑和更新行数据。行编辑按钮的命令名称为“编辑”。

     protected void grdADetails_RowEditing(object sender, GridViewEditEventArgs e)
     {
          grdADetails.EditIndex = e.NewEditIndex;
          DataTable dtADetails = (DataTable)ViewState["dtADetails"];// cache data to session        
           //Code for editing the functionality.
     }        
    
  3. 用户可以保存已编辑的行数据或取消编辑。

  4. 将页面发布到生产服务器后,只会触发一个网格视图事件,而不会触发剩余的两个网格视图事件。每个编辑按钮的命令名称为“编辑”以激活编辑事件。每个

0 个答案:

没有答案