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

How To Prevent NeedDataSource Event From Firing On Dropdownlist Postback In ASP.NET

4 Answers 479 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 01 Apr 2014, 07:43 PM
Hi,

I'm new user of the Telerik Controls and I have a question . . . 

I have a search screen where users can search data using various criteria.  One of the criterion is specified by making a choice in an asp.net dropdownlist.  When a certain choice is made I want to do a postback from the dropdownlist.  However, when I do this the Radgrid "NeedDataSource" Event fires.  Is there any way I can prevent the "NeedDataSource" Event from firing?  Or, is there a way to determine where the "NeedDataSource" Event was called from?  I don't need this event to fire in this case.

Thanks,

Andy 

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 04 Apr 2014, 11:44 AM
Hi Andy,

I'm not sure about your scenario. I guess your dropdownlist is outside the Grid. On its postback, the NeedDataSource will fire only if you call Rebind(). Here is a sample code i tried. I couldn't replicate such an issue at my end. Please provide your full code snippet.

ASPX:
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"    AutoPostBack="true" DataTextField="ShipCity" DataValueField="ShipCity">
</asp:DropDownList>
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView DataKeyNames="OrderID">
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID"/>          
            <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = GetDataTable("SELECT * FROM Orders");
}
public DataTable GetDataTable(string query)
{
    String ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
    DataTable myDataTable = new DataTable();
    conn.Open();
    try
    {
        adapter.Fill(myDataTable);
    }
    finally
    {
        conn.Close();
    }
    return myDataTable;
}

Thanks,
Princy
0
IT
Top achievements
Rank 1
answered on 25 Apr 2019, 01:53 PM
fd
0
IT
Top achievements
Rank 1
answered on 25 Apr 2019, 01:57 PM

i also have same issue princy, can you help.

