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

Find control in radgrid item command

13 Answers 3040 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Prassin
Top achievements
Rank 1
Prassin asked on 02 Oct 2012, 06:49 AM
Hi all,

how to use find control in rad grid item command ... i have two text boxes. i wish to retrieve the values from that textboxes inside the rad grid item command.. please help how it's possible...

Regards,
Prassin

13 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Oct 2012, 07:49 AM
Hello,

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
   {
       if (e.CommandName == "YourCommandName")
       {
           // For Normal mode
           GridDataItem item = e.Item as GridDataItem;
           TextBox TextBox1 = item.FindControl("TextBox1") as TextBox;
           // Access your TextBox Here
 
 
           // For Edit mode
           GridEditableItem eitem = e.Item as GridEditableItem;
           TextBox TextBox2 = eitem.FindControl("TextBox2") as TextBox; // From Item Template
           TextBox TextBox3 = eitem["ColumnUniqueName"].Controls[0] as TextBox; // From Bound Column
           // Access your TextBox Here
       }
   }


Thanks,
Jayesh Goyani
0
Prassin
Top achievements
Rank 1
answered on 02 Oct 2012, 08:50 AM
Hi Jayesh Goyani,

I have tried this code but i am getting an error like "Object reference not set to an instance of an object."

my code
Protected Sub rgdEditDevice_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgdEditDevice.ItemCommand
       If e.CommandName = "EditTest" Then
           Dim eitem As GridDataItem = TryCast(e.Item, GridDataItem)
           Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
           Dim TextBox1 As RadTextBox = TryCast(eitem.FindControl("txtEditIP"), RadTextBox)
           Dim a As String = TextBox1.Text
       End If
   End Sub

html

<FormTemplate>
          <table>
                  <tr>
                    <td align="left" valign="top">
                         <telerik:RadTextBox ID="txtEditIP" Text='<%# Bind("IP") %>' runat="server" Width="100px">
                           </telerik:RadTextBox>
                     </td>
                  </tr>
               </table>
    </FormTemplate>

please clarify any errors in this code

Regards,

Prassin
0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Oct 2012, 04:33 PM
Hello,

Can you please provide your RadGrid's structure/Code ?

So, we can help you to solve your issue.

Thanks,
Jayesh Goyani
0
Prassin
Top achievements
Rank 1
answered on 03 Oct 2012, 05:44 AM
Hi Jayesh,

This is the html

