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

Find UserControl in RadGrid on EditCommand

14 Answers 594 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JJ
Top achievements
Rank 1
JJ asked on 30 Oct 2012, 06:10 PM
I have a user control which I am using for Edits and Inserts. The usercontrol has properties on it that need to be set when rendered or when the editcommand is issued. I am trying to find the usercontrol in the RadGrid so that I can set its properties before it is shown in the edittemplate. The properties that need to be set change the user controls appearence when it is shown and the way it processes data, so the properties have to be set before data is bound....I have tried many events (the EditCommand follows) but the uc cannot be found and is allways Nothing/null.

 

 

Private Sub rg_EditCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles rg.EditCommand

 

 

 

    Dim uc As UserControl = CType(e.Item.FindControl(GridEditFormItem.EditFormUserControlID), UserControl)

 

    lbl_test.Text =

 

CType(uc, uc_Steps_Party).ID 'this is where the error occurs because uc == Nothing

 

 

 

End Sub

 

I have also tried:
EditCommand

ItemDataBound

ItemCreated

ItemCommand

Always a null exception refering tot he usercontrol.

The Telerick examples seem to only show finding the usercontrol (after) it has renedered like in the UpdateCommand or InsertCommand. I need to find the user control and set its properties before it gets to those events, when the usercontrol is first shown.


14 Answers, 1 is accepted

Sort by
0
Casey
Top achievements
Rank 1
answered on 30 Oct 2012, 07:52 PM
Hi JJ,

You should be able to find out if the RadGrid is in edit or insert mode in the Page Load (or other event) of the User Control with the following:


((GridEditableItem)this.NamingContainer).OwnerTableView.IsItemInserted

If this condition is true, then you know that this.NamingContainer is a GridEditFormInsertItem, otherwise it would be a GridEditFormItem.

I hope this helps!
Casey
0
JJ
Top achievements
Rank 1
answered on 30 Oct 2012, 08:52 PM
That does not answer my question, but thanks.

I do not need to know what mode the radgrid is in. I need to be able to configure the custom properties on the usercontrol before it is show in the editmode. I have to find the usercontrol, but do not know at what event the usercontrol can be found before the EditCommand is fired.

Someone else please help.
0
Casey
Top achievements
Rank 1
answered on 31 Oct 2012, 12:12 PM
Hi JJ,

I wasn't intending to only give you code to determine what mode the radgrid was in, I thought you wanted to do something to the controls inside of your user control based on the fact that the RadGrid was in edit mode. Now that I understand your need, I was able to step through the ItemDataBound event of the RadGrid to determine when the UserControl was showing. Hopefully the below code meets your needs.

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditFormItem && !RadGrid1.MasterTableView.IsItemInserted)
        {
            GridEditFormItem editItem = e.Item as GridEditFormItem;
            UserControl yourUC = ((UserControl)((GridEditFormItem)e.Item).FindControl("EditFormControl"));
            if (yourUC != null)
            {
                //your code here
            }
        }
}
0
JJ
Top achievements
Rank 1
answered on 31 Oct 2012, 01:25 PM
Mr. Casey, thanks so much for your help; that in fact worked for me.

It seems a pretty convoluted answer for such a simple and what I would think is a not uncommon task. It seems like you should have been able to configure the custom properties of a user control somewhere here:

 

 

<EditFormSettings EditFormType="webUserControl" UserControlName="~/UserControls/MyUserControl.ascx">

 

 

 

 

</EditFormSettings>

If anyone understands the problem and has a more elegant solution please post it.

 

0
Casey
Top achievements
Rank 1
answered on 31 Oct 2012, 01:33 PM
Hi JJ,

You're welcome, I'm glad that the solution worked for you!

Out curiosity, what properties are you wanting to set for the user control? It might help me, or others, to be able to provide a different solution to your problem.

Thanks!
Casey
0
JJ
Top achievements
Rank 1
answered on 31 Oct 2012, 01:38 PM
Casey, the properties I have to set on the UserControl are just label and link text properties, e.g.:

 

 

