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

[Solved] Optimize ViewState when using FilterTemplate

1 Answer 183 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bob
Top achievements
Rank 1
Bob asked on 09 Jul 2014, 03:30 PM
We need to optimize Viewstate within our grids for best performance.  Since we are using NeedsDataSource it seemed we would not need to use Viewstate so we tried to disable viewstate on the grid.  

However, we are using FilterTemplate on some of the grid columns.  The FilterTemplate includes a RadComboBox loaded during grid ItemDataBound.

Upon client side selection of the filter RadComboBox, we postback to requery and rebind the grid.  The grid is within an updatepanel.

This works, however, the RadComboBox within the FilterTemplate does not keep its state even if we set EnableViewState="true" and ViewStateMode="Enabled" on the RadComboBox.

In order to get the RadComboBox within the FilterTemplate to remember the selection, we have to set the entire grid EnableViewState="true" and ViewStateMode="Enabled".

Can we disable viewstate on the grid but still enable viewstate within the FilterTemplate?

Thanks...Bob Baldwin
Trabon Solutions

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 10 Jul 2014, 09:10 AM
Hi Bob,

The value of the filter input control is not persisted if ViewState is disabled. One suggestion is you can store the value in session and set it to the RadComboBox in its OnPreRender event.

ASPX:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" ScriptMode="Release" LoadScriptsBeforeUI="true">
</asp:ScriptManager>
 
<telerik:GridBoundColumn DataField="ShipCountry" UniqueName="ShipCountry" HeaderText="ShipCountry" AutoPostBackOnFilter="true">
    <FilterTemplate>
        <telerik:RadComboBox ID="rcbCountry" runat="server" DataValueField="ShipCountry" AppendDataBoundItems="true" AllowCustomText="true" DataTextField="ShipCountry" SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("ShipCountry").CurrentFilterValue %>' OnClientSelectedIndexChanged="categoryIndexChanged" DataSourceID="dsShipCountry" OnPreRender="rcbCountry_PreRender" >
            <Items>
                <telerik:RadComboBoxItem Text="All" />
            </Items>
        </telerik:RadComboBox>
        <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
            <script type="text/javascript">
              function categoryIndexChanged(sender, args) {              
                var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                tableView.filter("ShipCountry", args.get_item().get_value(), "EqualTo");
                PageMethods.CreateSessionViaJavascript(args.get_item().get_value());
                }
            </script>
        </telerik:RadScriptBlock>
    </FilterTemplate>
</telerik:GridBoundColumn>

C#:
protected void rcbCountry_PreRender(object sender, EventArgs e)
{
    RadComboBox rcbCountry = sender as RadComboBox;
    if (Session["SelectedID"] != null)
    {
        string value = Session["SelectedID"].ToString();
        if (value != "")
        {
            rcbCountry.SelectedValue = value;
        }
    }
}
[System.Web.Services.WebMethod]
public static string CreateSessionViaJavascript(string selectedId)
{
    Page objp = new Page();
    objp.Session["SelectedID"] = selectedId;
    return selectedId;
}

Thanks,
Princy
Tags
Grid
Asked by
Bob
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or