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

Duplicate Values with AppendDataBoundItems True

5 Answers 533 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Scott Marx
Top achievements
Rank 1
Scott Marx asked on 31 Aug 2011, 11:18 PM
I have a RadComboBox in web usercontrol and when I set the AppendDataBoundItems to true it duplicates the values.

.aspx Code
<telerik:RadComboBox ID="clientusersRadComboBox" runat="server" DataTextField="fullname" DataValueField="userid" AppendDataBoundItems="true" Height="150px">
    <Items>
        <telerik:RadComboBoxItem Text="Select User" />
    </Items>
</telerik:RadComboBox>

.aspx.cs Code
var rsClientUsers = from rsU in DB.tbl_users
    where rsU.clientid == intClientID && !(from rsL in DB.tbl_licenses
    where rsL.moduleid == intModuleID && rsL.clientid == intClientID
    select rsL.userid).Contains(rsU.userid)
    orderby rsU.firstname
    select new
    {
    fullname = rsU.firstname + ' ' + rsU.lastname,
    userid = rsU.userid
    };
 
clientusersRadComboBox.DataSource = rsClientUsers;
clientusersRadComboBox.DataBind();

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 01 Sep 2011, 09:28 AM
Hello Scott,

One suggestion is to switch ViewState off (enableViewState="false"). When ViewState is enabled, the contents are remembered each time a Postback happens, and it is repopulated with these remembered values.

If ViewState is switched off then this will no longer happen, because the values are not remembered between Postbacks. Instead, the instead it gets populated from scratch each time.

If you are populating the control for page load make sure that you have been checking for the !IsPostBack condition.
if (!IsPostBack )
{
    // Populate the RadCombo
}

This way we can make sure that the control populated only once.

Thanks,
Shinu.
0
Scott Marx
Top achievements
Rank 1
answered on 01 Sep 2011, 06:25 PM

Since the code is in a WebUserControl and is nested in a RadGrid it's always going to be loaded on a PostBack. So this is how I fixed the issue. I changed the code to just loop thru the record set and add the values to the RadComboBox.

clientusersRadComboBox.Items.Clear();
var rsClientUsers = from rsU in DB.tbl_users
    where rsU.clientid == intClientID && !(from rsL in DB.tbl_licenses
    where rsL.moduleid == intModuleID && rsL.clientid == intClientID
    select rsL.userid).Contains(rsU.userid)
    orderby rsU.firstname
    select new
    {
    fullname = rsU.firstname + ' ' + rsU.lastname,
    userid = rsU.userid
    };
 
RadComboBoxItem rcbItem1 = new RadComboBoxItem();
rcbItem1.Value = "0";
rcbItem1.Text = "Select User";
rcbItem1.Selected = true;
clientusersRadComboBox.Items.Add(rcbItem1);
 
foreach (var item in rsClientUsers)
{
    RadComboBoxItem rcbItem = new RadComboBoxItem();
    rcbItem.Value = item.userid.ToString();
    rcbItem.Text = item.fullname;
    rcbItem.Selected = true;
    clientusersRadComboBox.Items.Add(rcbItem);
}

 

0
David Cowan
Top achievements
Rank 1
answered on 22 Feb 2012, 04:46 PM
I see the same problem as the person above.  The key to this issue is AppendDataBoundItems and the combo box being in a UserControl.  If the combo is in a page it is fine.  If the combo is in a user control and AppendDataBoundItems is false it works fine.  Combine the 2 and you will get duplicate items.
0
Dimitar Terziev
Telerik team
answered on 27 Feb 2012, 09:34 AM
Hello Guys,

I've made a sample page with a RadComboBox in a user control with its AppendDataBoundItems property set to True and its items are not duplicated unless you rebind your control upon each post-back which then is the default behavior since you want the new items to be appended to already existing ones.

In case I'm missing something in your scenarios, then please someone open a support ticket and provide a runnable sample which we could debug locally.

