当前位置:编程学习 > C#/ASP.NET >>

GridView嵌套GridView,在第二个GridView中实现新增,编辑,更新等操作遇到了郁闷的问题?孟子E章上面那个问题的扩展

看完了
http://dotnet.aspx.cc/article/f73eeaa9-2bdc-47fd-afd2-59f2fa4897f5/read.aspx, 受益匪浅啊,所以我想实现两个GridView嵌套时,可以在子GridView中实现编辑和新增记录功能,代码如下:
<asp:GridView ID="ParentGridView" runat="server" AutoGenerateColumns="false" Width="780px"
      BorderWidth="1" DataKeyNames="ParentId" ShowHeader="false" OnRowDataBound="ParentGridView_RowDataBound">
      <Columns>
           <asp:TemplateField>
           <ItemTemplate>
            <div style="width: 100%; padding: 2px; font-weight: bold; background-color: #DEDEDE;float: left">
              <span style="float: left"><%#Eval("ParentName")%></span>
              <span style="float: right; color: Red; cursor: pointer" onclick="ShowHidden('<%#Eval("ParentId") %>',event)">隐藏</span>
            </div>
            
            <asp:GridView ID="ChildGridView" runat="server" AutoGenerateColumns="false" ShowHeader="true"
                HorizontalAlign="left" DataKeyNames="ChildId" Width="780px" OnRowEditing="ChildGridView_RowEditing"
                 OnRowCancelingEdit="ChildGridView_RowCancelingEdit" OnRowUpdating="ChildGridView_RowUpdating"
                 OnRowCommand="ChildGridView_RowCommand">
                <HeaderStyle BackColor="#888EE" />
                <Columns>                  
                  <asp:TemplateField HeaderText="操作" ShowHeader="true" ItemStyle-Width="200px">
                        <EditItemTemplate>
                            <asp:LinkButton ID="btnUpdate" runat="server" CausesValidation="True" CommandName="Update"
                                 Text="更新"></asp:LinkButton>
                            <asp:LinkButton ID="btnCancelUpdate" runat="server" CausesValidation="False" CommandName="Cancel"
                                Text="取消"></asp:LinkButton>
                        </EditItemTemplate>
                        <ItemTemplate>
                           <asp:LinkButton ID="btnNew" runat="server" CausesValidation="False" CommandName="New"
                                Text="新增" Width="90px"></asp:LinkButton> 
                            <asp:LinkButton ID="btnEdit" runat="server" CausesValidation="False" CommandName="Edit"
                                Text="编辑" Width="90px"></asp:LinkButton>                            
                        </ItemTemplate>
                  </asp:TemplateField>
                        
                  <asp:TemplateField HeaderText="ID" SortExpression="ChildId" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px">
                    <EditItemTemplate>
                        <asp:Label ID="txtChildId" runat="server" Text='<%# Eval("ChildId") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblChildId" runat="server" Text='<%#Eval("ChildId") %>'></asp:Label>
                    </ItemTemplate>
                    <InsertItemTemplate> </InsertItemTemplate>
                 </asp:TemplateField>
                 
                  <asp:TemplateField HeaderText="名称" SortExpression="ChildName" ItemStyle-HorizontalAlign="Center">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtChildName" runat="server" Text='<%# Eval("ChildName") %>'></asp:TextBox>
                        <asp:RequiredFieldValidator ID="CompareValidator1" runat="server" ControlToValidate="txtChildName"
                               ErrorMessage="名称必填填写">*</asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblChildName" runat="server" Text='<%# Eval("ChildName") %>'></asp:Label>
                    </ItemTemplate>
                     <InsertItemTemplate>
                        请输入版块名称:<asp:TextBox ID="txtChildNew" runat="server" Text='<%# Eval("ChildName") %>'></asp:TextBox>
                        <asp:RequiredFieldValidator ID="CompareValidator2" runat="server" ControlToValidate="txtChildNew"
                               ErrorMessage="名称必填填写">*</asp:RequiredFieldValidator>
                     </InsertItemTemplate>
                 </asp:TemplateField>
                </Columns>
            </asp:GridView>
              
          </ItemTemplate>
        </asp:TemplateField>
      </Columns>
    </asp:GridView>

protected void ChildGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   GridView ChildGridView= (GridView)sender; // 断点1
}

protected void ChildGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {   
        if (e.CommandName == "Update")// 断点2
        { 
        }
    }

现在的问题是:
1. 点击了“更新”按钮后,断点1是根本无法进入的;断点2可以进入,但是进入的话,要么e.CommandName = "Edit",要么e.CommandName == "New",可是明明点击的是Update,怎么会出现这种问题呢?
2. 如果需要新增,加入<InsertItemTemplate>,然后点击"新增“按钮,也就是btnNew,也进入不了任何断点?为什么?
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
                    <EditItemTemplate>
                        ...
                    </EditItemTemplate>
                    <ItemTemplate>
                        ...
                    </ItemTemplate>
                     <InsertItemTemplate>
                       ...
                     </InsertItemTemplate>
                 </asp:TemplateField>



请各位朋友帮忙分析一下以上两个问题,谢谢!


--------------------编程问答-------------------- 补充一下,Parent和child GridView都是通过程序绑定上去的,没有使用objectdatasource --------------------编程问答-------------------- up --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 我也想知道 --------------------编程问答-------------------- 我也想知道,这么发不了言,晕 --------------------编程问答-------------------- 没遇到,学习,帮忙顶下 --------------------编程问答-------------------- DataSource='<%#((System.Data.DataRowView)Container.DataItem).CreateChildView("tableRelation")%>' DataMember="GoodsTypeID" --------------------编程问答-------------------- DataSource='<%#((System.Data.DataRowView)Container.DataItem).CreateChildView("tableRelation")%>' DataMember="GoodsTypeID" --------------------编程问答-------------------- DataSource='<%#((System.Data.DataRowView)Container.DataItem).CreateChildView("tableRelation")%>' DataMember="GoodsTypeID"
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,