嵌套网格编辑不起作用

时间:2012-02-12 03:56:14

标签: c# asp.net

我遇到了这个问题,我在网格中有网格,嵌套网格是可编辑的。我无法让编辑工作。让事情变得复杂的是嵌套网格在模态弹出扩展器中。

它可以点击编辑按钮。然后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 个答案:

答案 0 :(得分:1)

我将尝试回答您的部分问题,更新和删除代码。你需要利用内部gridview的“onrowcommand”(OnRowCommand)。