我正在尝试使用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;
}
}
答案 0 :(得分: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();
答案 1 :(得分:1)
我建议分别使用OnInsertCommand,OnUpdateCommand和OnDeleteCommand。
比为每个命令使用switch语句要清晰得多。
func getSquadMembersCoordinates() -> Array<PFGeoPoint> {
var coordinatesArray:[PFGeoPoint] = [PFGeoPoint]()
if user != nil {
let userSquad = PFUser.currentUser()?["inSquad"] as? String
let query = PFUser.query()
query?.whereKey("username", notEqualTo: (PFUser.currentUser()? ["username"])!)
query?.whereKey("inSquad", equalTo: userSquad!)
query?.whereKey("visible", notEqualTo: false)
query?.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if results?.count != 0 {
if let results = results {
for object in results {
coordinatesArray.append(object.objectForKey("location") as! PFGeoPoint)
}
}
} else {
print("No Squad Members or not visible")
}
} else {
print ("\(error)")
}
}
} else {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SquadVC")
self.presentViewController(viewController, animated: true, completion: nil)
})
}
print("\(coordinatesArray.count)")
return coordinatesArray
}