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

Changing columns visiblity via Ajax

1 Answer 40 Views
Grid
This is a migrated thread and some comments may be shown as answers.
StephenWRogers
Top achievements
Rank 1
StephenWRogers asked on 11 Jul 2008, 11:28 AM
I've a problem with showing and hiding a column via an ajax request.  When I set the column's Visible property to false, the column is hidden, but when set back to true the column does not reappear.

Try the following page

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <script type="text/javascript"
        function firePostBack(arg) 
        { 
            $find('<%=ajaxManager.ClientID %>').ajaxRequest(arg); 
        } 
    </script> 
    <form id="form1" runat="server"
        <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
        <div> 
            <telerik:RadAjaxManager OnAjaxRequest="ajaxManager_AjaxRequest" ID="ajaxManager" runat="server"
                <AjaxSettings> 
                    <telerik:AjaxSetting AjaxControlID="ajaxManager"
                        <UpdatedControls> 
                            <telerik:AjaxUpdatedControl ControlID="grid" /> 
                        </UpdatedControls> 
                    </telerik:AjaxSetting> 
                </AjaxSettings> 
            </telerik:RadAjaxManager> 
        </div> 
         
        <telerik:RadGrid runat="server" ID="grid" OnNeedDataSource="grid_NeedDataSource"
             
            <MasterTableView AutoGenerateColumns="false"
                <Columns> 
                    <telerik:GridBoundColumn DataField="col1" HeaderText="Col1"></telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="col2" HeaderText="Col1"></telerik:GridBoundColumn> 
                </Columns> 
            </MasterTableView> 
         
        </telerik:RadGrid> 
 
 
        <href="#" onclick="firePostBack('1');return false;">Fire</a> 
    </form> 
</body> 
</html> 
 

C#

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Net; 
using System.Collections.Generic; 
using Telerik.Web.UI; 
 
public partial class _Default : System.Web.UI.Page  
 
 
    protected void grid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) 
    { 
        DataTable table = new DataTable(); 
        table.Columns.Add("col1"); 
        table.Columns.Add("col2"); 
         
        table.Rows.Add("data1""data2"); 
        table.Rows.Add("data3""data4"); 
        table.Rows.Add("data5""data6"); 
 
        grid.DataSource = table; 
    } 
 
    protected void ajaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e) 
    { 
        grid.Columns.FindByUniqueName("col2").Visible = !grid.Columns.FindByUniqueName("col2").Visible; 
    } 

When you click the fire button an ajax request is fired and the second column's visiblity is toggled.

The first click hides the column, which is fine, but the second click doesn't show it again..

Any ideas?

Thanks,

Stephen



1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 11 Jul 2008, 12:19 PM
Hello Stephen,

Please note that after hiding a column on the server, RadGrid needs to be rebound to show it back. Therefore you need to call grid.Rebind() in the AjaxRequest event handler.

Sincerely yours,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
StephenWRogers
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or