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

Server-Side Rebind Only Works First Time

6 Answers 92 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeremy Yoder
Top achievements
Rank 1
Jeremy Yoder asked on 23 Jun 2010, 03:34 PM

I tried posting this before and got an error, so I'll try again. Please delete one of them if this ends up being a double-post...

Rebind only calls the NeedDatasource event the first time on server-side when called more than once in the same postback. Seems like a bug, but I'm wondering if it's purposeful.

What I'm doing is rebinding an empty dataset to the grid to initialize it by removing all the rows, just in case something errors out during the following DB load. If the load is successful, I want to rebind the newly loaded dataset. However, it only works if I skip the first rebind, which seems odd.

6 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 25 Jun 2010, 05:48 AM
Hi Jeremy,

Could you paste your aspx and code-behind (please, use the code formatter tool of the ticket editor to make the code readable). Thanks.

Greetings,
Tsvetoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jeremy Yoder
Top achievements
Rank 1
answered on 25 Jun 2010, 03:47 PM

I made a simple dummy example to show it...

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="POGrid.aspx.vb" Inherits="POGrid" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!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>  
</head> 
<body> 
 
    <form id="form1" runat="server">  
 
    <asp:ScriptManager ID="ScriptManager1" runat="server">  
    </asp:ScriptManager> 
      
    <div> 
          
    <telerik:RadGrid ID="RadGridPO" runat="server" AutoGenerateColumns="False" GridLines="None" Width="500px" > 
        <HeaderContextMenu EnableAutoScroll="True">  
        </HeaderContextMenu> 
        <MasterTableView> 
            <CommandItemSettings ShowRefreshButton="True" /> 
            <Columns> 
                <telerik:GridBoundColumn DataField="PurchaseOrderDetailDescription" HeaderText="Detail Description" 
                    UniqueName="PurchaseOrderDetailDescription">  
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="Catalogue" HeaderText="Catalogue" UniqueName="Catalogue">  
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="UnitDescription" HeaderText="Unit" UniqueName="UnitDescription">  
                </telerik:GridBoundColumn> 
                <telerik:GridNumericColumn DataField="PurchaseOrderDetailAmount" HeaderText="Amount" 
                    UniqueName="PurchaseOrderDetailAmount" DataFormatString="{0:C2}">  
                </telerik:GridNumericColumn> 
                <telerik:GridNumericColumn DataField="PurchaseOrderDetailKey" HeaderText="PurchaseOrderDetailKey" 
                    UniqueName="PurchaseOrderDetailKey" Visible="False" ReadOnly="True">  
                </telerik:GridNumericColumn> 
            </Columns> 
        </MasterTableView> 
    </telerik:RadGrid> 
      
    <br /> 
      
    <asp:Button ID="btnTwo" runat="server" Text="Two Rebinds" /> 
    <asp:Button ID="btnOne" runat="server" Text="One Rebind" /> 
 
    </div> 
      
    </form> 
</body> 
</html> 
 


Option Strict On 
Option Explicit On 
 
Imports System.Data  
Imports Telerik.Web.UI  
Imports System.Web.Services  
 
