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

Rad Grid Sample

0 Answers 149 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mani kandan
Top achievements
Rank 1
mani kandan asked on 12 Aug 2010, 02:07 AM
hi
       Please check below code is correct or wrong

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server" />
    <telerik:RadCodeBlock runat="server">

        <script type="text/javascript">
            function onRowClick(object, eventArgs)
            {
                var index = eventArgs.get_itemIndexHierarchical();
                var dataItem = object.get_masterTableView().get_dataItems()[index];
                var row = dataItem.get_element();
                var ckeckBox = row.children[0].children[0];
                if (!ckeckBox.checked)
                {
                    ckeckBox.checked = true;
                    row.style.background = 'url("/Example/WebResource.axd?d=JSFPqlAaSqKUrTohMTfXqfrIw9LabHGFla0WzaxpulM-qz-1hwuBBMMilJcO_tHwtYJ1swr0Et0pNdj0ce13ug2&t=634050632760000000") repeat-x scroll 0 -3900px #97B8E8';
                }
                else
                {
                    ckeckBox.checked = false;
                    row.style.background = "";
                }

                var flag = true;
                for (var i = 0; i < object.get_masterTableView().get_dataItems().length; i++)
                {
                    var dataItem = object.get_masterTableView().get_dataItems()[i];
                    var row = dataItem.get_element();
                    var ckeckBox = row.children[0].children[0];
                    if (!ckeckBox.checked)
                    {
                        flag = false;
                    }
                }

                if (flag)
                {
                    setCheckBox(true);
                }
                else
                {
                    setCheckBox(false);
                }
            }

            function setSelectedAfterPostback(rowIDs)
            {
                var arrRowIDs = rowIDs.split(",");
                for (var i = 0; i < arrRowIDs.length - 1; i++)
                {
                    var row = document.getElementById(arrRowIDs[i]);
                    var ckeckBox = row.children[0].children[0];
                    ckeckBox.checked = true;
                    row.style.background = 'url("/Example/WebResource.axd?d=JSFPqlAaSqKUrTohMTfXqfrIw9LabHGFla0WzaxpulM-qz-1hwuBBMMilJcO_tHwtYJ1swr0Et0pNdj0ce13ug2&t=634050632760000000") repeat-x scroll 0 -3900px #97B8E8';
                }
            }

            function CheckBoxCheckedOnClient(rowID, chechBoxId)
            {
                var row = document.getElementById(rowID);
                var ckeckBox = document.getElementById(chechBoxId);
                if (ckeckBox.checked)
                {
                    row.style.background = 'url("/Example/WebResource.axd?d=JSFPqlAaSqKUrTohMTfXqfrIw9LabHGFla0WzaxpulM-qz-1hwuBBMMilJcO_tHwtYJ1swr0Et0pNdj0ce13ug2&t=634050632760000000") repeat-x scroll 0 -3900px #97B8E8';
                }
                else
                {
                    row.style.background = "";
                }
            }

            function setCheckBox(toCheck)
            {
                var checkBoxID = document.getElementById("hf1").value;
                var checkBox = document.getElementById(checkBoxID);
                checkBox.checked = toCheck;
            }

            function CheckUnCheckAll(id)
            {
                var sender = document.getElementById(id);
                var grid = $find("<%=RadGrid1.ClientID %>");

                for (var i = 0; i < grid.get_masterTableView().get_dataItems().length; i++)
                {
                    var dataItem = grid.get_masterTableView().get_dataItems()[i];
                    var row = dataItem.get_element();
                    var checkBox = row.children[0].children[0];
                    if (sender.checked)
                    {
                        checkBox.checked = true;
                        row.style.background = 'url("/Example/WebResource.axd?d=JSFPqlAaSqKUrTohMTfXqfrIw9LabHGFla0WzaxpulM-qz-1hwuBBMMilJcO_tHwtYJ1swr0Et0pNdj0ce13ug2&t=634050632760000000") repeat-x scroll 0 -3900px #97B8E8';
                    }
                    else
                    {
                        checkBox.checked = false;
                         row.style.background = "";
                    }
                }

            }
        </script>

    </telerik:RadCodeBlock>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        <Scripts>
            <%--Needed for JavaScript IntelliSense in VS2010--%>
            <%--For VS2008 replace RadScriptManager with ScriptManager--%>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
        </Scripts>
    </telerik:RadScriptManager>

    <script type="text/javascript">
        //Put your Java Script code here.
    </script>

    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    </telerik:RadAjaxManager>
    <div>
        <telerik:RadGrid runat="server" Skin="Outlook" AutoGenerateColumns="false" ID="RadGrid1">
            <MasterTableView>
                <Columns>
                    <telerik:GridTemplateColumn UniqueName="template">
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox" runat="server" />
                        </ItemTemplate>
                        <HeaderTemplate>
                            <asp:CheckBox ID="CheckBox" runat="server" />
                        </HeaderTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Item" UniqueName="Item" HeaderText="Item">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Date" UniqueName="Date" HeaderText="Date">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings>
                <ClientEvents OnRowClick="onRowClick" />
            </ClientSettings>
        </telerik:RadGrid>
        <asp:HiddenField ID="hf1" runat="server" />
        <asp:Button runat="server" ID="Button1" Text="Get selected rows IDs" />
    </div>
    </form>
