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

grid won't refresh data on postback

2 Answers 598 Views
Grid
This is a migrated thread and some comments may be shown as answers.
matt
Top achievements
Rank 1
matt asked on 07 Jul 2008, 09:24 PM
hello,

on my ASPX page I have two controls: a DropDownList, and a RadGrid. when the DropDownList posts back, I want to update the data in the RadGrid based on the list's current selected value.

sounds easy enough, but the problem -- my RadGrid seems to be caching its DataSource. it always shows its initiate data and never updates, even tho when i step thru the code-behind i can see the new data

ASPX

<!-- SELECT ROLE TO MANAGE --> 
Role to manage: <asp:DropDownList ID="ddlRoles" DataValueField="Name" DataTextField="Name" AutoPostBack="true" OnSelectedIndexChanged="ddlRoles_SelectedIndexChanged" runat="server" /> 
 
<div id="divEditUsersForm" style="margin-bottom:40px;" visible="true" runat="server"
     
    <p>Users in Role:</p> 
 
    <Telerik:RadGrid ID="gridRoleUsers"  
        Skin="Office2007" 
        GridLines="None" 
        AllowPaging="true" 
        PageSize="50"  
        PagerStyle-Mode="NextPrevAndNumeric"         
        AllowSorting="true"                  
        AutoGenerateColumns="False" 
        AllowMultiRowSelection="true"  
        AllowMultiRowEdit="true"  
        OnDeleteCommand="gridRoleUsers_DeleteCommand" 
        OnNeedDataSource="gridRoleUsers_NeedDataSource" 
        OnPreRender="grid_PreRender"  
        runat="server"
     
        <ClientSettings  
            EnableRowHoverStyle="true" 
            AllowDragToGroup="false"  
            AllowColumnsReorder="false"  
            AllowKeyboardNavigation="true" 
            ReorderColumnsOnClient="false" > 
            <Resizing AllowColumnResize="false" AllowRowResize="false" /> 
            <Selecting AllowRowSelect="true" EnableDragToSelectRows="true" /> 
        </ClientSettings> 
         
        <MasterTableView  
            CommandItemDisplay="Top"  
            DataKeyNames="Username"  
            EditMode="InPlace" 
            NoMasterRecordsText="No users to display."  
            CommandItemStyle-Font-Bold="true"  
            HeaderStyle-ForeColor="#191970"  
            ItemStyle-ForeColor="black"  
            AlternatingItemStyle-ForeColor="black"
             
            <%-- use for support for multi edit/deletes > --%> 
            <CommandItemTemplate> 
                <div style="padding:3px 0px;"
                    <asp:LinkButton ID="btnDelete" CommandName="DeleteSelected" OnClientClick="javascript:return confirm('Remove all selected Users?')" CausesValidation="false" Visible='<%# !gridRoleUsers.MasterTableView.IsItemInserted && gridRoleUsers.EditIndexes.Count == 0 %>' CssClass="commandItem" runat="server"><img src="/images/delete.gif" width="16" height="16" border="0"/> Remove Selected Users</asp:LinkButton> 
                </div> 
            </CommandItemTemplate> 
             
            <Columns> 
                <Telerik:GridClientSelectColumn HeaderStyle-Width="24" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" /> 
                <Telerik:GridButtonColumn CommandName="Delete" ButtonType="LinkButton" Text="Remove" UniqueName="deleteColumn" ConfirmText="Remove user from Role?" ConfirmTitle="Delete" HeaderStyle-Width="40" ItemStyle-ForeColor="#333333" ItemStyle-Font-Bold="True" /> 
                <Telerik:GridBoundColumn HeaderText="Username" UniqueName="Username" DataField="Username" HeaderStyle-Wrap="false" ReadOnly="true" /> 
                <Telerik:GridBoundColumn HeaderText="Role Name" UniqueName="RoleName" DataField="RoleName" HeaderStyle-Wrap="false" ReadOnly="true" /> 
            </Columns> 
 
        </MasterTableView>               
    </Telerik:RadGrid>   
</div> 
 
 
 
<Telerik:RadAjaxManager ID="radAjaxManager" runat="server"
    <AjaxSettings> 
        <%-- grid updates itself --%> 
        <Telerik:AjaxSetting AjaxControlID="gridRoleUsers"
            <UpdatedControls> 
                <Telerik:AjaxUpdatedControl ControlID="gridRoleUsers" LoadingPanelID="ajaxLoadingPanel" /> 
                <Telerik:AjaxUpdatedControl ControlID="lblMessage" /> 
            </UpdatedControls> 
        </Telerik:AjaxSetting> 
    </AjaxSettings> 
</Telerik:RadAjaxManager> 
 
<Telerik:RadAjaxLoadingPanel ID="ajaxLoadingPanel" Width="75px" Height="75px" Transparency="10" runat="server"
    <img alt="Loading..." src='<%= RadAjaxLoadingPanel.GetWebResourceUrl(Page, "Telerik.Web.UI.Skins.Default.Ajax.loading.gif") %>' style="border:0;" /> 
</Telerik:RadAjaxLoadingPanel> 


C#

protected void Page_Load(object sender, EventArgs e) 
    //set header's help URL 
    AutomaticVehicleLocator.controls.nav.Header ucHeader = (AutomaticVehicleLocator.controls.nav.Header)this.Master.FindControl("ucHeader"); 
    ucHeader.HelpUrl = "~/helpUserManager.aspx"
 
    if (!IsPostBack) 
    { 
        ddlRoles.DataSource = GetRolesTable(); 
        ddlRoles.DataBind(); 
    } 
 
    _role = ddlRoles.SelectedValue; 
     
 
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
 
 
protected void ddlRoles_SelectedIndexChanged(object sender, EventArgs e) 
    //_role = ddlRoles.SelectedValue; 
 
    SetRoleUsersGridDataSource(); 
 
    divEditUsersForm.Visible = true
    //litRoleName.Text = _role; 
 
/// <summary> 
/// Fired when grid needs a datasource. eg, after editing or deleting. 
/// </summary> 
protected void gridRoleUsers_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    SetRoleUsersGridDataSource(); 
 
private void SetRoleUsersGridDataSource() 
    gridRoleUsers.DataSource = null
 
    if (Strings.Exists(_role)) 
    { 
        // This event fails if a databind is specified - it has to do its own databind. 
        gridRoleUsers.DataSource = GetUsersForRole(_role); 
    } 


any idea whats going on here?


thanks!
matt

2 Answers, 1 is accepted

Sort by
0
Accepted
Veli
Telerik team
answered on 08 Jul 2008, 02:29 PM
Hi Matt,

Let me make a few remarks on the sample code:

When working with NeedDataSource, you should not explicitly call DataBind() method for RadGrid. Please remove the if(!IsPostBack) block from Page_Load.

In the SelectedIndexChanged event of the dropdownlist, set only your _role variable to the selected value, and rebind radGrid (RadGrid.Rebind()). Calling SetRoleUsersGridDataSource() instead does not ensure that RadGrid is rebound to its new data, therefore you do not have your page updated.

Fixing these things should make RadGrid work for you now.

All the best,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
matt
Top achievements
Rank 1
answered on 08 Jul 2008, 03:54 PM
well, the if(!IsPostBack) block is actually for a DropDownList, not a RadGrid so that should be fine.

but the other change -- using gridRoleUsers.Rebind()
-- fixed it.

thanks!
matt
Tags
Grid
Asked by
matt
Top achievements
Rank 1
Answers by
Veli
Telerik team
matt
Top achievements
Rank 1
Share this question
or