RadGrid is a complex ASP.NET control and like with the Microsoft MS GridView sometimes requires additional coding when its table structure should be deactivated client/server side (due to the nested control definitions and features switched on).
Client-side
When you want to enable/disable your grid client-side when certain conditions are met, you will need to take into account that you should disable the active links/images/inputs/spans/etc. inside the grid manually. To do that, you will need to traverse the elements inside the table structure and perform one of the actions outlined below:
- set the disabled attribute of the corresponding html element
- clear the href property of anchor tags
- return false from the onclick handler of images/links/buttons if attached
In addition, you can disable the keyboard navigation of the grid and the scrolling option along with some of the client features which has been enabled server-side. Later on, in order to enable the grid again and allow the user to interact with it, simply perform an ajax request (to simulate client-side behavior) - thus the grid will undo the changes made client-side automatically and will be functional again.
The code below demonstrates how to enable/disable grid instance from external html buttons:
| ASPX/ASCX |
Copy Code |
|
<script type="text/javascript"> var gridCtrl;
function GridCreated() { gridCtrl = window["<%=RadGrid1.ClientID %>"]; } function KeyPressed(key) { if(gridCtrl.Control.disabled) { return false; } } function DisableGrid() { gridCtrl.Control.disabled = "disabled";
gridCtrl.ClientSettings.Selecting.AllowRowSelect = false; gridCtrl.ClientSettings.Resizing.AllowColumnResize = false; gridCtrl.ClientSettings.Resizing.AllowRowResize = false;
gridCtrl.ClientSettings.AllowColumnsReorder = false; gridCtrl.ClientSettings.AllowDragToGroup = false;
gridCtrl.ClientSettings.EnablePostBackOnRowClick = false;
var links = gridCtrl.Control.getElementsByTagName("a"); var images = gridCtrl.Control.getElementsByTagName("img"); var inputs = gridCtrl.Control.getElementsByTagName("input"); var sortButtons = gridCtrl.Control.getElementsByTagName("span");
for(var i = 0; i < links.length; i++) { links[i].href = ""; links[i].onclick = function(){ return false; } } for(var i = 0; i < images.length; i++) { images[i].onclick = function(){ return false; } } for(var i = 0; i < sortButtons.length; i++) { sortButtons[i].onclick = function(){ return false; } } for(var i = 0; i < inputs.length; i++) { switch(inputs[i]. type) { case "button": inputs[i].onclick = function(){ return false; } break; case "checkbox": inputs[i].disabled = "disabled"; break; case "radio": inputs[i].disabled = "disabled"; break; case "text": inputs[i].disabled = "disabled"; break; case "password": inputs[i].disabled = "disabled"; break; case "image": inputs[i].onclick = function(){ return false; } break; case "file": inputs[i].disabled = "disabled"; break; default: break; } } var scrollArea = document.getElementById(gridCtrl.ClientID + "_GridData");
if(scrollArea) { scrollArea.disabled = "disabled"; } } function EnableGrid() { window[ "<%=RadGrid1.ClientID %>"].AjaxRequest("<%=RadGrid1.UniqueID%>", ""); } </script>
<rad:RadGrid ID="RadGrid1" DataSourceID="AccessDataSource1" runat="server" Skin="Outlook" Width= "95%" AutoGenerateColumns="False" PageSize="10" AllowSorting="True" AllowPaging="True" GridLines= "None" EnableAJAX="true" ShowGroupPanel="true" ShowStatusBar="true"> <PagerStyle Mode="NumericPages"></PagerStyle> <MasterTableView DataSourceID="AccessDataSource1" DataKeyNames="CustomerID" AllowMultiColumnSorting="True" Width= "100%" AllowFilteringByColumn="true"> <DetailTables> <rad:GridTableView DataKeyNames="OrderID" DataSourceID="AccessDataSource2" Width="100%" runat= "server" AllowFilteringByColumn="true"> <ParentTableRelation> <rad:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID" /> </ParentTableRelation> <Columns> <rad:GridEditCommandColumn UniqueName="EditCommandColumn" /> <rad:GridBoundColumn SortExpression="OrderID" HeaderText="OrderID" HeaderButtonType="TextButton" DataField= "OrderID" UniqueName="OrderID"> </rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="OrderDate" HeaderText="Date Ordered" HeaderButtonType="TextButton" DataField= "OrderDate" UniqueName="OrderDate"> </rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="Freight" HeaderText="Freight" HeaderButtonType="TextButton" DataField= "Freight" UniqueName="Freight"> </rad:GridBoundColumn> <rad:GridButtonColumn UniqueName="DeleteColumn" CommandName="Delete" ButtonType="ImageButton" ImageUrl= "RadControls/Grid/Skins/Orange/Delete.gif" /> </Columns> </rad:GridTableView> </DetailTables> <Columns> <rad:GridClientSelectColumn /> <rad:GridBoundColumn SortExpression="CustomerID" HeaderText="CustomerID" HeaderButtonType="TextButton" DataField= "CustomerID" UniqueName="CustomerID"> </rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="ContactName" HeaderText="Contact Name" HeaderButtonType="TextButton" DataField= "ContactName" UniqueName="ContactName"> </rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="CompanyName" HeaderText="Company" HeaderButtonType="TextButton" DataField= "CompanyName" UniqueName="CompanyName"> </rad:GridBoundColumn> <rad:GridButtonColumn UniqueName="DeleteColumn" CommandName="Delete" ButtonType="ImageButton" ImageUrl= "RadControls/Grid/Skins/Orange/Delete.gif" /> </Columns> </MasterTableView> <ClientSettings AllowColumnsReorder="true" AllowDragToGroup="true" AllowKeyboardNavigation="true" EnablePostBackOnRowClick= "true"> <Resizing AllowColumnResize="true" EnableRealTimeResize="true" /> <Selecting AllowRowSelect="true" /> <ClientEvents OnKeyPress="KeyPressed" OnGridCreated="GridCreated" /> <Scrolling AllowScroll="true" UseStaticHeaders="true" ScrollHeight="200px" /> </ClientSettings> </rad:RadGrid> <asp:AccessDataSource ID="AccessDataSource1" DataFile="~/Grid/Data/Access/Nwind.mdb" SelectCommand="SELECT * FROM Customers" runat="server"></asp:AccessDataSource> <asp:AccessDataSource ID="AccessDataSource2" DataFile="~/Grid/Data/Access/Nwind.mdb" SelectCommand="SELECT * FROM Orders Where CustomerID = ?" runat="server"> <SelectParameters> <asp:SessionParameter Name="CustomerID" SessionField="CustomerID" Type="string" /> </SelectParameters> </asp:AccessDataSource> <br /> <input id="btnClientDisable" type="button" value="Disable grid" onclick="DisableGrid()" /> <input id="btnEnable" type="button" value="Enable grid" onclick="EnableGrid()" /> |
| C# |
Copy Code |
|
protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument) { base.RaisePostBackEvent(sourceControl, eventArgument); } |
| VB.NET |
Copy Code |
|
Protected Overloads Overrides Sub RaisePostBackEvent(ByVal sourceControl As IPostBackEventHandler, ByVal eventArgument As String) MyBase.RaisePostBackEvent(sourceControl, eventArgument) End Sub |
 |