All the best,
Dimitar Terziev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Shukhrat Nekbaev
Top achievements
Rank 1
answered on 30 May 2012, 01:04 PM
Hi Dimitar,

try making an ajax page that has a radgrid which in turn uses user control to insert/update entries.

User control's markup

<
telerik:RadComboBox ID="ddlGimletProductionPublishInstances" runat="server" Style="z-index: 10010" DataTextField="Name" DataValueField="Id" AutoPostBack="true" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlGimletProductionPublishInstances_SelectedIndexChanged" Width="278px" EmptyMessage="<%$ Resources:PGC, General_DdlPleaseSelectTitle %>" LoadingMessage="<%$ Resources:PGC, General_LoadingText %>"> </telerik:RadComboBox>

some code behind
private void BindGimletProductionPublishInstances()
{
    RadComboBoxItem tmpDefaultItem = new RadComboBoxItem();
    tmpDefaultItem.Text = Resources.PGC.Administration_CompaniesViewNoProductionPublishInstanceDefined;
    tmpDefaultItem.Value = "-1";
    tmpDefaultItem.Selected = true;
 
    ddlGimletProductionPublishInstances.ClearSelection();
    ddlGimletProductionPublishInstances.Items.Clear();
    ddlGimletProductionPublishInstances.Items.Add(tmpDefaultItem);
 
    ddlGimletProductionPublishInstances.DataSource = GimletPublishInstanceEntity.GetAllProductionGimletInstances();
    ddlGimletProductionPublishInstances.DataBind();
}
 
protected void Page_Load(object sender, EventArgs e)
{
    if (!HasOneTimeDatabindingHappened)
    {
        BindGimletProductionPublishInstances();
    }
}
 
protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
 
    if (!HasOneTimeDatabindingHappened)
        HasOneTimeDatabindingHappened = true;
}
 
 
private bool HasOneTimeDatabindingHappened
{
    get { return ViewState[WebUIConsts.Controls.HasOneTimeDatabindingHappened_Key] != null; }
    set { ViewState[WebUIConsts.Controls.HasOneTimeDatabindingHappened_Key] = value; }
}

(note that this is a copy-paste from existing project, not new project to test issue in separate environment)

I set breakpoint in BindGimletProductionPublishInstances and when it's hit I step and see items count, in the end of the method ddlGimletProductionPublishInstances.Items.Count says it has 2 items (as should), I let debugger go, expand combobox and see 3 items: first - my own item and next 2 are the same.

Controls: 2012.1.411.40

Workaround: set in markup AppendDataBoundItems="false"
and change BindGimletProductionPublishInstances method to add items manually:

private void BindGimletProductionPublishInstances()
{
    RadComboBoxItem tmpDefaultItem = new RadComboBoxItem();
    tmpDefaultItem.Text = Resources.PGC.Administration_CompaniesViewNoProductionPublishInstanceDefined;
    tmpDefaultItem.Value = "-1";
    tmpDefaultItem.Selected = true;
 
    ddlGimletProductionPublishInstances.ClearSelection();
    ddlGimletProductionPublishInstances.Items.Clear();
    ddlGimletProductionPublishInstances.Items.Add(tmpDefaultItem);
 
    var items = GimletPublishInstanceEntity.GetAllProductionGimletInstances().ToList();
    items.ForEach(item =>
    {
        RadComboBoxItem cbItem = new RadComboBoxItem();
        cbItem.Text = item.Name;
        cbItem.Value = item.Id.ToString();
 
        ddlGimletProductionPublishInstances.Items.Add(cbItem);
    });
}


Thanks!


Tags
ComboBox
Asked by
Scott Marx
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Scott Marx
Top achievements
Rank 1
David Cowan
Top achievements
Rank 1
Dimitar Terziev
Telerik team
Shukhrat Nekbaev
Top achievements
Rank 1
Share this question
or