Getting Control Values from CommandItemTemplate when EnableViewState=False

3 posts, 0 answers
  1. Dennis
    Dennis avatar
    62 posts
    Member since:
    Oct 2007

    Posted 15 Apr 2009 Link to this post

    Version 2009.1.311.35

    I have a RadGrid in a Web UserControl that is loaded into another Page. In that Grid, I have defined a custom CommandItemTemplate that contains a RadComboBox for selection and a ASP Button to execute (which causes a postback calling the OnItemCommand event of the Grid).

    I fill the contents of the RadComboBox during the OnItemCreated event of the Grid. When EnableViewState is true on the Grid, I have no problem reading the value of the selection of the RadComboBox through the OnItemCommand event of the Grid, but when set to false, I can never get the value of the RadComboBox because it is reset to default.

    At what point and how would I get the value of the RadComboBox when EnableViewState is false? Also, I seem to only be able to reference the controls in the CommandItemTemplate during the OnItemCreated and OnItemCommand events of the Grid. Is there way to reference these controls during any other time (e.g. Page_Load)?
  2. Georgi Krustev
    Georgi Krustev avatar
    3725 posts

    Posted 21 Apr 2009 Link to this post

    Hello Dennis,

    Unfortunately the behavior you are trying to achieve is not possible. When the EnableViewState property of the grid is set to "false", it is re-created every time when a PostBack is initiated. This means the inner items are re-created too. The RadGrid items are created when ItemCreated event is raised and populated with data when ItemDataBound event is fired. You cannot retrieve the RadComboBox in the Page_Load event handler, because the control is not created yet. Please review this online help article which is devoted on RadGrid’s lifecycle.
    I will suggest you put the RadComboBox control outside of the grid. Thus you can attain the functionality you are searching for.

    Kind regards,
    Georgi Krustev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Dennis
    Dennis avatar
    62 posts
    Member since:
    Oct 2007

    Posted 21 Apr 2009 Link to this post

    I was able to get it to work using a couple of pieces.

    1. Saving the SelectedValue of the ComboBox into the Session. This would allow me to set the SelectedValue during the Grid.OnItemCreated (if I wanted a default or reset the appropriate between postbacks) when I created the value list.

    2. Get the ComboBox SelectedValue during the Grid.OnNeedDataSource when the GridRebindReason is "PostbackViewStateNotPersisted". I had to drill through the Controls collection to manually find the appropraite control, but it did house the SelectedValue and allow me to save it into the Session.

    3. Using the Grid.OnItemCommand, I picked up the button click that was also in the Grid CommandItem Template using the CommandName argument. I then retreived the SelectedValue that was stored in the Session state during the "PostbackViewStateNotPersisted" rebind step for processing.

    I don't know if this is the recommended way to do this, but it did work.


Back to Top