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

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

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.

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
}
}
}

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.

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

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.



I tried:
Dim uc As UserControl = CType(CType(e.Item, GridCommandItem).FindControl("EditFormControl"), UserControl)
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

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.

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?
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

(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