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

duplicated items in RadListBox

1 Answer 140 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
achuchu
Top achievements
Rank 1
achuchu asked on 30 Nov 2010, 08:55 PM

Hi

As I was adding more data in the radgrid, it does a postback , however, the items in the radlistbox is duplicated
everytime there is a postbac, attached is the code. To run, click the -> to add data to the right hand side radlistbox,
and add a row to the radgrid, once finished, click the button at the bottom, you can see the items in the radlistbox
is duplicated, sometimes even tripled, please help,this is an urgent issue.

thanks, -regards, Al




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
  
/// <summary>
/// Summary description for Auto
/// </summary>
public class Auto
{
      
        //
        // TODO: Add constructor logic here
        //
         public int VIN { get; set; }
        public string Model { get; set; }
        public string Make { get; set; }
        public int HiddenMileage { get; set; }
  
      
}




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 
{
    public List<Auto> Cars;
    public List<Auto> destCars = new List<Auto>();
  
    protected void Page_Load(object sender, EventArgs e)
    {
  
        if (!IsPostBack)
        {
              
            //Grid
            LoadCars();
  
            //transfer boxes
            this.lbAOBSource.ItemDataBound += new RadListBoxItemEventHandler(lbAOBDestination_ItemDataBound);
            this.lbAOBDestination.Transferring += new RadListBoxTransferringEventHandler(lbAOBSource_Transferring);
            lbAOBDestination.Updating += new RadListBoxUpdatingEventHandler(lbAOBDestination_Updating);
            lbAOBDestination.PreRender += new EventHandler(lbAOBDestination_PreRender);
            lbAOBDestination.Inserting += new RadListBoxInsertingEventHandler(lbAOBDestination_Inserting);
            lbAOBDestination.DataBinding += new EventHandler(lbAOBDestination_DataBinding);
            //lbAOBDestination.
  
            this.lbAOBDestination.RegisterWithScriptManager = false;
  
            this.lbAOBSource.DataValueField = "VIN";
            this.lbAOBSource.DataTextField = "Model";
  
  
            //set the object in session state
            this.Session["Cars"] = Cars;
  
            //set the destination list
        }
        else
        {
            Cars = (List<Auto>)this.Session["Cars"];
        }
        this.lbAOBSource.DataSource = Cars;
        this.lbAOBSource.DataBind();
  
    }
  
    void lbAOBDestination_DataBinding(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
    }
  
    void lbAOBDestination_Inserting(object sender, RadListBoxInsertingEventArgs e)
    {
        //throw new NotImplementedException();
    }
  
    void lbAOBDestination_PreRender(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
    }
  
    void lbAOBDestination_Updating(object sender, RadListBoxUpdatingEventArgs e)
    {
        //throw new NotImplementedException();
    }
  
    void lbAOBSource_Transferring(object sender, RadListBoxTransferringEventArgs e)
    {
        //throw new NotImplementedException();
    }
  
    void lbAOBDestination_ItemDataBound(object sender, RadListBoxItemEventArgs e)
    {
        //throw ne//w NotImplementedException();
    }
  
  
    private void LoadCars()
    {
        Cars = new List<Auto>();
  
        Auto Car = new Auto();
        Car.Make = "Ford";
        Car.Model = "Fiesta";
        Car.HiddenMileage = 100000;
        Car.VIN = 101;
        Cars.Add(Car);
  
        Car = new Auto();
        Car.Make = "Isuzu";
        Car.Model = "Rodeo";
        Car.HiddenMileage = 5000;
        Car.VIN = 102;
        Cars.Add(Car);
  
        Car = new Auto();
        Car.Make = "Honda";
        Car.Model = "Civic";
        Car.HiddenMileage = 200000;
        Car.VIN = 103;
        Cars.Add(Car);
  
        Car = new Auto();
        Car.Make = "chevy";
        Car.Model = "Cobalt";
        Car.HiddenMileage = 100000;
        Car.VIN = 104;
        Cars.Add(Car);
    }
  
    protected void GridCars_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        GridCars.MasterTableView.DataSource = Cars;
        //GridCars.DataSource = Cars;
        //GridCars.DataBind();
    }
  
    protected void GridCars_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        Auto newCar = new Auto();
        GridDataItem gdiItem = (GridDataItem)e.Item.DataItem;
        newCar.VIN = int.Parse(((TextBox)e.Item.FindControl("txtVIN")).Text);
        newCar.Make = ((TextBox)e.Item.FindControl("txtMake")).Text;
        newCar.Model = ((TextBox)e.Item.FindControl("txtModel")).Text;
        newCar.HiddenMileage = int.Parse(((TextBox)e.Item.FindControl("txtMileage")).Text);
        if (Cars == null)
        {
            Cars = new List<Auto>();
            LoadCars();
        }
        Cars.Add(newCar);
        this.Session["Cars"] = Cars;
        //e.ExecuteCommand("Cancel");
    }
  
    protected void GridCars_PreRender(object sender, EventArgs e)
    {
  
        GridCars.MasterTableView.Rebind();
        //GridCars.Rebind();
    }
  
    protected void GridCars_ItemCommand(object source, GridCommandEventArgs e)
    {
  
    }
  
    protected void GridCars_ItemInserted(object source, GridInsertedEventArgs e)
    {
  
    }
  
    protected void GridCars_DeleteCommand(object source, GridCommandEventArgs e)
    {
  
    }
  
    protected void GridCars_EditCommand(object source, GridCommandEventArgs e)
    {
  
    }
  
    protected void GridCars_UpdateCommand(object source, GridCommandEventArgs e)
    {
  
    }
  
  
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "There are " + lbAOBDestination.Items.Count + " items in the destination";
    }
}






