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

[Solved] Viewstate of controls in CommandItemTemplate

3 Answers 214 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Edvard Pitka
Top achievements
Rank 1
Edvard Pitka asked on 29 Jun 2009, 06:04 PM
It seems like view state of the controls put in CommandItemTemplate is not maintained. Also changing any of the properties in command event handler does not have any effect on the control. Here is the grid markup

  <telerik:RadGrid ID="NotesGrid" EnableEmbeddedSkins="False" Skin="srunite" Width="100%" 
                AllowSorting="False" AllowPaging="False" AllowMultiRowSelection="False" runat="server" 
                GridLines="None" EnableViewState="true"
                <MasterTableView Width="100%" Summary="RadGrid table" AutoGenerateColumns="False" 
                    CommandItemDisplay="Top" TableLayout="Auto" GroupLoadMode="Client" AllowNaturalSort="false" 
                    AllowSorting="false"
                    <AlternatingItemStyle BackColor="#eeeeee" /> 
                    <CommandItemTemplate> 
                        <div class="gridActions"
                            Notes 
                            <asp:DropDownList ID="cmdFilterNoteType" runat="server" AutoPostBack="false" /> 
                            <asp:LinkButton ID="cmdShowAll" runat="server" AutoPostBack="true" CommandName="ShowAll" 
                                Text="Show All"  Visible="false"/> 
                            <asp:LinkButton ID="cmdShowLatest" runat="server" AutoPostBack="true" CommandName="ShowLatest" 
                                Text="Show Latest"  /> 
                        </div> 
                    </CommandItemTemplate> 


and here are event handlers

      protected void Page_Init(object sender, EventArgs e) 
        { 
            masterPage = (SiteMasterPages.BidModule)Master; 
            presenter.SetModel(masterPage.CurrentBid); 
 
 
            NotesGrid.ItemDataBound += new Telerik.Web.UI.GridItemEventHandler(NotesGrid_ItemDataBound); 
            NotesGrid.ItemCommand += new GridCommandEventHandler(NotesGrid_ItemCommand); 
        } 
 

First problem:
See how I am finding the  LinkButton and making it invisible, or assigning different text but when it renders nothing has changed. I can see it finding the control, I can debug and see that property has been changed, but it still renders these controls as if nothing has changed.  Why?

protected void NotesGrid_ItemCommand(object source, GridCommandEventArgs e)  
        {  
            if (e.CommandName == "ShowLatest")  
            {  
                presenter.ShowLatestNotes();  
                e.Item.FindControl("cmdShowLatest").Visible = false;  
                ((LinkButton)e.Item.FindControl("cmdShowLatest")).Text = "sdasdfdsaf";  
                e.Item.FindControl("cmdShowAll").Visible = true;  
            }  
            else if (e.CommandName == "ShowAll")  
            {  
                presenter.ShowAllNotes();  
                e.Item.FindControl("cmdShowAll").Visible = false;  
                e.Item.FindControl("cmdShowLatest").Visible = true;  
            }  
        }  

Second problem:
I have to rebind the dropdownlist that resides in commanditemtemplate on each postback as it does not maintain the viewstate. Why not?
  protected void NotesGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
        { 
            if (e.Item.ItemType == GridItemType.CommandItem) 
            { 
                if (!Page.IsPostBack) 
                { 
 
                    DropDownList filter = e.Item.FindControl("cmdFilterNoteType"as DropDownList; 
                    if (filter != null
                    { 
                        filter.DataSource = _notes.Select(x => x.Key); 
                        filter.DataBind(); 
 
                        filter.Items.Insert(0, new ListItem("All")); 
                    } 
                } 
            } 
            else if (e.Item.ItemType == GridItemType.GroupHeader) 
            { 
                GridTableCell cell = e.Item.Controls[0] as GridTableCell; 
                cell.Attributes.Add("noteType", ((System.Data.DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString()); 
            } 
        } 




3 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 02 Jul 2009, 04:05 PM
Hi Edvard,

Please have in mind that calling DataBind() resets the DropDownList items and selection. In order to maintain the ViewState you should remove the following line: filter.DataBind(); 

Regarding the other issue the code looks fine to me, therefore I need to test it and will perform further investigation.

All the best,
Pavlina
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.
0
Edvard Pitka
Top achievements
Rank 1
answered on 02 Jul 2009, 06:02 PM
I have there because it is not maintaing the state, so after postback my dropdown did not contain any entries. 
0
Shinu
Top achievements
Rank 2
answered on 06 Jul 2009, 06:17 AM
Hi Edvard,

Can you try binding the DropDownList in the ItemCreated event and see whether it is working fine.

Thanks
Shinu



Tags
Grid
Asked by
Edvard Pitka
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Edvard Pitka
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or