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

OnNeedDataSource is not firering unless EnableViewState is set to false

1 Answer 93 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nikolaus
Top achievements
Rank 1
Nikolaus asked on 31 Aug 2017, 08:22 AM

Hello,

I've created a small page with a RadGrid on it. Now the problem is, that the event OnNeedDataSource is only fired if i set ViewState=false(I found this solution on an other thread). Now the grid does work, but if I also habe 2 GridButtonColumn's in the grid. Now if I click them, the OnNeedDataSource event is only called bevore the method "RadGrid1_ItemCommand". So any changes I make to the database are not shown in the grid!

aspx code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="QU_R17_ASP_Testproject.WebForm1" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register TagPrefix="a" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI, Version=2017.2.711.45, Culture=neutral, PublicKeyToken=121fae78165ba3d4" %>
 
<!DOCTYPE html>
 
<head runat="server">
    <title></title>
</head>
<body>
    <form runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
         
        <div class="demo-container">
            <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
            </telerik:RadAjaxLoadingPanel>
            <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">
            <telerik:RadDatePicker ID="StartDateTimePicker" runat="server" AutoPostBack="false" Culture="en-US" >
                <Calendar UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False" EnableWeekends="True" FastNavigationNextText="&lt;&lt;"></Calendar>
                <DateInput DisplayDateFormat="yyyy-MM-dd" DateFormat="yyyy-MM-dd" LabelWidth="40%" AutoPostBack="True" runat="server">
                    <EmptyMessageStyle Resize="None" />
                    <ReadOnlyStyle Resize="None" />
                    <FocusedStyle Resize="None" />
                    <DisabledStyle Resize="None" />
                    <InvalidStyle Resize="None" />
                    <HoveredStyle Resize="None" />
                    <EnabledStyle Resize="None" />
                </DateInput>
                <DatePopupButton ImageUrl="" HoverImageUrl=""></DatePopupButton>
            </telerik:RadDatePicker>
 
                <telerik:RadDatePicker ID="EndDateTimePicker" runat="server" AutoPostBack="false" Culture="en-US" >
                <Calendar UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False" EnableWeekends="True" FastNavigationNextText="&lt;&lt;"></Calendar>
                <DateInput DisplayDateFormat="yyyy-MM-dd" DateFormat="yyyy-MM-dd" LabelWidth="40%" AutoPostBack="True">
                    <EmptyMessageStyle Resize="None" />
                    <ReadOnlyStyle Resize="None" />
                    <FocusedStyle Resize="None" />
                    <DisabledStyle Resize="None" />
                    <InvalidStyle Resize="None" />
                    <HoveredStyle Resize="None" />
                    <EnabledStyle Resize="None" />
                </DateInput>
                <DatePopupButton ImageUrl="" HoverImageUrl=""></DatePopupButton>
            </telerik:RadDatePicker>
                 
                <telerik:RadTextBox ID="ResultTextBox" BorderStyle="None" runat="server" Resize="Both" Width="1000px"></telerik:RadTextBox>
                                             
                <telerik:RadGrid RenderMode="Lightweight" ID="RadGrid2" runat="server" AllowPaging="True" AllowSorting="True" AllowFilteringByColumn="True" ShowGroupPanel="True" DataKeyNames="ID"
                OnItemCommand="RadGrid1_ItemCommand" OnNeedDataSource="RadGrid2_NeedDataSource" EnableViewState="False">
                <MasterTableView AutoGenerateColumns="false" ShowFooter="True">
                    <PagerStyle AlwaysVisible="true" />
                    <CommandItemSettings ShowAddNewRecordButton="False" ShowRefreshButton="False" />
                    <Columns>
                        <telerik:GridBoundColumn FilterControlWidth="120px" DataField="Status" HeaderText="Status"
                            AutoPostBackOnFilter="false" CurrentFilterFunction="Contains"
                            ShowFilterIcon="false">
                            <FilterTemplate>
                                <telerik:RadComboBox RenderMode="Lightweight" ID="RadComboBoxStatus"
                                    Width="110px" SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("Status").CurrentFilterValue %>'
                                    runat="server" OnClientSelectedIndexChanged="StatusIndexChanged">
                                    <Items>
                                        <telerik:RadComboBoxItem Text="All" />
                                        <telerik:RadComboBoxItem Text="Assigned" Value="Assigned" />
                                        <telerik:RadComboBoxItem Text="Idle" Value="Idle" />
                                        <telerik:RadComboBoxItem Text="CLOSED" Value="CLOSED" />
                                    </Items>
                                </telerik:RadComboBox>
                                <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
                                    <script type="text/javascript">
                                        function StatusIndexChanged(sender, args) {
                                            var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                        tableView.filter("Status", args.get_item().get_value(), "EqualTo");
                                    }
                                    </script>
                                </telerik:RadScriptBlock>
                            </FilterTemplate>
                        </telerik:GridBoundColumn>
 
                        <telerik:GridBoundColumn FilterControlWidth="150px" DataField="System" HeaderText="System"
                            AutoPostBackOnFilter="true" CurrentFilterFunction="Contains"
                            ShowFilterIcon="false">
                            <FilterTemplate>
                                <telerik:RadComboBox RenderMode="Lightweight" ID="RadComboBoxSystem"
                                    Width="150px" AppendDataBoundItems="true" SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("System").CurrentFilterValue %>'
                                    runat="server" OnClientSelectedIndexChanged="SystemIndexChanged">
                                    <Items>
                                        <telerik:RadComboBoxItem Text="All" />
                                        <telerik:RadComboBoxItem Text="Printing" Value="Printing" />
                                        <telerik:RadComboBoxItem Text="Wet" Value="Wet" />
                                        <telerik:RadComboBoxItem Text="Assembly" Value="Assembly" />
                                    </Items>
                                </telerik:RadComboBox>
                                <telerik:RadScriptBlock ID="RadScriptBlock2" runat="server">
                                    <script type="text/javascript">
                                        function SystemIndexChanged(sender, args) {
                                            var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                        tableView.filter("System", args.get_item().get_value(), "EqualTo");
                                    }
                                    </script>
                                </telerik:RadScriptBlock>
                            </FilterTemplate>
                        </telerik:GridBoundColumn>
 
                        <telerik:GridMaskedColumn DataField="SystemId" HeaderText="SystemId" Display="False" />
                         
                        <telerik:GridMaskedColumn DataField="JobId" HeaderText="JobId"
                            FilterControlWidth="80px" AutoPostBackOnFilter="false" CurrentFilterFunction="EqualTo"
                            FilterDelay="2000" ShowFilterIcon="false" Display="False">
                            <ItemStyle HorizontalAlign="Right" />
                        </telerik:GridMaskedColumn>
                         
                        <telerik:GridBoundColumn FilterControlWidth="80px" DataField="Job" HeaderText="Job"
                            AutoPostBackOnFilter="true" CurrentFilterFunction="Contains"
                            ShowFilterIcon="false">
                            <ItemStyle HorizontalAlign="Right" />
                        </telerik:GridBoundColumn>
 
                        <telerik:GridMaskedColumn DataField="UniqueID" HeaderText="UniqueID"
                            FilterControlWidth="110px" AutoPostBackOnFilter="false" CurrentFilterFunction="EqualTo"
                            FilterDelay="1000" ShowFilterIcon="false">
                        </telerik:GridMaskedColumn>
 
                        <telerik:GridBoundColumn FilterControlWidth="70px" DataField="Lot" HeaderText="Lot"
                            AutoPostBackOnFilter="true" CurrentFilterFunction="Contains"
                            ShowFilterIcon="false">
                            <ItemStyle HorizontalAlign="Right" />
                        </telerik:GridBoundColumn>
                         
                        <telerik:GridBoundColumn FilterControlWidth="95px" DataField="LogDateTime" HeaderText="LogDateTime" ShowFilterIcon="false" AllowFiltering="False">
                            <ItemStyle HorizontalAlign="Right" />
                        </telerik:GridBoundColumn>                       
 
                        <telerik:GridMaskedColumn DataField="ProductID " HeaderText="ProductID "
                            FilterControlWidth="50px" AutoPostBackOnFilter="false" CurrentFilterFunction="EqualTo"
                            FilterDelay="2000" ShowFilterIcon="false">
                            <ItemStyle HorizontalAlign="Right" />
                        </telerik:GridMaskedColumn>
 
                        <telerik:GridBoundColumn FilterControlWidth="310px" DataField="Product" HeaderText="Product"
                            AutoPostBackOnFilter="true" CurrentFilterFunction="Contains"
                            ShowFilterIcon="false">
                        </telerik:GridBoundColumn>
 
                        <telerik:GridButtonColumn ButtonType="PushButton" CommandName="DeleteItem" Text="DeleteItem" UniqueName="DeleteItem" HeaderText="DeleteItem">
                        </telerik:GridButtonColumn>
                         
                        <telerik:GridButtonColumn ButtonType="PushButton" CommandName="VerifyItem" Text="VerifyItem" UniqueName="VerifyItem" HeaderText="VerifyItem" >
                        </telerik:GridButtonColumn>
                    </Columns>
                    <HeaderStyle Font-Bold="True" />
                </MasterTableView>
                <ExportSettings>
                    <Pdf PageWidth="">
                    </Pdf>
                </ExportSettings>
                <ClientSettings EnableRowHoverStyle="true" AllowDragToGroup="True">
                    <Selecting AllowRowSelect="True" />
                </ClientSettings>
                <GroupingSettings CollapseAllTooltip="Collapse all groups" />
                <SortingSettings SortedBackColor="#FFF6D6" EnableSkinSortStyles="false" />
                <HeaderStyle Width="100px" />
                <FilterMenu RenderMode="Lightweight" />
                <HeaderContextMenu RenderMode="Lightweight" />
            </telerik:RadGrid>
            </telerik:RadAjaxPanel>
        </div
 
    </form>