this is my code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RadGridNeedDataSourceIssue.aspx.cs" Inherits="Telerik.RadGridNeedDataSourceIssue" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
          <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
        </div>
         <asp:DropDownList ID="ddlCompany" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCompany_SelectedIndexChanged">
             <asp:ListItem Text="Select One" Value="-1"></asp:ListItem>
             <asp:ListItem Text="All" Value="0"></asp:ListItem>
             <asp:ListItem Text="Company1" Value="1"></asp:ListItem>
         </asp:DropDownList>
        <div>
             <telerik:RadAjaxManager runat="server" ID="RadAjaxManager1" DefaultLoadingPanelID="RadAjaxLoadingPanel1">
                <AjaxSettings>
                    <telerik:AjaxSetting AjaxControlID="RadGrid1">
                        <UpdatedControls>
                            <telerik:AjaxUpdatedControl ControlID="RadGrid1"></telerik:AjaxUpdatedControl>                  
                        </UpdatedControls>
                    </telerik:AjaxSetting>
                </AjaxSettings>
            </telerik:RadAjaxManager>
             <telerik:RadAjaxLoadingPanel runat="server" ID="RadAjaxLoadingPanel1"
                Transparency="20" BackColor="#E0E0E0" InitialDelayTime="500">       
            </telerik:RadAjaxLoadingPanel>
             <telerik:RadGrid ID="RadGrid1"
                SkinID="Material"
                Skin="Windows7"
                runat="server"
                EnableHeaderContextMenu="True"
                ShowGroupPanel="False"
                ClientSettings-Scrolling-AllowScroll="true"
                GroupingEnabled="False"
                EnableLinqExpressions="false"
                EnableHeaderContextFilterMenu="True"
                AllowPaging="True"
                PagerStyle-AlwaysVisible="true"
                AllowSorting="True"
                ShowFooter="True"
                CellSpacing="-1"
                GridLines="Both"
                RenderMode="Classic"
                PagerStyle-PageButtonCount="30"
                Height="350"
                AllowFilteringByColumn="true"
                PagerStyle-Position="Bottom" OnNeedDataSource="RadGrid1_NeedDataSource"
                 >
                <MasterTableView
                    CssClass="MasterClass" AutoGenerateColumns="false" DataKeyNames="" OverrideDataSourceControlSorting="True" CommandItemDisplay="Top">
                    <CommandItemSettings ShowExportToExcelButton="true" ShowAddNewRecordButton="false" ShowRefreshButton="false" />
                    <Columns>
                        <telerik:GridBoundColumn Visible="false" SortExpression="EmployeeNo" HeaderText="Employee ID" HeaderButtonType="TextButton" DataField="EmployeeNo">
                        </telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn FilterControlWidth="80" ItemStyle-Width="50" SortExpression="FirstName" HeaderText="First Name" UniqueName="FirstName" HeaderButtonType="TextButton" DataField="FirstName">
                            <ItemTemplate>
                                <asp:LinkButton runat="server" ID="lnkEmployeeName"
                                    Text='<%# Eval("FirstName")%>'
                                    KeyEmpId='<%# (string.IsNullOrEmpty("EmployeeId"))? "0" : Eval("EmployeeId")%>'
                                    KeyPlanId='<%# (string.IsNullOrEmpty("PlanId"))? "0" : Eval("PlanId")%>'
                                    KeyUserId='<%#(string.IsNullOrEmpty("UserId"))? "0" : Eval("UserId")%>'
                                    KeyCompanyId='<%#(string.IsNullOrEmpty("CompanyId"))? "0" : Eval("CompanyId")%>'
                                    KeyParticipantId='<%# (string.IsNullOrEmpty("ParticipantId"))? "0" : Eval("ParticipantId")%>'
                                    KeyEmployeeStatusCd='<%# (string.IsNullOrEmpty("EmployeeStatus"))? "0" : Eval("EmployeeStatus")%>'
                                    KeyPlanName='<%#(string.IsNullOrEmpty("PlanName"))? "0" : Eval("PlanName")%>'
                                    KeyCompanyName='<%#(string.IsNullOrEmpty("CompanyName"))? "0" : Eval("CompanyName")%>'>
                                </asp:LinkButton>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridBoundColumn ItemStyle-Width="50" SortExpression="LastName" HeaderText="Last Name" HeaderButtonType="TextButton" UniqueName="LastName" DataField="LastName"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn ItemStyle-Width="70" Visible="true" SortExpression="CompanyName" HeaderText="Company Name" HeaderButtonType="TextButton" DataType="System.String" UniqueName="CompanyName" DataField="CompanyName"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn Visible="true" SortExpression="PlanId" HeaderText="Plan ID" HeaderButtonType="TextButton" DataType="System.String" UniqueName="PlanId" DataField="PlanId"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn SortExpression="PlanName" HeaderText="Plan Name" HeaderButtonType="TextButton" DataType="System.String" UniqueName="PlanName" DataField="PlanName"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn Visible="true" SortExpression="ParamDesc" HeaderText="Employment Status" HeaderButtonType="TextButton" DataType="System.String" UniqueName="EmployeeStatus" DataField="EmployeeStatus"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn FilterControlWidth="50" CurrentFilterFunction="Contains" ShowFilterIcon="false" AutoPostBackOnFilter="true" Visible="false" SortExpression="ParamDesc" HeaderText="City" HeaderButtonType="TextButton" DataType="System.String" DataField="City"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn Visible="false" SortExpression="ParamDesc" HeaderText="ZipCode" HeaderButtonType="TextButton" DataType="System.String" DataField="ZipCode"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn Visible="false" SortExpression="ParamDesc" HeaderText="EmployeeId" HeaderButtonType="TextButton" DataType="System.String" DataField="EmployeeId"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn Visible="false" SortExpression="ParamDesc" HeaderText="UserId" HeaderButtonType="TextButton" DataType="System.String" DataField="UserId"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn Visible="false" SortExpression="ParamDesc" HeaderText="CompanyId" HeaderButtonType="TextButton" DataType="System.String" DataField="CompanyId"></telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn FilterControlWidth="75" HeaderText="Action" UniqueName="Action" Exportable="false" AllowSorting="false" AllowFiltering="false">
                            <ItemTemplate>
                                <asp:LinkButton runat="server" ID="lnkCompany" Text="Edit"
                                    KeyEmpId='<%# (string.IsNullOrEmpty("EmployeeId"))? "0" : Eval("EmployeeId")%>'
                                    KeyPlanId='<%# (string.IsNullOrEmpty("PlanId"))? "0" : Eval("PlanId")%>'
                                    KeyUserId='<%#(string.IsNullOrEmpty("UserId"))? "0" : Eval("UserId")%>'
                                    KeyCompanyId='<%#(string.IsNullOrEmpty("CompanyId"))? "0" : Eval("CompanyId")%>'
                                    KeyParticipantId='<%# (string.IsNullOrEmpty("ParticipantId"))? "0" : Eval("ParticipantId")%>'
                                    KeyEmployeeStatusCd='<%# (string.IsNullOrEmpty("EmployeeStatus"))? "0" : Eval("EmployeeStatus")%>'
                                    KeyPlanName='<%#(string.IsNullOrEmpty("PlanName"))? "0" : Eval("PlanName")%>'
                                    KeyCompanyName='<%#(string.IsNullOrEmpty("CompanyName"))? "0" : Eval("CompanyName")%>'
                                    >
                                </asp:LinkButton>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>
                </MasterTableView>

                <ClientSettings AllowDragToGroup="False" AllowColumnsReorder="False" Scrolling-UseStaticHeaders="True"
                    ReorderColumnsOnClient="False">
                    <ClientEvents OnGridCreated="Grid_OnGridCreated" />
                    <Selecting AllowRowSelect="False"></Selecting>
                    <Virtualization EnableVirtualization="True" InitiallyCachedItemsCount="1000"
                        LoadingPanelID="RadAjaxLoadingPanel1"
                        ItemsPerView="1000" />
                    <Scrolling AllowScroll="true" ScrollHeight="152px" />
                </ClientSettings>

            </telerik:RadGrid>
        </div>

    </form>
