2012-02-12 50 views
0

我被困在這個問題上,我的網格內嵌套的網格是可編輯的。我無法使編輯工作。什麼是複雜的事情是,嵌套的網格是在一個模式彈出式擴展。嵌套的網格編輯不起作用

它工作的地方,我點擊編輯按鈕點。然後EmptyDataText屬性開始使用消息。如果我再次點擊它,網格會在編輯模式下打開,但更新/取消按鈕不起作用。單擊時取消按鈕顯示EmptyDataText屬性。

另一個問題是,這個嵌套網格不使用對象數據源,所以我將不得不在代碼文件中更新和刪除所有內容。現在還不確定要這樣做。

我想就如何更正這一問題提出一些建議。下面是代碼:

<asp:GridView ID="gvForum" runat="server" DataSourceID="odsForumApproval" DataKeyNames="id" Width="200px" 
RepeatColumns="1" DataKeyField="id" CssClass="gridview" 
AutoGenerateColumns="False" GridLines="None" OnSelectedIndexChanged="_OnCommand"> 
<AlternatingRowStyle CssClass="altbgcolor" /> 
<Columns> 
    <asp:TemplateField > 
     <ItemTemplate> 
      <asp:Label runat="server" ID="lblTitle" Text='<%# Bind("Title") %>' /> 

      <asp:Panel id="div" runat="server" align="center" class="confirm" style="display:none" > 
       <asp:GridView runat="server" ID="gvForumDetail" AutoGenerateColumns="False" DataKeyNames="id" 
        AllowPaging='true' CssClass="gridview" 
        AllowSorting="true" PageSize="5" CellPadding="5" OnRowEditing="gvForumDetail_OnRowEditing" 
        OnRowCancelingEdit="gvForumDetail_CancelRecord" > 
        <AlternatingRowStyle CssClass="altbgcolor" /> 
        <RowStyle VerticalAlign="Top" HorizontalAlign="Left" /> 
        <HeaderStyle CssClass="greenbar" ForeColor="White" /> <Columns> 
         <asp:BoundField DataField="id" ReadOnly="true" Visible="false" /> 
         <asp:TemplateField HeaderText="Title"> 
          <ItemTemplate> 
           <asp:Label runat="server" ID="lblTraining" Text='<%# Bind("title") %>' /> 
          </ItemTemplate> 
          <EditItemTemplate> 
           <asp:TextBox runat="server" ID="txtTraining" Text='<%# Bind("title") %>' /> 
          </EditItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Post Message"> 
          <ItemTemplate> 
           <asp:Label runat="server" ID="lblCompletionDate" Width="250" Text='<%# Bind("description") %>' /> 
          </ItemTemplate> 
          <EditItemTemplate> 
           <asp:TextBox runat="server" ID="txtDescription" Text='<%# Bind("description") %>' TextMode="MultiLine" Rows="5" Width="250" /> 
          </EditItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Posted By"> 
          <ItemTemplate> 
           <asp:Label runat="server" ID="lblRecurence" Text='<%# Bind("MemberName") %>' /> 
          </ItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField HeaderText="Posted Date"> 
          <ItemTemplate> 
           <asp:Label runat="server" ID="lblNotes" Text='<%# Eval("itemdate", "{0:d}") %>' /> 
          </ItemTemplate> 
          <EditItemTemplate> 
           <asp:CheckBox runat="server" ID="cbxApproved" Text='<%# Bind("approved") %>' /> 
          </EditItemTemplate> 
         </asp:TemplateField> 
         <asp:CommandField ShowCancelButton="true" ShowEditButton="true" ShowDeleteButton="true" /> 
        </Columns> 
       </asp:GridView> 
       <br /> 
       <Club:RolloverLink ID="btnClose" runat="server" Text="Close" /> 
      </asp:Panel> 

      <ajaxToolKit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" 
       TargetControlID="lblTitle" 
       PopupControlID="div" 
       CancelControlID="btnClose" 
       BackgroundCssClass="modalBackground" /> 

     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:CommandField ShowSelectButton="True" /> 
</Columns> 

後面的代碼:

public void _OnCommand(object sender, EventArgs e) 
{ 
    ObjectDataSource ods = new ObjectDataSource(); 
    ods.ID = "ods_ForumDetail"; 
    ods.EnableViewState = true; 
    ods.TypeName = "ForumApproval"; 
    ods.SelectMethod = "GetForumById"; 

    string id = ""; 
    int rowIndex = gvForum.SelectedIndex; 
    id = gvForum.DataKeys[rowIndex].Value.ToString(); 
    ods.SelectParameters.Add("id", System.TypeCode.Int32, id); 

    var ModalPopupExtender1 = (ModalPopupExtender)(gvForum.SelectedRow.FindControl("ModalPopupExtender1")); 
    ModalPopupExtender1.Show(); 

    var gvForumDetail = (GridView)(gvForum.SelectedRow.FindControl("gvForumDetail")); 
    gvForumDetail.DataSource = ods; 
    gvForumDetail.DataBind(); 
} 

protected void gvForumDetail_OnRowEditing(Object sender, GridViewEditEventArgs e) 
{ 
    var ModalPopupExtender1 = (ModalPopupExtender)(gvForum.SelectedRow.FindControl("ModalPopupExtender1")); 
    ModalPopupExtender1.Show(); 

    var gvForumDetail = (GridView)(gvForum.SelectedRow.FindControl("gvForumDetail")); 

    gvForumDetail.EditIndex = e.NewEditIndex; 
    gvForumDetail.DataBind(); 
} 

protected void gvForumDetail_CancelRecord(object sender, GridViewCancelEditEventArgs e) 
{ 
    var ModalPopupExtender1 = (ModalPopupExtender)(gvForum.SelectedRow.FindControl("ModalPopupExtender1")); 
    ModalPopupExtender1.Show(); 

    var gvForumDetail = (GridView)(gvForum.SelectedRow.FindControl("gvForumDetail")); 
    gvForumDetail.EditIndex = -1; 
    gvForumDetail.DataBind(); 
} 

回答

1

我會盡量回答你的問題的一部分,更新和刪除代碼。您需要利用內部gridview的「onrowcommand」(OnRowCommand)。

+0

是的,我看到OnRowCommand在任何OnUpdating或OnDeleting之前觸發。謝謝你,這是一個好的開始。 – Risho 2012-02-12 04:48:39