</body>
</html>

Code behind
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using System.Collections.Generic;

public partial class Default : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
        RadGrid1.ItemDataBound += new GridItemEventHandler(RadGrid1_ItemDataBound);
        RadGrid1.PreRender += new EventHandler(RadGrid1_PreRender);
        RadGrid1.ItemCreated += new GridItemEventHandler(RadGrid1_ItemCreated);
    }

    void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item.ItemType == GridItemType.Header)
        {
            GridHeaderItem headerItem = (GridHeaderItem)e.Item;
            CheckBox chkSelectAll = (CheckBox)headerItem["template"].Controls[1];
            hf1.Value = chkSelectAll.ClientID;

            string script = "CheckUnCheckAll('" + chkSelectAll.ClientID + "')";
            chkSelectAll.Attributes.Add("onclick", script);
        }
    }

    void RadGrid1_PreRender(object sender, EventArgs e)
    {
        string IDs = string.Empty;
        string rowIDs = string.Empty;
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            CheckBox checkBox = item["template"].FindControl("CheckBox") as CheckBox;
            if (checkBox.Checked)
            {
                IDs = IDs + item["ID"].Text + ",";
                rowIDs = rowIDs + item.ClientID.ToString() + ",";
            }
        }
        if (!string.IsNullOrEmpty(IDs))
        {
            IDs = IDs.Substring(0,IDs.LastIndexOf(","));
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "setSelectedAfterPostback", "setSelectedAfterPostback('" + rowIDs + "');", true);
            RadAjaxManager1.Alert(IDs);
        }
    }

    void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            CheckBox checkBox = item["template"].FindControl("CheckBox") as CheckBox;
            string script = "CheckBoxCheckedOnClient('" + item.ClientID + "', '" + checkBox.ClientID + "')";
            checkBox.Attributes.Add("onclick", script);
        }
    }

    void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("ID");
        table.Columns.Add("Item");
        table.Columns.Add("Date", typeof(DateTime));
        for (int i = 1; i < 8; i++)
        {
            table.Rows.Add(i, "Item" + i.ToString(), DateTime.Now.AddDays(i));
        }
        RadGrid1.DataSource = table;
    }
}