<%@ 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">
  
<head runat="server">
    <title></title>
    <telerik:RadStyleSheetManager id="RadStyleSheetManager1" runat="server" />
</head>
<body>
    <form id="form1" runat="server">
      
       <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
      
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
      <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="GridCars">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="GridCars" LoadingPanelID="RadAjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
      
      
      <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
        
        <telerik:RadGrid ID="GridCars" runat="server" AutoGenerateEditColumn="True" AutoGenerateDeleteColumn="True"
        AllowAutomaticInserts="False" AllowAutomaticDeletes="False" AllowAutomaticUpdates="False"
        Skin="Office2007" OnNeedDataSource="GridCars_NeedDataSource" OnPreRender="GridCars_PreRender"
        OnInsertCommand="GridCars_InsertCommand" OnItemCommand="GridCars_ItemCommand"
        OnItemInserted="GridCars_ItemInserted" OnDeleteCommand="GridCars_DeleteCommand"
        OnEditCommand="GridCars_EditCommand" OnUpdateCommand="GridCars_UpdateCommand">
        <MasterTableView CommandItemDisplay="Top" EditMode="PopUp" AutoGenerateColumns="True"
            DataKeyNames="VIN">
            <EditFormSettings InsertCaption="Add Auto" CaptionFormatString="Edit VIN: {0}" CaptionDataField="VIN"
                EditFormType="Template" PopUpSettings-Modal="true">
                <FormTemplate>
                    <table id="Table1" cellspacing="1" cellpadding="1" width="250" border="0">
                        <tr>
                            <td>
                            </td>
                            <td>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                VIN:
                            </td>
                            <td>
                                <asp:TextBox ID="txtVin" Text='<%# Bind( "VIN") %>' runat="server">
                                </asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Make:
                            </td>
                            <td>
                                <asp:TextBox ID="txtMake" Text='<%# Bind( "Make") %>' runat="server">
                                </asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Model:
                            </td>
                            <td>
                                <asp:TextBox ID="txtModel" Text='<%# Bind( "Model") %>' runat="server">
                                </asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Mileage:
                            </td>
                            <td>
                                <asp:TextBox ID="txtMileage" Text='<%# Bind( "HiddenMileage") %>' runat="server">
                                </asp:TextBox>
                            </td>
                        </tr>
                    </table>
                    <table style="width: 100%">
                        <tr>
                            <td align="right" colspan="2">
                                <asp:Button ID="Button1" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                    runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                                </asp:Button
                                <asp:Button ID="Button2" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel">
                                </asp:Button>
                            </td>
                        </tr>
                    </table>
                </FormTemplate>
                <PopUpSettings Modal="True"></PopUpSettings>
            </EditFormSettings>
        </MasterTableView>
    </telerik:RadGrid>
      
      
    <telerik:RadListBox ID="lbAOBSource" runat="server" TransferToID="lbAOBDestination"
        AllowTransfer="True" Height="150" Width="360" Skin="Office2007" AllowTransferOnDoubleClick="true"
        AllowTransferDuplicates="false" SelectionMode="Multiple">
        <ButtonSettings TransferButtons="Common" />
    </telerik:RadListBox>
    <telerik:RadListBox ID="lbAOBDestination" runat="server" Height="150" Width="300"
        AllowTransferDuplicates="false" Skin="Office2007" AllowTransferOnDoubleClick="true"
        SelectionMode="Multiple" />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Click Me" 
        onclick="Button1_Click" />
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    <div>
  
    </div>
    </form>
</body>
</html>

1 Answer, 1 is accepted

Sort by
0
Dimitar Terziev
Telerik team
answered on 03 Dec 2010, 03:01 PM
Hi Achuchu,

The problem is that AJAX manager doesn't update the listbox controls.
I'm sending you a page with your code where AJAX properties are set  and everything is working as it should be.



All the best,
Dimitar Terziev
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Ajax
Asked by
achuchu
Top achievements
Rank 1
Answers by
Dimitar Terziev
Telerik team
Share this question
or