Partial Class POGrid  
    Inherits System.Web.UI.Page  
 
    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
 
        If Not IsPostBack Then 
 
            If POGridData Is Nothing Then 
 
                Dim dt As New DataTable("PODetails")  
 
                dt.Columns.Add("PurchaseOrderDetailDescription"GetType(String))  
                dt.Columns.Add("Catalogue"GetType(String))  
                dt.Columns.Add("UnitDescription"GetType(String))  
                dt.Columns.Add("PurchaseOrderDetailAmount"GetType(Decimal))  
                dt.Columns.Add("PurchaseOrderDetailKey"GetType(Integer))  
 
                ' Create dummy rows to delete/edit/insert  
                Dim vals(1) As String 
                vals = New String() {"Bob""Barker""Each""30""1"}  
                dt.Rows.Add(vals)  
                vals = New String() {"Harry""Potter""Each""60""2"}  
                dt.Rows.Add(vals)  
                vals = New String() {"Rudyard""Kipling""Each""90""3"}  
                dt.Rows.Add(vals)  
                vals = New String() {"Neil""Gaiman""Each""120""4"}  
                dt.Rows.Add(vals)  
                vals = New String() {"Ann""Landers""Each""150""5"}  
                dt.Rows.Add(vals)  
                HoldData = dt  
 
 
                ' Create an empty dataset to assign later.  
                Dim dtEmpty As New DataTable("Empty")  
                dtEmpty.Columns.Add("PurchaseOrderDetailDescription"GetType(String))  
                dtEmpty.Columns.Add("Catalogue"GetType(String))  
                dtEmpty.Columns.Add("UnitDescription"GetType(String))  
                dtEmpty.Columns.Add("PurchaseOrderDetailAmount"GetType(Decimal))  
                dtEmpty.Columns.Add("PurchaseOrderDetailKey"GetType(Integer))  
                EmptyData = dtEmpty  
 
                POGridData = HoldData  
 
            End If 
 
        End If 
 
    End Sub 
 
    Private Sub RadGridPO_NeedDataSource(ByVal source As System.ObjectByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGridPO.NeedDataSource  
        RadGridPO.DataSource = POGridData  
    End Sub 
 
    Private Property POGridData() As DataTable  
        Get 
            If Session("POGridData"Is Nothing Then Return Nothing 
            Return CType(Session("POGridData"), DataTable)  
        End Get 
        Set(ByVal value As DataTable)  
            Session("POGridData") = value  
        End Set 
    End Property 
 
    Private Property HoldData() As DataTable  
        Get 
            If Session("HoldData"Is Nothing Then Return Nothing 
            Return CType(Session("HoldData"), DataTable)  
        End Get 
        Set(ByVal value As DataTable)  
            Session("HoldData") = value  
        End Set 
    End Property 
 
    Private Property EmptyData() As DataTable  
        Get 
            If Session("EmptyData"Is Nothing Then Return Nothing 
            Return CType(Session("EmptyData"), DataTable)  
        End Get 
        Set(ByVal value As DataTable)  
            Session("EmptyData") = value  
        End Set 
    End Property 
 
    Protected Sub btnTwo_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles btnTwo.Click  
        POGridData = EmptyData  
        RadGridPO.Rebind()  
        POGridData = HoldData  
        RadGridPO.Rebind()  
    End Sub 
 
    Protected Sub btnOne_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles btnOne.Click  
        POGridData = EmptyData  
        POGridData = HoldData  
        RadGridPO.Rebind()  
    End Sub 
 
End Class 
 

After clicking btnTwo, the grid still has the EmptyData because the second rebind doesn't call the NeedDataSource event.
0
Tsvetoslav
Telerik team
answered on 29 Jun 2010, 03:42 PM
Hello Jeremy,

You have forgot to attach the click handlers to the two buttons and they are just making a post-back without executing the code within them (note that AutoEventWireup="false" for your page).

Regards,
Tsvetoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jeremy Yoder
Top achievements
Rank 1
answered on 06 Jul 2010, 06:57 PM

I was gone for a week and am only now able to respond.

Actually, the code executes fine -- I can put breakpoints in both button event clicks and step through them. So the problem still remains -- why does the grid remain empty after the code that rebinds twice, but not in the rebind once code?
0
Accepted
Tsvetoslav
Telerik team
answered on 09 Jul 2010, 12:35 PM
Hello Jeremy,

You should call the Rebind method just once, there is no need whatsoever to set the grid first to an empty data source and after that to the actual one.

Best wishes,
Tsvetoslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jeremy Yoder
Top achievements
Rank 1
answered on 09 Jul 2010, 02:35 PM

Well, I wanted to to blank out the grid before trying to populate it in case the latter has an error. But with some try-catch statements and restructuring, I can make it work with only a final rebind. Good to know that rebind only works once on a postback, which I hadn't realized or read anywhere. Thanks.
Tags
Grid
Asked by
Jeremy Yoder
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Jeremy Yoder
Top achievements
Rank 1
Share this question
or