2016-01-21 59 views
0

我正嘗試使用ItemCommand事件在RadGrid中插入新項目。但在此之後無法關閉編輯表單。無法關閉Telerik 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; 
    } 
} 

回答

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(); 
+0

我現在加了。它顯示了我在網格中的編輯列,但我仍然面臨問題。編輯表單不關閉。 – kamalpreet

+0

我剛添加了一些代碼隱藏代碼。確保將它添加到你的'ItemCommand'事件中。 – Sunil

+0

仍然沒有運氣! – kamalpreet

1

我想推薦分別使用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. 
}