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

What am I doing wrong here????

2 Answers 55 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kim
Top achievements
Rank 1
Kim asked on 22 Mar 2012, 04:47 AM

I have a simple grid bound to a List of objects.

I have a command button that calls a javascript function which executes an ajax request. In that request I add a new item to the list and rebind my grid. The new item flashes in the grid for a moment then dissappears, leaving only the original item. I am new to telerik but this seem so basic. I must be missing something or not sure how to do this. See code below.



Default.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="bug._Default" %>
  
<%@ 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">
  
<head id="Head1" runat="server">
<title></title>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">    
        <script type="text/javascript" id="telerikClientEvents1">
            function RadButton1_Clicked(sender, args) {
                $find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("Rebind");
            }
  
        </script>
    </telerik:RadCodeBlock>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server"/>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
                <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="grdVoucherTypes" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
        </AjaxSettings>
      
    </telerik:RadAjaxManager>
  
      
    <table width="600px" style="margin: 0px auto">
    <tr><td style="text-align: center">
        <telerik:RadButton ID="RadButton1" runat="server" onclientclicked="RadButton1_Clicked" Text="CLick me to add new item" >
        </telerik:RadButton>
        </td>
        </tr>
        <tr>
        <td>
        <telerik:RadGrid ID="grdVoucherTypes" runat="server" AutoGenerateColumns="False" CellSpacing="0" Width="59%" style="margin-top:10px; margin-left: auto; margin-right: auto; margin-bottom: 0px;" AllowPaging="True" AllowSorting="True" GridLines="None" PageSize="20" >
                
            <MasterTableView ClientDataKeyNames="VoucherTypeID">
                <Columns>
                    <telerik:GridBoundColumn HeaderText="ID"  DataField="VoucherTypeID" UniqueName="VoucherTypeID" Visible="False" DataType="System.Int32" ReadOnly="True"/>
                    <telerik:GridBoundColumn HeaderText="Description" DataField="VoucherTypeDesc" UniqueName="VoucherTypeDesc">
                        <ItemStyle HorizontalAlign="Left"/>
                        <HeaderStyle Width="200px"></HeaderStyle>
                    </telerik:GridBoundColumn>
                </Columns>
  
</MasterTableView>
</telerik:RadGrid>
</td>
</tr>
    </table>
    </form>
</body>
</html>

Default.aspx.vb
Public Class _Default
    Inherits System.Web.UI.Page
  
    Public Property MyList() As List(Of VoucherType)
        Get
            Return Session("MyList")
        End Get
        Set(value As List(Of VoucherType))
            Session("MyList") = value
        End Set
    End Property
  
    Sub RadAjaxManager1_AjaxRequest(sender As Object, e As Telerik.Web.UI.AjaxRequestEventArgs)
  
        Dim x As New VoucherType
        x.VoucherTypeID = 10
        MyList.Add(x)
  
        grdVoucherTypes.Rebind()
  
    End Sub
  
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim x As New List(Of VoucherType)
        Dim y = New VoucherType
        y.VoucherTypeID = 1
        x.Add(y)
  
        MyList = x
  
        'grdVoucherTypes.DataSource = MyList
  
    End Sub
  
    Private Sub grdVoucherTypes_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdVoucherTypes.NeedDataSource
        grdVoucherTypes.DataSource = MyList
    End Sub
  
End Class
  
Public Class VoucherType
    Private m_VoucherTypeID As Long
    Private m_VoucherTypeDescription As String
    Private m_Value As Long
    Public IsNew As Boolean
  
  
    Public Property Value As Long
        Get
            Return m_Value
        End Get
        Set(value As Long)
            m_Value = value
        End Set
    End Property
  
    Public Property VoucherTypeID As Long
        Get
            Return m_VoucherTypeID
        End Get
        Set(value As Long)
            m_VoucherTypeID = value
        End Set
    End Property
  
    Public ReadOnly Property Available As Long
        Get
            Return 0
        End Get
    End Property
  
    Public ReadOnly Property Active As Long
        Get
            Return 0
        End Get
    End Property
  
    Public ReadOnly Property Expired As Long
        Get
            Return Available - Active
        End Get
    End Property
  
    Public Property VoucherTypeDescription As String
        Get
            Return m_VoucherTypeDescription
        End Get
        Set(value As String)
            m_VoucherTypeDescription = value
        End Set
    End Property
  
    Public ReadOnly Property VoucherTypeDesc As String
        Get
            Dim str As String
            str = Value
  
            Return "Test voucher"
        End Get
    End Property
  
End Class




2 Answers, 1 is accepted

Sort by
0
BabaYa
Top achievements
Rank 1
answered on 23 Mar 2012, 04:45 PM
Hi

I see you ar making Ajax on client clik of button. But maybe button make postback the same time. Try stop postback of button.

Thanks
BabaYa
0
Kim
Top achievements
Rank 1
answered on 24 Mar 2012, 04:15 AM

Oh man, that's exactly what it was, my bad!

Thank you so much!

Tags
Grid
Asked by
Kim
Top achievements
Rank 1
Answers by
BabaYa
Top achievements
Rank 1
Kim
Top achievements
Rank 1
Share this question
or