</body>
</html>

 

aspx.cs code:

public partial class WebForm1 : System.Web.UI.Page
 {
     protected void Page_Load(object sender, EventArgs e)
     {
         if (!IsPostBack)
         {
             StartDateTimePicker.SelectedDate = DateTime.Now.AddDays(-14);
             EndDateTimePicker.SelectedDate = DateTime.Now;
             LoadDataForRadGrid1();
         }
     }
 
     private void LoadDataForRadGrid1()
     {
 
         RadGrid2.DataSource = GetDataTable(StartDateTimePicker.FocusedDate, EndDateTimePicker.FocusedDate);
         RadGrid2.DataBind();
     }
 
     public DataTable GetDataTable(DateTime startDateTime, DateTime endDateTime)
     {
         string query = @"select * from [dbo].[ufGetList]()";
 
         String connectionString = ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
         DataTable myDataTable = new DataTable();
 
         using (SqlConnection conn = new SqlConnection(connectionString))
         {
             SqlCommand cmd = new SqlCommand(query, conn);
             conn.Open();
             SqlDataAdapter adapter = new SqlDataAdapter
             {
                 SelectCommand = new SqlCommand(query, conn)
             };
             adapter.Fill(myDataTable);
             conn.Close();
         }
 
         return myDataTable;
     }
 
     protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
     {
         if (e.CommandName == "DeleteItem")
         {
 
         }
     }
 
     protected void RadGrid2_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
     {
         (sender as RadGrid).DataSource = GetDataTable(StartDateTimePicker.SelectedDate.Value, EndDateTimePicker.SelectedDate.Value);
     }
 
 }

 

Thanks.

1 Answer, 1 is accepted

Sort by
0
Peter Milchev
Telerik team
answered on 31 Aug 2017, 01:05 PM
Hello Pius,

Would you please check if calling the .Rebind() method instead of DataBind after the database changes are made in the ItemCommand event? 

More information on when to use the Rebind and DataBind methods could be found in the Most Common Mistakes article.

Regards,
Peter Milchev
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Nikolaus
Top achievements
Rank 1
Answers by
Peter Milchev
Telerik team
Share this question
or