In my grid, I need the ability to click "Edit" (using GridEditCommandColumn) and enable EditForms mode. I also need the ability to click "Delete" and also enable EditForms mode. Prior to deleting the row in question, the user needs to submit a brief reason as to why that row is being deleted. If I have just a standard GridButtonColumn, I don't have the ability to do anything but the confirmation text to confirm the deletion (or so I think).
Basically, I need to have two GridEditCommandColumns, one for "Edit" and one for "Delete". Is this possible?
Thanks,
Kyle Parker
9 Answers, 1 is accepted
You can change the text for your second GridEditCommandColumn and handle UpdateCommand where you can cancel the grid default behavior and perform desired action instead update.
Regards,
Vlad
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Thank you for the response. I have tried that, but I believe I run into problems with the GridTemplateColumn and the EditItemTemplate. How do I know which "edit item template" to use for the "Edit" versus the "Delete"? I think that's where the error I was getting came into play.
Here is the error:
Multiple controls with the same ID 'EditButton' were found. FindControl requires that controls have unique IDs.
This occurs on the ItemDataBound event when I am trying to find the various controls in the ItemTemplate.
Here's a small code sample:
<DetailTables>
<telerik:GridTableView DataKeyNames="MemberID,MemberTermID" Caption="Membership information" Name="MembershipList" AllowFilteringByColumn="false"
ItemStyle-VerticalAlign="Top" Width="600" EditMode="InPlace" NoDetailRecordsText="There is no membership information available">
<ParentTableRelation>
<telerik:GridRelationFields DetailKeyField="OrganizationID" MasterKeyField="OrganizationID" />
</ParentTableRelation>
<ItemStyle CssClass="GridRow_BallState" />
<AlternatingItemStyle CssClass="GridAltRow_BallState" />
<Columns>
<telerik:GridEditCommandColumn UniqueName="EditMemberTerm" ButtonType="LinkButton" EditText="Edit"></telerik:GridEditCommandColumn>
<telerik:GridEditCommandColumn UniqueName="DeleteMemberTerm" ButtonType="LinkButton" EditText="Delete"></telerik:GridEditCommandColumn>
<telerik:GridTemplateColumn HeaderText="Term" SortExpression="Term" DataField="Term">
<ItemTemplate>
<asp:Label runat="server" ID="lblTerm"></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Role" SortExpression="MemberRole" DataField="MemberRole">
<ItemTemplate>
<asp:Label runat="server" ID="lblRole"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddlRole"></asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ID="rfvRole" Display="Dynamic" ErrorMessage="*" InitialValue="0"
ControlToValidate="ddlRole"></asp:RequiredFieldValidator>
</EditItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</telerik:GridTableView>
</DetailTables>
Code behind:
#region
Membership detail table
if (!e.Item.IsInEditMode)
{
Label lblTerm = (Label)e.Item.FindControl("lblTerm");
lblTerm.Text =
DataBinder.Eval(e.Item.DataItem, "Term").ToString();
Label lblRole = (Label)e.Item.FindControl("lblRole");
lblRole.Text =
DataBinder.Eval(e.Item.DataItem, "MemberRole").ToString();
Label lblAnonymousMemberFlag = (Label)e.Item.FindControl("lblAnonymousMemberFlag");
lblAnonymousMemberFlag.Text =
DataBinder.Eval(e.Item.DataItem, "AnonymousMemberFlag").ToString();
}
else
{
...
}
You can use a GridButtonColumn with its CommandName set to Edit rather than using a the EditCommandColumn twice, due to which you get the error. Also you can switch between the AutoGeneratedEditForm and a FormTemplateEditFrom to display different forms for Edit and Delete. Check out the following code to understand better.
aspx:
<telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" runat="server" OnItemCommand="RadGrid1_ItemCommand"> |
<MasterTableView DataSourceID="SqlDataSource1"> |
<Columns> |
<telerik:GridEditCommandColumn UniqueName="EditMemberTerm"></telerik:GridEditCommandColumn> |
<telerik:GridButtonColumn UniqueName="DeleteMemberTerm" CommandArgument="Delete" CommandName="Edit" Text="Delete" ></telerik:GridButtonColumn> |
</Columns> |
<EditFormSettings> |
<FormTemplate> |
Reason : |
<asp:TextBox ID="TextBox1" TextMode="MultiLine" runat="server"></asp:TextBox> |
</FormTemplate> |
</EditFormSettings> |
</MasterTableView> |
</telerik:RadGrid> |
cs(To switch the EditFroms):
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) |
{ |
if (e.CommandName == "Edit") |
{ |
if (e.CommandArgument == "Delete") |
{ |
RadGrid1.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.Template; |
} |
else |
{ |
RadGrid1.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.AutoGenerated; |
} |
} |
} |
Thanks
Princy.
Thank you for the reply. I have attempted to implement your suggestion, but when I switch between "Edit" and "Delete" I see the "edit" version of the form. I assume I'm missing something!
Would you be able to point me in the right direction?
Thank you!
<DetailTables> |
<telerik:GridTableView DataKeyNames="MemberID,MemberTermID" Caption="Membership information" Name="MembershipList" AllowFilteringByColumn="false" |
ItemStyle-VerticalAlign="Top" Width="600" EditMode="EditForms" NoDetailRecordsText="There is no membership information available"> |
<ParentTableRelation> |
<telerik:GridRelationFields DetailKeyField="OrganizationID" MasterKeyField="OrganizationID" /> |
</ParentTableRelation> |
<ItemStyle CssClass="GridRow_BallState" /> |
<AlternatingItemStyle CssClass="GridAltRow_BallState" /> |
<Columns> |
<telerik:GridEditCommandColumn UniqueName="EditMemberTerm" ButtonType="LinkButton" EditText="Edit"></telerik:GridEditCommandColumn> |
<telerik:GridButtonColumn UniqueName="DeleteMemberTerm" ButtonType="LinkButton" CommandArgument="Delete" CommandName="Edit" Text="Delete"></telerik:GridButtonColumn> |
<telerik:GridTemplateColumn HeaderText="Term" SortExpression="Term" DataField="Term"> |
<ItemTemplate> |
<asp:Label runat="server" ID="lblTerm"></asp:Label> |
</ItemTemplate> |
</telerik:GridTemplateColumn> |
<telerik:GridTemplateColumn HeaderText="Role" SortExpression="MemberRole" DataField="MemberRole"> |
<ItemTemplate> |
<asp:Label runat="server" ID="lblRole"></asp:Label> |
</ItemTemplate> |
<EditItemTemplate> |
<asp:DropDownList runat="server" ID="ddlRole"></asp:DropDownList> |
<asp:RequiredFieldValidator runat="server" ID="rfvRole" Display="Dynamic" ErrorMessage="*" InitialValue="0" |
ControlToValidate="ddlRole"></asp:RequiredFieldValidator> |
</EditItemTemplate> |
</telerik:GridTemplateColumn> |
<telerik:GridTemplateColumn HeaderText="Anonymous" SortExpression="AnonymousMemberFlag" DataField="AnonymousMemberFlag"> |
<ItemTemplate> |
<asp:Label runat="server" ID="lblAnonymousMemberFlag"></asp:Label> |
</ItemTemplate> |
<EditItemTemplate> |
<asp:DropDownList runat="server" ID="ddlAnonymousMember"></asp:DropDownList> |
<asp:RequiredFieldValidator runat="server" ID="rfvAnonymousMember" Display="Dynamic" ErrorMessage="*" InitialValue="0" |
ControlToValidate="ddlAnonymousMember"></asp:RequiredFieldValidator> |
</EditItemTemplate> |
</telerik:GridTemplateColumn> |
</Columns> |
<EditFormSettings> |
<FormTemplate> |
Delete this! |
</FormTemplate> |
</EditFormSettings> |
</telerik:GridTableView> |
aspx.cs
if (e.Item.OwnerTableView.Name == "MembershipList") |
{ |
int intMemberTermID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["MemberTermID"].ToString()); |
switch (e.CommandName) |
{ |
case "Edit": |
if ((string) e.CommandArgument == "Delete") |
{ |
rgOrganizationList.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.Template; |
} |
else |
{ |
rgOrganizationList.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.AutoGenerated; |
} |
break; |
} |
} |
It seems that you are trying to switch between the AutoGeneratedEditForm and a FormTemplateEditForm for your Detail Table. Then you should be trying out the following code:
cs:
if (e.Item.OwnerTableView.Name == "MembershipList") |
{ |
int intMemberTermID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["MemberTermID"].ToString()); |
switch (e.CommandName) |
{ |
case "Edit": |
if ((string) e.CommandArgument == "Delete") |
{ |
e.Item.OwnerTableView.EditFormSettings.EditFormType = GridEditFormType.Template; |
} |
else |
{ |
e.Item.OwnerTableView.EditFormSettings.EditFormType = GridEditFormType.AutoGenerated; |
} |
break; |
} |
} |
Thanks
Princy.
Thank you - this works perfectly!
Kyle
I've tried several different things and looked through the support posts, but none of the options seemed to work. I'm not sure if it is because my form template is in a detail table?
Here's my code...
<DetailTables> |
<telerik:GridTableView DataKeyNames="MemberID,MemberTermID" Caption="Membership information" Name="MembershipList" AllowFilteringByColumn="false" |
ItemStyle-VerticalAlign="Top" Width="600" EditMode="EditForms" NoDetailRecordsText="There is no membership information available" |
CommandItemDisplay="TopAndBottom" CommandItemSettings-AddNewRecordText="Add new term membership"> |
<ParentTableRelation> |
<telerik:GridRelationFields DetailKeyField="OrganizationID" MasterKeyField="OrganizationID" /> |
</ParentTableRelation> |
<ItemStyle CssClass="GridRow_BallState" /> |
<AlternatingItemStyle CssClass="GridAltRow_BallState" /> |
<Columns> |
<telerik:GridEditCommandColumn UniqueName="EditMemberTerm" ButtonType="LinkButton" EditText="Edit"></telerik:GridEditCommandColumn> |
<telerik:GridButtonColumn UniqueName="DeleteMemberTerm" ButtonType="LinkButton" CommandArgument="Delete" CommandName="Edit" Text="Delete"></telerik:GridButtonColumn> |
</Columns> |
<EditFormSettings> |
<FormTemplate> |
<table cellpadding="2"> |
<tr> |
<td> |
<asp:Label runat="server" ID="lblDeletedReason" Text="Reason:"></asp:Label> |
</td> |
<td> |
<asp:TextBox runat="server" ID="tbxDeletedReason" TextMode="MultiLine" Rows="5" Columns="40"></asp:TextBox> |
<asp:RequiredFieldValidator runat="server" ID="rfvDeletedReason" Display="Dynamic" ErrorMessage="*" |
ControlToValidate="tbxDeletedReason"></asp:RequiredFieldValidator> |
</td> |
</tr> |
<tr> |
<td colspan="2"> |
<asp:LinkButton runat="server" ID="lbntDelete" Text="Delete" CommandName="DeleteTerm" CausesValidation="true"></asp:LinkButton> |
<asp:LinkButton runat="server" ID="lbtnCancel" Text="Cancel" CommandName="CancelTerm" CausesValidation="false"></asp:LinkButton> |
</td> |
</tr> |
</table> |
</FormTemplate> |
</EditFormSettings> |
</telerik:GridTableView> |
</DetailTables> |
And this is the codebehind the ItemCommand event:
protected void rgOrganizationList_ItemCommand(Object sender, GridCommandEventArgs e) |
{ |
if (e.Item is GridDataItem) |
{ |
if (e.Item.OwnerTableView.Name == "MembershipList") |
{ |
switch (e.CommandName) |
{ |
case "Edit": |
e.Item.OwnerTableView.EditFormSettings.EditFormType = (string) e.CommandArgument == "Delete" ? GridEditFormType.Template : GridEditFormType.AutoGenerated; |
break; |
case "DeleteTerm": |
int intMemberTermID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["MemberTermID"].ToString()); |
TextBox tbxDeletedReason = (TextBox)e.Item.FindControl("tbxDeletedReason"); |
Data.MemberTerm.Delete(intMemberTermID, tbxDeletedReason.Text, UserGuid); |
break; |
case "CancelTerm": |
e.Item.OwnerTableView.ClearEditItems(); |
break; |
} |
} |
} |
} |
It is returning "null" for
TextBox
tbxDeletedReason = (TextBox)e.Item.FindControl("tbxDeletedReason");
Thanks for any help!
Kyle
You can access the LinkButton and thereby access the TextBox in its NamingContainer as shown below:
cs:
protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e) |
{ |
if (e.Item.OwnerTableView.Name == "MembershipList") |
{ |
switch (e.CommandName) |
{ |
case "Edit": |
e.Item.OwnerTableView.EditFormSettings.EditFormType = (string)e.CommandArgument == "Delete" ? GridEditFormType.Template : GridEditFormType.AutoGenerated; |
break; |
case "DeleteTerm": |
int intMemberTermID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["MemberTermID"].ToString()); |
LinkButton linkBtn = (LinkButton)e.CommandSource; |
GridEditFormItem editItem = (GridEditFormItem)linkBtn.NamingContainer; |
TextBox tbxDeletedReason = (TextBox)editItem.FindControl("tbxDeletedReason"); |
Data.MemberTerm.Delete(intMemberTermID, tbxDeletedReason.Text, UserGuid); |
break; |
case "CancelTerm": |
e.Item.OwnerTableView.ClearEditItems(); |
break; |
} |
} |
Thanks
Princy.
Kyle