<telerik:RadGrid ID="rgdEditDevice" runat="server" CellSpacing="0" DataSourceID="SqlDataSource1"
                                 AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True" Width="565px"
                                 ShowStatusBar="True" AllowAutomaticInserts="True" AllowAutomaticDeletes="True"
                                 AllowAutomaticUpdates="True" EnableHeaderContextMenu="True" Height="340px" PageSize="14"
                                 GridLines="None" OnUpdateCommand="RadGrid1_UpdateCommand">
                                 <GroupingSettings CaseSensitive="false" />
                                 <FilterMenu EnableImageSprites="False">
                                 </FilterMenu>
                                 <PagerStyle Mode="NumericPages" AlwaysVisible="true" />
                                 <ClientSettings>
                                     <Scrolling AllowScroll="True" UseStaticHeaders="True" />
                                     <Selecting AllowRowSelect="true"></Selecting>
                                 </ClientSettings>
                                 <MasterTableView AutoGenerateColumns="False" DataKeyNames="DeviceID" DataSourceID="SqlDataSource1"
                                     AllowAutomaticInserts="true" HierarchyDefaultExpanded="True" CommandItemDisplay="Top"
                                     CommandItemSettings-ShowRefreshButton="false">
                                     <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
                                     <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                                         <HeaderStyle Width="20px"></HeaderStyle>
                                     </RowIndicatorColumn>
                                     <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                                         <HeaderStyle Width="20px"></HeaderStyle>
                                     </ExpandCollapseColumn>
                                     <Columns>
                                         <telerik:GridEditCommandColumn ButtonType="ImageButton">
                                             <HeaderStyle Width="3%" />
                                         </telerik:GridEditCommandColumn>
                                         <telerik:GridBoundColumn DataField="DeviceID" DataType="System.Int32" FilterControlAltText="Filter DeviceID column"
                                             HeaderText="DeviceID" ReadOnly="True" SortExpression="DeviceID" UniqueName="DeviceID"
                                             Visible="false">
                                         </telerik:GridBoundColumn>
                                         <telerik:GridBoundColumn DataField="SerialNumber" HeaderText="SerialNumber" SortExpression="SerialNumber"
                                             UniqueName="SerialNumber" DataType="System.String" Visible="false" FilterControlAltText="Filter SerialNumber column">
                                         </telerik:GridBoundColumn>
                                         <telerik:GridBoundColumn DataField="Port" FilterControlAltText="Filter Port column"
                                             HeaderText="Port" SortExpression="Port" UniqueName="Port" Visible="false" DataType="System.Int64">
                                         </telerik:GridBoundColumn>
                                         <telerik:GridBoundColumn DataField="IP" HeaderText="IP" SortExpression="IP" UniqueName="IP"
                                             FilterControlAltText="Filter IP column" Visible="false">
                                         </telerik:GridBoundColumn>
                                         <telerik:GridBoundColumn DataField="DeviceName" HeaderText="DeviceName" SortExpression="DeviceName"
                                             AllowFiltering="false" UniqueName="DeviceName" FilterControlAltText="Filter DeviceName column"
                                             HeaderStyle-Width="150px">
                                         </telerik:GridBoundColumn>
                                         <telerik:GridBoundColumn DataField="DeviceRemark" FilterControlAltText="Filter DeviceRemark column"
                                             HeaderText="DeviceRemark" SortExpression="DeviceRemark" UniqueName="DeviceRemark">
                                         </telerik:GridBoundColumn>
                                         <telerik:GridBoundColumn DataField="LocationID" FilterControlAltText="Filter LocationID column"
                                             HeaderText="LocationID" Visible="false" SortExpression="LocationID" UniqueName="LocationID"
                                             DataType="System.Int32">
                                         </telerik:GridBoundColumn>
                                         <telerik:GridButtonColumn ConfirmText="Delete this employee?" ButtonType="ImageButton"
                                             CommandName="Delete" Text="Delete" UniqueName="DeleteColumn1">
                                             <HeaderStyle Width="40px" />
                                             <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
                                         </telerik:GridButtonColumn>
                                     </Columns>
                                     <EditFormSettings EditFormType="Template">
                                         <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                                         </EditColumn>
                                         <FormTemplate>
                                             <table>
                                                 <tr>
                                                     <td align="left" valign="top">
                                                         Device name:
                                                     </td>
                                                     <td align="left" valign="top">
                                                         <telerik:RadTextBox ID="txtDevice" Text='<%# Bind("DeviceName") %>' runat="server">
                                                         </telerik:RadTextBox>
                                                         <asp:RequiredFieldValidator ID="rvName" runat="server" ControlToValidate="txtDevice"
                                                             ErrorMessage="Should enter value" />
                                                     </td>
                                                     <td align="left" valign="top">
                                                         IP:
                                                     </td>
                                                     <td align="left" valign="top">
                                                         <telerik:RadTextBox ID="txtEditIP" Text='<%# Bind("IP") %>' runat="server" Width="100px">
                                                         </telerik:RadTextBox>
                                                         <%--<asp:TextBox ID="txtEditIP" Text='<%# Bind("IP") %>' runat="server" Width="100px">
                                                         </asp:TextBox>--%>
                                                     </td>
                                                     <td align="left" valign="top">
                                                         Port:
                                                     </td>
                                                     <td align="left" valign="top">
                                                         <telerik:RadTextBox ID="txtEditPort" Text='<%# Bind("Port") %>' runat="server" Width="100px">
                                                         </telerik:RadTextBox>
                                                     </td>
                                                 </tr>
                                                 <%--<tr>
                                                     
                                                     <td align="left" valign="top" colspan="3">
                                                         <telerik:RadButton ID="RadButton1" runat="server" Text="Test Connect" CommandName="EditTest">
                                                             <Icon PrimaryIconCssClass="rbOk" PrimaryIconLeft="4" PrimaryIconTop="4" />
                                                         </telerik:RadButton>
                                                         Serial:
                                                     
                                                         <telerik:RadTextBox ID="txtEditSerial" Text='<%# Bind("SerialNumber") %>' runat="server"
                                                             Width="100px" Enabled="false">
                                                         </telerik:RadTextBox>
                                                     </td>
                                                 </tr>--%>
                                                 <tr>
                                                     <td align="left" valign="middle">
                                                         Remark:
                                                     </td>
                                                     <td align="left" valign="top" colspan="6">
                                                         <telerik:RadTextBox ID="txtRemarks" Text='<%# Bind("DeviceRemark") %>' runat="server"
                                                             TextMode="MultiLine" Width="400px">
                                                         </telerik:RadTextBox>
                                                     </td>
                                                 </tr>
                                                 <tr>
                                                     <td>
                                                         <telerik:RadButton ID="btnUpdate" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>'
                                                             runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>'
                                                             Icon-PrimaryIconCssClass="rbOk">
                                                         </telerik:RadButton>
                                                     </td>
                                                     <td>
                                                         <telerik:RadButton ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                                             CommandName="Cancel" Icon-PrimaryIconCssClass="rbCancel">
                                                         </telerik:RadButton>
                                                     </td>
                                                 </tr>
                                             </table>
                                         </FormTemplate>
                                     </EditFormSettings>
                                     <PagerStyle AlwaysVisible="True" CssClass="footer-bg"></PagerStyle>
                                 </MasterTableView>
                             </telerik:RadGrid>


