Find control in radgrid item command

13 posts, 0 answers
  1. Prassin
    Prassin avatar
    87 posts
    Member since:
    Jun 2012

    Posted 02 Oct 2012 Link to this post

    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
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 02 Oct 2012 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Prassin
    Prassin avatar
    87 posts
    Member since:
    Jun 2012

    Posted 02 Oct 2012 Link to this post

    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
  5. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 02 Oct 2012 Link to this post

    Hello,

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

    So, we can help you to solve your issue.

    Thanks,
    Jayesh Goyani
  6. Prassin
    Prassin avatar
    87 posts
    Member since:
    Jun 2012

    Posted 03 Oct 2012 Link to this post

    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
  7. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 03 Oct 2012 Link to this post

    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.
  8. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 03 Oct 2012 Link to this post

    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
  9. Henry
    Henry avatar
    4 posts
    Member since:
    Nov 2015

    Posted 18 Nov 2015 in reply to Jayesh Goyani Link to this post

    why my e.item is always null? 
  10. Eyup
    Admin
    Eyup avatar
    3007 posts

    Posted 23 Nov 2015 Link to this post

    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
  11. Henry
    Henry avatar
    4 posts
    Member since:
    Nov 2015

    Posted 24 Nov 2015 in reply to Eyup Link to this post

    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

  12. Eyup
    Admin
    Eyup avatar
    3007 posts

    Posted 26 Nov 2015 Link to this post

    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
  13. Henry
    Henry avatar
    4 posts
    Member since:
    Nov 2015

    Posted 26 Nov 2015 in reply to Eyup Link to this post

    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.

  14. Eyup
    Admin
    Eyup avatar
    3007 posts

    Posted 01 Dec 2015 Link to this post

    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017