Find a control of an edit form in item command event

8 posts, 1 answers
  1. William
    William avatar
    30 posts
    Member since:
    Oct 2008

    Posted 23 Feb 2009 Link to this post

    Hi,

    I have a grid with an edit form with textboxes that I need to disable based on a button that is clicked in the form. I'm wondering if there is a way to find the textboxes in the itemcommand events that get triggered by the buttons?

    I've been trying to use something like:

     

    Private Sub RadGrid2_ItemCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid2.ItemCommand

     

     

    If e.CommandName = "TimeInOutTextboxes" Then

     

     

    Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)

     

     

    Dim txtTimeIn As TextBox = DirectCast(editedItem.FindControl("txtTimeIn"), TextBox)

     

    txtTimeIn.Enabled =

    False

     

     

    End If

    but an error is thrown at "txtTimeIn.Enabled = False" because the control is not being found

    Thanks


     

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 23 Feb 2009 Link to this post

    Hi William,

    Try the following code snippets and see whether its working fine for you.

    VB:
    Protected Sub RadGrid1_ItemCommand(ByVal source As ObjectByVal e As GridCommandEventArgs) 
        If e.CommandName = "TimeInOutTextboxes" Then 
            Dim editedItem As GridEditableItem = DirectCast(e.Item, GridEditableItem) 
            Dim txtTimeIn As TextBox = DirectCast(DirectCast((editedItem), Telerik.Web.UI.GridDataItem).EditFormItem.FindControl("txtTimeIn"), TextBox) 
            txtTimeIn.Enabled = False 
        End If 
    End Sub 

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. William
    William avatar
    30 posts
    Member since:
    Oct 2008

    Posted 24 Feb 2009 Link to this post

    Thanks a lot Shinu, but I'm getting an error in this line?:
    DirectCast
    (DirectCast((editedItem), Telerik.Web.UI.GridDataItem).EditFormItem.FindControl("txtTimeIn"), TextBox)

    The error is :
    'Telerik.WebControls.GridEditableItem' cannot be converted to 'Telerik.Web.UI.GridDataItem'.

    Thanks again,
     William
  5. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Feb 2009 Link to this post

    Hi William,

    Try the below code snippets for accesing the textbox inside form template.

    VB:
    Protected Sub RadGrid1_ItemCommand(ByVal source As ObjectByVal e As GridCommandEventArgs)  
        If e.CommandName = "TimeInOutTextboxes" Then 
            Dim editedItem As GridEditableItem = DirectCast(TryCast((e.CommandSource), Button).NamingContainer, GridEditableItem)  
            Dim txtName As TextBox = DirectCast(editedItem.FindControl("txtTimeIn"), TextBox)  
        End If 
    End Sub 

    Thanks,
    Princy.
  6. William
    William avatar
    30 posts
    Member since:
    Oct 2008

    Posted 24 Feb 2009 Link to this post

    That works great Princy. Thanks! 
  7. srinivas
    srinivas avatar
    1 posts
    Member since:
    Jun 2011

    Posted 10 Dec 2011 Link to this post

    Can you post the same code in c#
  8. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 11 Dec 2011 Link to this post

    Hello Srinivas,

    Here is the sample code in c#.
    c#:
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
     if (e.CommandName == "TimeInOutTextboxes")
      {
        GridEditableItem editedItem = (GridEditableItem)((e.CommandSource) as Button).NamingContainer;
        TextBox txtName = (TextBox)editedItem.FindControl("txtTimeIn");
       }
    }
    Here is the link for code converter.
    http://converter.telerik.com/

    -Shinu.
  9. Beth
    Beth avatar
    1 posts
    Member since:
    Mar 2013

    Posted 27 Sep in reply to Shinu Link to this post

    Thank you, your code helped me a great deal!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017