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

Access RadGrid usercontrol as editform on ItemCommand when e.CommandName == "Edit"???

12 Answers 256 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JJ
Top achievements
Rank 1
JJ asked on 15 Jan 2013, 10:30 PM
On ItemCommand when e.CommandName == "Edit" I am trying to access controls in the UserControl I am using for my EditForm.

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

Sort by
0
Shinu
Top achievements
Rank 1
answered on 16 Jan 2013, 04:55 AM
Hi,

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
0
JJ
Top achievements
Rank 1
answered on 16 Jan 2013, 03:12 PM
OK, my confusion seems to be arising from what mode I am actually in when my "Edit" button is clicked. To clarify: this is where my Edit button is being clicked:


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

Select Case e.CommandName

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" />

 

 


0
Andrey
Telerik team
answered on 21 Jan 2013, 09:45 AM
Hi,

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
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 21 Jan 2013, 01:54 PM
I want to access the usercontrol in the ItemCommand event because I want full control of all the controls therin whereby some are shown conditionally based on the data given to the usercontrol. I found a way around this however. My solution which I have not seen recommended by anyone on the Telerik or Asp.Net fora is to control the contents, the data and contained controls, of the usercontrol in the PageLoad event of the usercontrol itself. I am able to look at the [dataitem]'s properties at this point and conditionally show the controls that are relevant to it according to my requirements. If I want to know if I have an INSERT or UPDATE, I just test a property in the blank version of the dataitem I send for a New Record/Insert, e.g. I have a ID field that represents the key of the table I am inserting; I know from the contruction of the blank dataitem that that ID == 0 everytime it is sent to the usercontrol and adjust the usercontrol upon that known condition.

Thanks for your help.
0
Elliott
Top achievements
Rank 2
answered on 21 Jan 2013, 04:37 PM
um, doesn't a user control have code behind?
.ascx.vb or .ascx.cs
they would have PageLoad and PreRender event handlers

0
Andrey
Telerik team
answered on 24 Jan 2013, 11:29 AM
Hello JJ,

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
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 24 Jan 2013, 03:48 PM
Thanks for your reply. My usercontrol is so integral to the host radgrid and page that it really works for me to adjust the PageLoad of the usercontrol, also I assume changes in the usercontrol as changes happen in the page.  It works for my requirement and better for the way my code is organized on such a complex page.

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.
0
Andrey
Telerik team
answered on 29 Jan 2013, 09:58 AM
Hello,

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
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 29 Jan 2013, 01:57 PM
I see. Whenever there is a problem with a .Net control, it always comes down to the events I have found...I will try to remember when I work with a RadGrid. Thanks. Speaking of events, is there any reference for the events of a RadGrid...sort of a timeline for what happens when??
0
Andrey
Telerik team
answered on 01 Feb 2013, 02:01 PM
Hello,

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
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
Celeste
Top achievements
Rank 1
answered on 10 Oct 2013, 01:12 PM
Andrey, I am trying to resolve this same issue on post http://www.telerik.com/account/support-tickets/view-ticket.aspx?threadid=745399. Can you review and help me with it also? Thanks. Celeste
0
Pavlina
Telerik team
answered on 15 Oct 2013, 02:56 PM
Hi Celeste,

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
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 the blog feed now.
Tags
Grid
Asked by
JJ
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
JJ
Top achievements
Rank 1
Andrey
Telerik team
Elliott
Top achievements
Rank 2
Celeste
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or