Web.config
<?xml version="1.0"?>
<!--
    Note: As an alternative to hand editing this file you can use the
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in
    machine.config.comments usually located in
    \Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
    <configSections>
        <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                    <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                    <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                    <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                    <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                </sectionGroup>
            </sectionGroup>
        </sectionGroup>
    </configSections>
    <appSettings>
        <add key="Telerik.ScriptManager.TelerikCdn" value="Disabled"/>
        <add key="Telerik.StyleSheetManager.TelerikCdn" value="Disabled"/>
    </appSettings>
    <connectionStrings/>
    <system.web>
        <!--
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
        -->
        <compilation debug="true">
            <assemblies>
                <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            </assemblies>
        </compilation>
        <pages>
            <controls>
                <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add tagPrefix="telerik" namespace="Telerik.Web.UI" assembly="Telerik.Web.UI"/>
            </controls>
        </pages>
        <!--
            The <authentication> section enables configuration
            of the security authentication mode used by
            ASP.NET to identify an incoming user.
        -->
        <authentication mode="Windows"/>
        <!--
            The <customErrors> section enables configuration
            of what to do if/when an unhandled error occurs
            during the execution of a request. Specifically,
            it enables developers to configure html error pages
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
        <httpHandlers>
            <remove verb="*" path="*.asmx"/>
            <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
            <add path="ChartImage.axd" type="Telerik.Web.UI.ChartHttpHandler" verb="*" validate="false"/>
            <add path="Telerik.Web.UI.SpellCheckHandler.axd" type="Telerik.Web.UI.SpellCheckHandler" verb="*" validate="false"/>
            <add path="Telerik.Web.UI.DialogHandler.aspx" type="Telerik.Web.UI.DialogHandler" verb="*" validate="false"/>
            <add path="Telerik.RadUploadProgressHandler.ashx" type="Telerik.Web.UI.RadUploadProgressHandler" verb="*" validate="false"/>
            <add path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource" verb="*" validate="false"/>
        </httpHandlers>
        <httpModules>
            <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="RadUploadModule" type="Telerik.Web.UI.RadUploadHttpModule"/>
            <add name="RadCompression" type="Telerik.Web.UI.RadCompression"/>
        </httpModules>
    </system.web>
    <system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                <providerOption name="CompilerVersion" value="v3.5"/>
                <providerOption name="WarnAsError" value="false"/>
            </compiler>
            <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                <providerOption name="CompilerVersion" value="v3.5"/>
                <providerOption name="OptionInfer" value="true"/>
                <providerOption name="WarnAsError" value="false"/>
            </compiler>
        </compilers>
    </system.codedom>
    <!--
        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
    -->
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <modules>
            <remove name="ScriptModule"/>
            <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <remove name="RadUploadModule"/>
            <add name="RadUploadModule" type="Telerik.Web.UI.RadUploadHttpModule" preCondition="integratedMode,runtimeVersionv2.0"/>
            <remove name="RadCompression"/>
            <add name="RadCompression" type="Telerik.Web.UI.RadCompression" preCondition="integratedMode,runtimeVersionv2.0"/>
        </modules>
        <handlers>
            <remove name="WebServiceHandlerFactory-Integrated"/>
            <remove name="ScriptHandlerFactory"/>
            <remove name="ScriptHandlerFactoryAppServices"/>
            <remove name="ScriptResource"/>
            <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <remove name="ChartImage_axd"/>
            <add name="ChartImage_axd" path="ChartImage.axd" type="Telerik.Web.UI.ChartHttpHandler" verb="*" preCondition="integratedMode,runtimeVersionv2.0"/>
            <remove name="Telerik_Web_UI_SpellCheckHandler_axd"/>
            <add name="Telerik_Web_UI_SpellCheckHandler_axd" path="Telerik.Web.UI.SpellCheckHandler.axd" type="Telerik.Web.UI.SpellCheckHandler" verb="*" preCondition="integratedMode,runtimeVersionv2.0"/>
            <remove name="Telerik_Web_UI_DialogHandler_aspx"/>
            <add name="Telerik_Web_UI_DialogHandler_aspx" path="Telerik.Web.UI.DialogHandler.aspx" type="Telerik.Web.UI.DialogHandler" verb="*" preCondition="integratedMode,runtimeVersionv2.0"/>
            <remove name="Telerik_RadUploadProgressHandler_ashx"/>
            <add name="Telerik_RadUploadProgressHandler_ashx" path="Telerik.RadUploadProgressHandler.ashx" type="Telerik.Web.UI.RadUploadProgressHandler" verb="*" preCondition="integratedMode,runtimeVersionv2.0"/>
            <remove name="Telerik_Web_UI_WebResource_axd"/>
            <add name="Telerik_Web_UI_WebResource_axd" path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource" verb="*" preCondition="integratedMode,runtimeVersionv2.0"/>
        </handlers>
    </system.webServer>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
                <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
                <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

No answers yet. Maybe you can help?

Tags
Grid
Asked by
mani kandan
Top achievements
Rank 1
Share this question
or