</body>
</html>

 

using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace Telerik
{
    public partial class RadGridNeedDataSourceIssue : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {  
            
        }       
        protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            Database database = null;
            DataTable dtEmployeeAccountBalance = new DataTable();
            database = new SqlDatabase("Data Source=COREDC;Initial Catalog=COREAge_S19_PerfDev_Trans;Integrated Security=False;User ID=coredcdev;Password=welcome@2020;Connect Timeout=0");
            DbCommand dbCommand = database.GetStoredProcCommand("EMP_GetEmployees");
            dbCommand.CommandTimeout = 60000;
            database.AddInParameter(dbCommand, "@CompanyId", DbType.Int64, 0);
            database.AddInParameter(dbCommand, "@PlanId", DbType.Int64, 0);
            database.AddInParameter(dbCommand, "@ClassificationNameID", DbType.Int64, 0);
            database.AddInParameter(dbCommand, "@ClassificationTypeID", DbType.Int64, 0);
            database.AddInParameter(dbCommand, "@UniquePersonalIdentification", DbType.String, "");
            database.AddInParameter(dbCommand, "@UserId", DbType.Int64, 1);
            database.AddInParameter(dbCommand, "@PartnerId", DbType.Int64, 1);
            database.AddInParameter(dbCommand, "@RoleId", DbType.Int64, 0);
            DataSet dsEmployeeAccountBalance = database.ExecuteDataSet(dbCommand);
            dtEmployeeAccountBalance = dsEmployeeAccountBalance.Tables[0];
            RadGrid1.DataSource = dtEmployeeAccountBalance;
        }
    }
}

 

 

0
Peter Milchev
Telerik team
answered on 30 Apr 2019, 12:52 PM
Hi,

From the provided code snippets, it seems that the DropDownList is triggering a full postback and the fact that the Grid is with enabled Virtualization is forcing it to retrieve the data again via the NeedDataSource event. 

For convenience and better visibility from the community, I am sharing the reply from the duplicate support ticket: 

With Virtualization the data is kept on client-side, therefore, the grid needs to re-generate its client-side data/cache. 

I want to shed some light on the Virtualization feature and its binding specifics:

This built-in functionality is dedicated only for View mode of the grid:
http://demos.telerik.com/aspnet-ajax/grid/examples/performance/virtualization/defaultcs.aspx

And performance when checking a large volume of records:
http://www.telerik.com/blogs/how-to-load-1m-records-in-telerik-s-asp.net-grid-without-compromising-performance

However, its also has its limitations due to its complex internal logic. To summarize, Virtualization is suitable only for Preview mode for the items. I am afraid Row/Cell Selection is not supported as mentioned in the list:
https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/functionality/scrolling/virtualization#limitations

Here is a summary what is happening when using Virtualization:

1. The grid loads initially - regardless of the total grid records in the database, only the initially set in the Virtualization settings are loaded. NeedDataSource fires.

2. If the user scrolls to another page, the grid loads only the records for this page and saves them in the client-side cache. NeedDataSource fires.

3. Every time the user comes again to an already loaded page, the grid does not makes a query to the server, but gets this data from the cache. NeedDataSource does not fire.

4. If the user scrolls to a new not-visited page, the new data will be requested from the server and saved in the cache. Its the same as step 2, this process repeats depending on the user's actions. NeedDataSource fires.

5. If the user updates the entire page via a postback or makes an AJAX request which updates the grid also, the cache will be cleared and the collection process will start anew - the cache is not being sent to the server, which would eliminate the point of Virtualization optimization. NeedDataSource fires.


Regards,
Peter Milchev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Andy
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
IT
Top achievements
Rank 1
Peter Milchev
Telerik team
Share this question
or