With this code the scrolling option will be displayed under IE browsers only. Gecko-based browsers does not recognize disabled="disabled" attribute for scrollable divs. |
Server-side
The approach differs a bit when you prefer to disable the grid server-side. To summarize, you have to set the Enabled property of the control to false and switch off the auto postback on row click, column resizing, client row selection and keyboard navigation. Finally, locate the filter images when filtering is enabled and disable them on ItemCreated as well (thus preventing the filter menu from being displayed on click).
Later on, roll back the changes when you enable the grid on the page.
The following implementation shows how to enable/disable RadGrid with ajax request from external html buttons:
| ASPX/ASCX |
Copy Code |
|
<script type="text/javascript"> function DisableGrid() { window[ "<%=RadGrid1.ClientID %>"].AjaxRequest("<%=RadGrid1.UniqueID%>", "DisableGrid"); } function EnableGrid() { window[ "<%=RadGrid1.ClientID %>"].AjaxRequest("<%=RadGrid1.UniqueID%>", "EnableGrid"); } </script>
<rad:RadGrid ID="RadGrid1" DataSourceID="AccessDataSource1" runat="server" Skin="Outlook" Width= "95%" AutoGenerateColumns="False" PageSize="10" AllowSorting="True" AllowPaging="True" GridLines= "None" EnableAJAX="true" ShowGroupPanel="true" ShowStatusBar="true" OnItemCreated= "RadGrid1_ItemCreated" OnPreRender="RadGrid1_PreRender"> <PagerStyle Mode="NumericPages"></PagerStyle> <MasterTableView DataSourceID="AccessDataSource1" DataKeyNames="CustomerID" AllowMultiColumnSorting="True" Width= "100%" AllowFilteringByColumn="true"> <DetailTables> <rad:GridTableView DataKeyNames="OrderID" DataSourceID="AccessDataSource2" Width="100%" runat= "server" AllowFilteringByColumn="true"> <ParentTableRelation> <rad:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID" /> </ParentTableRelation> <Columns> <rad:GridEditCommandColumn UniqueName="EditCommandColumn" /> <rad:GridBoundColumn SortExpression="OrderID" HeaderText="OrderID" HeaderButtonType="TextButton" DataField= "OrderID" UniqueName="OrderID"> </rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="OrderDate" HeaderText="Date Ordered" HeaderButtonType="TextButton" DataField= "OrderDate" UniqueName="OrderDate"> </rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="Freight" HeaderText="Freight" HeaderButtonType="TextButton" DataField= "Freight" UniqueName="Freight"> </rad:GridBoundColumn> <rad:GridButtonColumn UniqueName="DeleteColumn" CommandName="Delete" ButtonType="ImageButton" ImageUrl= "RadControls/Grid/Skins/Orange/Delete.gif" /> </Columns> </rad:GridTableView> </DetailTables> <Columns> <rad:GridBoundColumn SortExpression="CustomerID" HeaderText="CustomerID" HeaderButtonType="TextButton" DataField= "CustomerID" UniqueName="CustomerID"> </rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="ContactName" HeaderText="Contact Name" HeaderButtonType="TextButton" DataField= "ContactName" UniqueName="ContactName"> </rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="CompanyName" HeaderText="Company" HeaderButtonType="TextButton" DataField= "CompanyName" UniqueName="CompanyName"> </rad:GridBoundColumn> <rad:GridButtonColumn UniqueName="DeleteColumn" CommandName="Delete" ButtonType="ImageButton" ImageUrl= "RadControls/Grid/Skins/Orange/Delete.gif" /> </Columns> </MasterTableView> <ClientSettings AllowColumnsReorder="true" AllowDragToGroup="true" AllowKeyboardNavigation="true" EnablePostBackOnRowClick= "true"> <Resizing AllowColumnResize="true" EnableRealTimeResize="true" /> <Selecting AllowRowSelect="true" /> <Scrolling AllowScroll="true" UseStaticHeaders="true" ScrollHeight="200px" /> </ClientSettings> </rad:RadGrid> <asp:AccessDataSource ID="AccessDataSource1" DataFile="~/Grid/Data/Access/Nwind.mdb" SelectCommand= "SELECT * FROM Customers" runat="server"></asp:AccessDataSource> <asp:AccessDataSource ID="AccessDataSource2" DataFile="~/Grid/Data/Access/Nwind.mdb" SelectCommand= "SELECT * FROM Orders Where CustomerID = ?" runat="server"> <SelectParameters> <asp:SessionParameter Name="CustomerID" SessionField="CustomerID" Type="string" /> </SelectParameters> </asp:AccessDataSource> <br /> < input id="btnServerDisable" type="button" value="Disable grid" onclick="DisableGrid()" /> < input id="btnEnable" type="button" value="Enable grid" onclick="EnableGrid()" /> |
| C# |
Copy Code |
|
protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument) { base.RaisePostBackEvent(sourceControl, eventArgument);
if (sourceControl is RadGrid && eventArgument.IndexOf("Grid") != -1) { RadGrid grid = sourceControl as RadGrid;
switch(eventArgument) { case "DisableGrid": { grid.Enabled = false; grid.ClientSettings.EnablePostBackOnRowClick = false; grid.ClientSettings.Resizing.AllowColumnResize = false; grid.ClientSettings.Selecting.AllowRowSelect = false; grid.ClientSettings.AllowKeyboardNavigation = false;
Session[ "disableFilterMenu"] = true; break; } case "EnableGrid": { grid.Enabled = true; grid.ClientSettings.EnablePostBackOnRowClick = true; grid.ClientSettings.Resizing.AllowColumnResize = true; grid.ClientSettings.Selecting.AllowRowSelect = true; grid.ClientSettings.AllowKeyboardNavigation = true;
break; } } grid.Rebind();
} }
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) { if (e.Item is GridFilteringItem && Session["disableFilterMenu"] != null) { foreach(GridColumn column in e.Item.OwnerTableView.RenderColumns) { //you can check for other types of built-in columns as well if(column is GridBoundColumn) { Image filterImage = (e.Item as GridFilteringItem)[column.UniqueName].Controls[1] as Image; filterImage.Attributes[ "disabled"] = "true"; } } } } protected void RadGrid1_PreRender(object sender, EventArgs e) { Session[ "disableFilterMenu"] = null; } |
| VB.NET |
Copy Code |
|
Protected Overloads Overrides Sub RaisePostBackEvent(ByVal sourceControl As IPostBackEventHandler, ByVal eventArgument As String) MyBase.RaisePostBackEvent(sourceControl, eventArgument)
If TypeOf sourceControl Is RadGrid AndAlso eventArgument.IndexOf("Grid") <> -1 Then Dim grid As RadGrid = CType(sourceControl, RadGrid)
Select Case eventArgument Case "DisableGrid" grid.Enabled = False grid.ClientSettings.EnablePostBackOnRowClick = False grid.ClientSettings.Resizing.AllowColumnResize = False grid.ClientSettings.Selecting.AllowRowSelect = False grid.ClientSettings.AllowKeyboardNavigation = False
Session("disableFilterMenu") = True Exit Select Case "EnableGrid" grid.Enabled = True grid.ClientSettings.EnablePostBackOnRowClick = True grid.ClientSettings.Resizing.AllowColumnResize = True grid.ClientSettings.Selecting.AllowRowSelect = True grid.ClientSettings.AllowKeyboardNavigation = True
Exit Select End Select
grid.Rebind() End If End Sub
Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemCreated
If TypeOf e.Item Is GridFilteringItem AndAlso Session("disableFilterMenu") <> Nothing Then
For Each column As GridColumn In e.Item.OwnerTableView.RenderColumns
If TypeOf column Is GridBoundColumn Then Dim filterImage As Image = CType(CType(e.Item, GridFilteringItem)(column.UniqueName).Controls(1), Image) filterImage.Attributes( "disabled") = "true" End If Next End If End Sub Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles RadGrid1.PreRender Session("disableFilterMenu") = Nothing End Sub |
 |
Keep in mind that because of the Rebind() call to disable the grid, its previous expanded state might will be lost. |
 |
Note that you can perform the same task with standard postbacks instead of asynchronous requests. |
How to trigger ajax request from client script residing in user control/master page you can learn from
this article.