【问题标题】:Not able to close Edit Form in Telerik RadGrid无法在 Telerik RadGrid 中关闭编辑表单
【发布时间】:2016-01-21 17:09:11
【问题描述】:

我正在尝试使用ItemCommand 事件在 RadGrid 中插入新项目。但在此之后无法关闭编辑表单。

这是我的 aspx 中的代码-

<telerik:RadGrid ID="rgItems" Skin="Metro" runat="server" AutoGenerateColumns="false" Width="100%"
    AllowAutomaticInserts="true"
    MasterTableView-CommandItemSettings-ShowRefreshButton="false"
    OnNeedDataSource="rgItems_NeedDataSource" OnItemCommand="rgItems_ItemCommand">

    <MasterTableView CommandItemDisplay="Top" AllowAutomaticInserts="true" CommandItemSettings-ShowAddNewRecordButton="true">
        <EditFormSettings EditFormType="Template">
            <FormTemplate>
                <asp:Panel ID="pnlNewItem" runat="server" DefaultButton="btnInsert">
                    <div class="form-group">
                            <asp:TextBox ID="txtClass" runat="server" CssClass="form-control" placeholder="Enter Class" Text='<%# Eval("Class") %>'></asp:TextBox>
                    </div>
                    <div class="form-group">
                            <asp:TextBox ID="txtWeight" runat="server" CssClass="form-control" placeholder="Enter Weight" Text='<%# Eval("Weight") %>'></asp:TextBox>
                    </div>

                    <div class="box-footer">
                        <asp:Button ID="btnCancel" runat="server" Text="Cancel" class="btn btn-default" CommandName="Cancel" />
                        <asp:Button ID="btnInsert" runat="server" class="btn btn-info pull-right"
                            CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'
                            Text='<%# (Container is GridEditFormInsertItem) ? "Add Item" : "Update" %>' />
                    </div>
                </asp:Panel>
            </FormTemplate>
        </EditFormSettings>
        <Columns>
            <telerik:GridTemplateColumn HeaderText="Class">
                <ItemTemplate>
                    <asp:Label ID="lblClass" runat="server" placeholder="Enter Class" Text='<%# Eval("Class") %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Weight">
                <ItemTemplate>
                    <asp:Label ID="lblWeight" runat="server" placeholder="Enter Weight" Text='<%# Eval("Weight") %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

这是ItemCommand事件中的代码-

protected void rgItems_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{

        DataTable dtItems_Global = new DataTable();
        dtItems_Global.Columns.Add(new DataColumn("Class", typeof(string)));
        dtItems_Global.Columns.Add(new DataColumn("Weight", typeof(string)));

        if (rgItems.Items.Count > 0)
        {
            foreach (GridDataItem gdi in rgItems.Items)
            {
                DataRow drItem = dtItems_Global.NewRow();
                drItem["Class"] = (gdi.FindControl("lblClass") as Label).Text;
                drItem["Weight"] = (gdi.FindControl("lblWeight") as Label).Text;

                dtItems_Global.Rows.Add(drItem);
            }
        }
    switch (e.CommandName)
    {
        case "PerformInsert":
            TextBox txtItemClass = (e.Item.FindControl("txtClass") as TextBox);
            TextBox txtItemWeight = (e.Item.FindControl("txtWeight") as TextBox);

            DataRow drItem = dtItems_Global.NewRow();
            drItem["Class"] = txtItemClass.Text;
            drItem["Weight"] = txtItemWeight.Text;
            dtItems_Global.Rows.Add(drItem);

            rgItems.Rebind();
            break;
    }
}

【问题讨论】:

    标签: c# asp.net telerik radgrid


    【解决方案1】:

    您能否在 RadGrid 的列标记中包含如下所示的空编辑列?那是缺失的。

    <telerik:GridEditCommandColumn>
    </telerik:GridEditCommandColumn>
    

    此外,添加上述标记后,ItemCommand 的代码隐藏还应包含以下代码。

    protected void rgItems_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.InitInsertCommandName) //"Add Item" button clicked
            {
                GridEditCommandColumn editColumn = (GridEditCommandColumn)rgItems.MasterTableView.GetColumn("EditCommandColumn");
                editColumn.Visible = false;
            }
            else if (e.CommandName == RadGrid.RebindGridCommandName && e.Item.OwnerTableView.IsItemInserted)
            {
                e.Canceled = true;
            }
            else
            {
                GridEditCommandColumn editColumn = (GridEditCommandColumn)rgItems.MasterTableView.GetColumn("EditCommandColumn");
                if (!editColumn.Visible)
                    editColumn.Visible = true;
            }
        }
    

    如果上面没有解决它,那么在ItemInserted事件中使用下面代码的简单方法。

    e.KeepInInsertMode = false; 
    rgItems.EditIndexes.Clear(); 
    rgItems.Rebind(); 
    

    【讨论】:

    • 我现在添加了。它向我显示网格中的编辑列,但我仍然面临问题。编辑表单未关闭。
    • 我刚刚添加了一些代码隐藏代码。确保将其添加到您的 ItemCommand 事件中。
    • 还要确保在您的原始ItemCommand 事件中没有发生异常。您可以通过在调试模式下单步执行代码来了解这一点。
    • 一点也不例外。另外,我没有使用编辑功能,而只使用添加新记录功能。插入后,表单保持打开状态。
    • 您可以尝试在PerfomInsert 部分代码中的Rebind 之前添加rgItems.EditIndexes.Clear(); 吗?
    【解决方案2】:

    我想建议分别使用 OnInsertCommand、OnUpdateCommand 和 OnDeleteCommand。

    这比对每个命令都使用 switch 语句要干净得多。

    <telerik:RadGrid ID="rgItems" 
       ... 
       OnItemCommand="REMOVE THIS EVENT"
       OnInsertCommand="rgItems_InsertCommand"
       OnUpdateCommand="rgItems_UpdateCommand"           
       OnDeleteCommand="rgItems_DeleteCommand">
       <MasterTableView CommandItemDisplay="Top" DataKeyNames="Id"> 
         Make sure Id is the unique Id in your database - normally primary key.
    </telerik:RadGrid>
    
    protected void rgItems_InsertCommand(object source, GridCommandEventArgs e)
    {
       var item = e.Item as GridEditFormItem;
       var txtClass= item.FindControl("txtClass") as TextBox;
       // Insert to database - Do not need to call rgItems.Rebind(); here.
    }
    
    protected void rgItems_UpdateCommand(object source, GridCommandEventArgs e)
    {
       var item = e.Item as GridEditFormItem;    
       int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"]);   
       var txtClass= item.FindControl("txtClass") as TextBox;    
       // Update - Do not need to call rgItems.Rebind(); here.
    }
    
    protected void rgItems_DeleteCommand(object source, GridCommandEventArgs e)
    {
       int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"]);     
       // Delete - Do not need to call rgItems.Rebind(); here.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多