This is a migrated thread and some comments may be shown as answers.

Clear fields in grid item on item command

2 Answers 172 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alin
Top achievements
Rank 1
Alin asked on 20 May 2011, 03:30 AM
Hello

I have a grid with a template column like this
<telerik:GridTemplateColumn HeaderText="Program Id" AllowFiltering="true" Reorderable="true" SortExpression="Program_Id"
                    UniqueName="Program_Id" DataField="Program_Id">
                        <ItemTemplate>
                            <asp:Label ID="lblProgrammeId" runat="server" Text='<%# Eval("Program_Id") %>' />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <telerik:RadTextBox ID="txbProgrammeId" Width="50px" runat="server" TextMode="SingleLine"
                                Text='<%# Bind("Program_Id") %>' />
                            <telerik:RadButton ID="ValidateProgBtn" AutoPostBack="true" runat="server" Width="16px"
                                Height="16px" CommandName="ValidateProgId" ToolTip="Validate the programme id" Text="Cancel">
                                <Image ImageUrl="Images/1305817624_clean.png" />
                            </telerik:RadButton>
                             <%--OnClientClicked="ClearFields" OnClientClicking="OnClientButtonClickingHandler"--%>
                            <telerik:RadButton ID="RadButton1" AutoPostBack="true" Value="NoPostBack" runat="server" Width="16px"
                                Height="16px" CommandName="Clear All"
                                ToolTip="Clear the fields" Text="Clear">
                                <Image ImageUrl="Images/1305817654_edit-clear.png" />
                            </telerik:RadButton>
                            <asp:Label ID="IsNewProgLbl" runat="server" style="width:auto; color:Orange" Text=""></asp:Label>
                        </EditItemTemplate>
                        <ItemStyle VerticalAlign="Top" />
                 </telerik:GridTemplateColumn>

When I click the clear all button, in edit mode, I want the fields from the grid to be cleared and also set the readonly property to false( it is set to true in a previous state). So I set the command name, then catch it in RadGrid1_ItemCommand event handler and try to access  a field like this:
......
if (e.CommandName == "Clear All")
            {
                     
                    (e.Item.FindControl("txbProgrammeId") as RadTextBox).Text = "";
                    (e.Item.FindControl("txbProgrammeId") as RadTextBox).ReadOnly = false;
...........
But it seems that only the Label control exists! I can't find the RadTextBox or RadButton controls. I find this strange, since when the grid is in insert mode it works ok. I even tried to force the grid to be in edit mode
e.Item.Edit = true;
GridEditableItem editedItem = e.Item as GridEditableItem;
but still nothing.
Is there a way that I can use to solve this? Basically, I want a  button that clears some fields when the grid is in edit or insert mode.

Thx a lot.

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 20 May 2011, 07:56 AM
Hello Alin,

 Since the RadTextBox is in EditItemTemplate, you need to access the GridEditableItem(EditMode=InPlace) first. For that you can use the  "e.CommandSource" for getting the RadButton and then access the GridEdittableItem by using the RadButton's NamingContainer property. Here is the sample code that I tried. Hope this helps you.

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
  if (e.CommandName == "ClearAll")
    {
    GridEditableItem item = (GridEditableItem)((e.CommandSource) as RadButton).NamingContainer;
    RadTextBox txtBox = (RadTextBox)item.FindControl("txbProgrammeId");
       txtBox.Text = "";
       txtBox.ReadOnly = false;
      }
}

Thanks,
Princy.
0
Alin
Top achievements
Rank 1
answered on 20 May 2011, 11:24 AM
Thx a lot man. A cool solution I couldn't ve have figured it out by my self otherwise :)
Tags
Grid
Asked by
Alin
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Alin
Top achievements
Rank 1
Share this question
or