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()); |
| } |
| } |