I tried to use:
Dim uc As UserControl =
CType(e.Item.FindControl(GridEditFormItem.EditFormUserControlID), UserControl)
where "GridEditFormItem.EditFormUserControlID" is the UserControl. (I know to try this because it works when I use it On ItemCommand when e.CommandName == "Update")
This line, however, comes up with uc=Nothing when I attempt to use it while e.CommandName == "Edit". Is the usercontrol called something besides "GridEditFormItem.EditFormUserControlID" when e.CommandName == "Edit"???
The usercontrol seems to be called something else when e.CommandName == "Edit" vs e.CommandName == "Update" and yet it is still within the ItemCommand event. Does anyone know how I can access the same UserControl OnItemCommand while e.CommandName == "Edit"??
12 Answers, 1 is accepted
With reference to this forum thread, 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.
C#:
protected
void
RadGrid1_ItemCreated(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridEditFormItem && e.Item.IsInEditMode)
{
GridEditFormItem item = (GridEditFormItem)e.Item;
UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
}
}
Thanks,
Shinu
<Columns>
...<telerik:GridEditCommandColumn EditText="Edit" UniqueName="rg_column_edit" />
...
</Columns>
To test what mode I am in when this Edit btn is clicked, I set the following in OnItemCommand:
Protected Sub rg_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs)
Case "Edit" 'Edit link is clicked
lbl_test_modes.Text = _
"Edit clicked: " & e.CommandName & _
"<br/>IsInEditMode: " & e.Item.IsInEditMode.ToString & _"<br/>IsDataBound: " & e.Item.IsDataBound.ToString
End Sub
When I click the btn on the UI after building I get this result in the lbl_test_modes.Text:
Edit clicked: Edit
IsInEditMode: False
IsDataBound: True
So I am really confused why 1) e.Item.IsInEditMode.ToString is False; and 2) the e.CommandName is "Edit" after I click on the "Edit" btn (???) What mode am I actually in and how would I access the UserControl I am using for editing when I click the Edit button I have set in:
<telerik:GridEditCommandColumn EditText="Edit" UniqueName="rg_column_edit" />
Why you need to access the user control in the ItemCommand event of RadGrid? If you need data from the input controls located in the UserControl it is better to use the OnUpdateCommand event. Through the event arguments of this event you could get a reference to edited item and access the input controls and their values.
A working sample of this approach could be found in this help topic. Give it a try and check whether the issue still replicates.
All the best,
Andrey
the Telerik team
Thanks for your help.
.ascx.vb or .ascx.cs
they would have PageLoad and PreRender event handlers
Putting your code in the Page_Load event handler of the user control is not recommended because if you change the page in which you use the user control you will need to modify user control's code in order to match the new scenario.
Since you want to conditionally show/hide controls from the user control and you want this condition to be based on the value you receive from the database the best time to do this is within the ItemDataBound event of RadGrid. Thus you won't need to specifically customize your code for a given scenario. In the ItemDataBound event you could find all the controls from the user control and set their VIsible properties to be dependent on the values from the datasource.
You could check this help topic for more information on how to access different controls in the ItemDataBound event of RadGrid.
Kind regards,
Andrey
the Telerik team
But to your points, how would I distinguish the isEdit mode in the DataBound event when my Edit button is clicked:
<
telerik:GridEditCommandColumn EditText="Edit" UniqueName="rg_column_edit" />
When I checked the e.Item.IsInEditMode property on ItemCreated when this Edit button is clicked, I kept getting False.
Yes, if you check the property on ItemCreated it will return false value, that is why I suggested to use the ItemDataBound event instead. ItemCreated event is raised too early and the item has not been set to be in edit mode yet. On the other hand ItemDataBound event is raised just after all the items are created and put in edit mode and are ready to be bound.
You could check this help topic for an example how to access controls when the items is in edit mode.
Greetings,
Andrey
the Telerik team
Yes, doing the right thing at the right time is important for getting correct results.
About your other question you could check this help topic and the rest of the same category for a comprehensive explanation of how the control work.
Greetings,
Andrey
the Telerik team
I checked the pointed post and noticed that it is already answered by my colleague Kostadin. If you have any further questions or problems related to post I would like to ask you to continue your communication there.
Regards,
Pavlina
Telerik