Private _Header As String = Nothing

 

 

 

Public Property Header() As String

 

 

 

Get

 

 

 

Return lit_header.Text 'This is just a title in the body of the usercontrol that changes in other iterations
End Get

 

 

 

Set(ByVal value As String)

 

 

 

Me._Header = value

 

lit_header.Text = _Header

 

 

End Set

 

 

 

End Property

Nothing special, purely asethetic, but if I had to do more sophisticated stuff per the UserControl properties, finding the UserControl in the right place before it is rendered in the edittemplate is essential.

 

0
Casey
Top achievements
Rank 1
answered on 31 Oct 2012, 01:45 PM
Is there a reason you want to set the properties in the parent page events, as opposed to in the events of the controls in the user control? 
0
JJ
Top achievements
Rank 1
answered on 31 Oct 2012, 02:47 PM
The properties of the usercontrol supercede the functionality of the usercontrol, so I have to set them in the host page where I know how I want to use the particular interation of the usercontrol.
0
JJ
Top achievements
Rank 1
answered on 31 Oct 2012, 08:47 PM
Casey, new problem. I cannot find the usercontrol at ItemCommand.

I tried:

Dim uc As UserControl = CType(CType(e.Item, GridCommandItem).FindControl("EditFormControl"), UserControl)

0
Radoslav
Telerik team
answered on 02 Nov 2012, 08:21 AM
Hello JJ,

To achieve the desired functionality you could try getting the user control into the RadGrid1_ItemCreated event. For example:
Protected Sub RadGrid1_ItemCreated(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemCreated
      If e.Item.IsInEditMode Then
          Dim userControl As EditFormControl = CType(e.Item.FindControl(GridEditFormItem.EditFormUserControlID), EditFormControl)
          userControl.MyCustomProperty = "MyCustomProperty"
      End If
  End Sub

Additionally I am sending you a simple example. Please check it out and let me know if it helps you.

Regards,
Radoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
JJ
Top achievements
Rank 1
answered on 02 Nov 2012, 01:06 PM
I need to access my usercontrol after ItemCreated (in ItemCommand) so your example does not work for me.

Again, (ItemCommand) is where I need to access my usercontrol.

Are you saying that I can definitivly not access a usercontrol in the ItemCommand event??? Please confirm.
0
Casey
Top achievements
Rank 1
answered on 02 Nov 2012, 01:44 PM
JJ,

I'm pretty sure the UserControl can't be accessed in the ItemCommand event, for Edit/InitInsert commands. Do you need to set the label and link text properties based on the command name being edit vs. insert? 
0
Radoslav
Telerik team
answered on 07 Nov 2012, 07:55 AM
Hi JJ,

You could not access the user control from the RadGrid’s edit form on RadGrid.ItemCommand event. The RadGrid.ItemCommand event is fired before ItemCreated event, so into the ItemCommand event the grid has not created the user control yet. The first event where you could access your user control is ItemCreated.

I hope this helps.

All the best,
Radoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
reguapo
Top achievements
Rank 1
answered on 20 Mar 2014, 08:57 PM
I know is late but still I was having the same problem and this post help me, and here is a better graphic on the radgrid events order, and that helps a little bit to understand the above answers:
(found this in radgrid event sequence)

On edit/update/insert/delete action or paging/sorting/grouping/filtering operation
For each Item:
ItemCreated
Page.Load
Grid_Instance.ItemCommand
Grid_Instance.EditCommand/UpdateCommand/InsertCommandorGridInstance.PageIndexChanged/SortCommand/GroupsChanging/ItemCommand
Grid_Instance.NeedDataSource
For each Item:
ItemCreated
ItemDataBound
Page.PreRender
----
Invoking the Rebind() method from postback event handler of outside control or RadGrid will raise automatically the NeedDataSource event
Tags
Grid
Asked by
JJ
Top achievements
Rank 1
Answers by
Casey
Top achievements
Rank 1
JJ
Top achievements
Rank 1
Radoslav
Telerik team
reguapo
Top achievements
Rank 1
Share this question
or