thanks,

Prassin
0
Shinu
Top achievements
Rank 2
answered on 03 Oct 2012, 06:11 AM
Hi,

Try the following to achieve your scenario.
VB:
Protected Sub RadGrid1_ItemCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs)
    If e.CommandName = "EditTest" Then
        Dim editedItem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
        Dim txt As RadTextBox = DirectCast(DirectCast(editedItem, Telerik.Web.UI.GridDataItem).EditFormItem.FindControl("txtDevice"), RadTextBox)
    End If
End Sub

Thanks,
Shinu.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 03 Oct 2012, 08:42 AM
Hello,

try with below code snippet.

Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
    If e.CommandName = "EditTest" Then
        Dim eitem As GridEditableItem = TryCast(e.Item, GridEditableItem)
        Dim txtEditIP As RadTextBox = TryCast(eitem.FindControl("txtEditIP"), RadTextBox)
    End If
End Sub
 
'OR
 
Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
    If e.CommandName = "EditTest" Then
        Dim eitem As GridEditFormItem = TryCast(e.Item, GridEditFormItem)
        Dim txtEditIP As RadTextBox = TryCast(eitem.FindControl("txtEditIP"), RadTextBox)
    End If
End Sub


Thanks,
Jayesh Goyani
0
Henry
Top achievements
Rank 1
answered on 18 Nov 2015, 11:16 PM
why my e.item is always null? 
0
Eyup
Telerik team
answered on 23 Nov 2015, 11:31 AM
Hi Henry,

Could you elaborate on your specific scenario? Generally, if you want to access controls in edit mode, you can use the approaches demonstrated in the following section:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-controls-in-editinsert-mode

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Henry
Top achievements
Rank 1
answered on 24 Nov 2015, 11:59 AM

please check this link.  Thanks for reply

http://stackoverflow.com/questions/33784829/why-my-e-item-in-radgrid-itemcommand-event-always-null/33799873?noredirect=1#comment55399903_33799873

0
Eyup
Telerik team
answered on 26 Nov 2015, 11:25 AM
Hi Henry,

You are using e.Item as GridEditFormItem, but when the EditMode is InPlace, you need to use GridEditableItem editableItem = e.Item as GridEditableItem. Or GridDataInsertItem as explained in the provided section in my previous reply.

Alternatively, you can use the RadGrid1.MasterTableView.GetInsertItem() method.

Looking forward to hearing from you.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Henry
Top achievements
Rank 1
answered on 26 Nov 2015, 02:40 PM

Thanks a lot. I tried another way. I managed to put datasource and parameters on client side, and it works just fine. I gave up e.items things.

But I have another problem about raddropdownlist/ radcombobox in Radgrid returns null to parameters of datasource(insert and update). I've seen the doc of telerik demo. still can't find the problem.

0
Eyup
Telerik team
answered on 01 Dec 2015, 07:34 AM
Hi Henry,

I've already replied to your query in the following forum:
http://www.telerik.com/forums/raddropdownlist-in-radgrid-cause-datarowview-error

I suggest that we continue this new issue on the mentioned thread.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Roger
Top achievements
Rank 1
Veteran
answered on 04 Aug 2020, 04:15 PM

I had the same issue. It never worked for me using FindControl on e.Item with any cast I tried, if the command was triggered by a button in FormTemplate and the control I was looking for was in same container (just next to this button).

Finally, it worked for me by using FindControl on e.CommandSource, which holds the initiating control. You just need to cast it to actual type first (e.g. Button or LinkButton) and up you go...

Tags
Grid
Asked by
Prassin
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Prassin
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Henry
Top achievements
Rank 1
Eyup
Telerik team
Roger
Top achievements
Rank 1
Veteran
Share this question
or