FindItems and SelectedItems Issues

5 posts, 1 answers
  1. FEST
    FEST avatar
    31 posts
    Member since:
    Jul 2011

    Posted 07 Jul 2011 Link to this post

    Hi forum!

    i'm having some trouble making a combobox select an item knowing its value. I've seen demos and documentation and i cloned the examples but its not working for me. I don't know what im doing wrong.

     When i select an item from my combobox1, even in the RadComboBox1_SelectedIndexChanged the selectedIndex is always = -1 so this way i can't know the index of the item and use it for other things.
    So to try and selecte an item in the radcombobox2 i go for the value of the item selected in the radcombobox1. This value comes right but when i try RadComboBox2.SelectedIndex = RadComboBox2.FindItemIndexByValue(boletim_id) the index i receive from the find method is -1 and i know that exists an item with the boletim_id value in the radcombobox2 because the datasource in both radcombobox is the same. The same will happen if i try the finditemindexbytext or finditembyvalue or finditembytext.

    File Code (ASP)
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_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 runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
        </telerik:RadScriptManager>
        <div>
          
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:Processos8ConnectionString %>" 
                SelectCommand="SELECT [Sinistrado], [Sinistrado_ID] FROM [Sinistrados]">
            </asp:SqlDataSource>
          
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                ConnectionString="<%$ ConnectionStrings:Processos8ConnectionString %>" 
                SelectCommand="SELECT [Sinistrado_ID], [Sinistro_ID], [Tomador_ID], [Boletim_id] FROM [Boletins]">
            </asp:SqlDataSource>
              
          
        </div>
        <telerik:RadComboBox ID="RadComboBox1" Runat="server" 
            DataSourceID="SqlDataSource1" DataTextField="Sinistrado" 
            DataValueField="Sinistrado_ID" AllowCustomText="True" 
            AutoCompleteSeparator="|" AutoPostBack="True" ItemsPerRequest="20" 
            MarkFirstMatch="True" ShowMoreResultsBox="True" 
            EnableAutomaticLoadOnDemand="True">
        </telerik:RadComboBox>
        <telerik:RadTextBox ID="RadTextBox1" Runat="server">
        </telerik:RadTextBox>
        <telerik:RadTextBox ID="RadTextBox2" Runat="server">
        </telerik:RadTextBox>
        <telerik:RadComboBox ID="RadComboBox2" Runat="server" 
            DataSourceID="SqlDataSource1" DataTextField="Sinistrado" 
            DataValueField="Sinistrado_ID" AllowCustomText="True" 
            AutoCompleteSeparator="|" AutoPostBack="True" ItemsPerRequest="20" 
            MarkFirstMatch="True" ShowMoreResultsBox="True" 
            EnableAutomaticLoadOnDemand="True">
        </telerik:RadComboBox>
        <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" 
                DataSourceID="SqlDataSource2" GridLines="None" 
            AutoGenerateColumns="False">
    <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
      
    <MasterTableView datakeynames="Boletim_id" 
                    datasourceid="SqlDataSource2">
    <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
      
    <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
    <HeaderStyle Width="20px"></HeaderStyle>
    </RowIndicatorColumn>
      
    <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
    <HeaderStyle Width="20px"></HeaderStyle>
    </ExpandCollapseColumn>
      
        <Columns>
            <telerik:GridButtonColumn Text="Select" CommandName="Select" ButtonType="ImageButton" ImageUrl="~/images/select_16_mouse.png">
            </telerik:GridButtonColumn>
            <telerik:GridBoundColumn DataField="Sinistrado_ID" DataType="System.Int32" 
                FilterControlAltText="Filter Sinistrado_ID column" HeaderText="Sinistrado_ID" 
                SortExpression="Sinistrado_ID" UniqueName="Sinistrado_ID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Sinistro_ID" DataType="System.Int32" 
                FilterControlAltText="Filter Sinistro_ID column" HeaderText="Sinistro_ID" 
                SortExpression="Sinistro_ID" UniqueName="Sinistro_ID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Tomador_ID" DataType="System.Int32" 
                FilterControlAltText="Filter Tomador_ID column" HeaderText="Tomador_ID" 
                SortExpression="Tomador_ID" UniqueName="Tomador_ID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Boletim_id" DataType="System.Int32" 
                FilterControlAltText="Filter Boletim_id column" HeaderText="Boletim_id" 
                ReadOnly="True" SortExpression="Boletim_id" UniqueName="Boletim_id">
            </telerik:GridBoundColumn>
        </Columns>
      
    <EditFormSettings>
    <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
    </EditFormSettings>
    </MasterTableView>
      
            <ClientSettings>
                <Selecting AllowRowSelect="True" />
            </ClientSettings>
      
    <FilterMenu EnableImageSprites="False"></FilterMenu>
            </telerik:RadGrid>
          
        </form>
    </body>
    </html>

    File Code(VB.NET)
    Imports Telerik.Web.UI
    Imports System.IO
    Imports System.Data.SqlClient
    Imports System.Configuration
    Imports System.Data
    Imports System.Net
    Partial Class _Default
        Inherits System.Web.UI.Page
      
        Protected Sub RadComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs) Handles RadComboBox1.SelectedIndexChanged
            SqlDataSource2.SelectCommand = "SELECT [Sinistrado_ID], [Sinistro_ID], [Tomador_ID], [Boletim_id] FROM [Boletins] WHERE SINISTRADO_ID = " & RadComboBox1.SelectedValue
            RadGrid1.DataBind()
            RadComboBox1.SelectedIndex = RadComboBox1.SelectedIndex
        End Sub
      
        Protected Sub RadGrid1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.SelectedIndexChanged
            Dim boletim_id As String = DirectCast(RadGrid1.SelectedItems.Item(0), GridDataItem).Cells(6).Text.ToString
            RadTextBox1.Text = DirectCast(RadGrid1.SelectedItems.Item(0), GridDataItem).Cells(5).Text.ToString
            RadTextBox2.Text = DirectCast(RadGrid1.SelectedItems.Item(0), GridDataItem).Cells(4).Text.ToString
            Dim x As Integer = RadComboBox2.FindItemIndexByValue(boletim_id)
            'Dim z As Integer = RadComboBox1.SelectedItem.Index
            RadComboBox1.Text = ""
            RadComboBox1.SelectedValue = ""
            RadComboBox2.SelectedIndex = RadComboBox2.FindItemIndexByValue(boletim_id)
            RadComboBox2.SelectedIndex = RadComboBox1.SelectedItem.Index
        End Sub
    End Class


    Many Thanks,
    FEST
  2. FEST
    FEST avatar
    31 posts
    Member since:
    Jul 2011

    Posted 08 Jul 2011 Link to this post

    Hi!

    I'm sorry for doing a double post buti have knews. I kind solved my problem. I was giving o my radcombobox1 (rcb_sinistrados) the data with a datasource, this datasource id no filled the combo wth items but it showed them (dnt understand how that is possible but ichecked a thousand times the items and it was empty), so to fill the items i did i what i used to do in asp comboboxes, i created a sub
    Private Sub carregar_combos()
        Dim strligacao As String = ConfigurationManager.ConnectionStrings("tester2").ConnectionString
        Dim ligacao As SqlConnection = New SqlConnection(strligacao)
        Dim SqlComm As String = "SELECT [Sinistrado], [Sinistrado_ID] FROM [Sinistrados]"
        Dim comand1 As New SqlCommand(SqlComm, ligacao)
        ligacao.Open()
        Try
            Dim dr1 As SqlDataReader
            dr1 = comand1.ExecuteReader
            While dr1.Read
                Dim instance As New RadComboBoxItem(dr1.GetValue(0), dr1.GetValue(1))
                rcb_sinistrado.Items.Add(instance)
            End While
            dr1.Close()
            ligacao.Close()
        Catch ex As Exception
        End Try
    End Sub

    Here i fill the items of my combobox but now i have a problem thatthe first time i request the items (i click the arrow of the combo or start typing something) it takes loads od time to open because its loading everything. This would be understandable had i not put the
    rcb_sinistrado.EnableAutomaticLoadOnDemand = True
    rcb_sinistrado.LoadingMessage = "A carregar..."
    rcb_sinistrado.ItemsPerRequest = 20
    rcb_sinistrado.Filter = RadComboBoxFilter.Contains
    rcb_sinistrado.ShowMoreResultsBox = True
    before calling the sub routime i showed. these parameters are also in html tags so i don't understand why is this happening.

    Many Thanks,
    FEST
  3. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1268 posts

    Posted 12 Jul 2011 Link to this post

    Hello Fest,

    The reason for the experience behavior is due to the fact that when you use load on demand the items of the RadCombobox are not persisted on the server-side and this is way you could not find a particular item either by text, value or index. You could only access the Text or SelectedValue of the RadComboBox.

    Regarding your current issue could you clarify what is the exact implementation that you are using, whether you are using AutomaticLoadOnDemand or not. In case you are using it and you have set the ItemsPerRequest property then the RadComboBox shouldn't be loading all of the items from the data source, but the size set in this property.

    Best wishes,
    Dimitar Terziev
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  4. FEST
    FEST avatar
    31 posts
    Member since:
    Jul 2011

    Posted 13 Jul 2011 Link to this post

    Hi Admin

    Sorry for my delay. My combo has the atomaticloadoddemand = True, ItemsPerRequest = 20, ShowMoreResults = True, AllowCostumText = True, AutoPostBack = True and the rest i think its not relevant. I did not know that the load meant that so i guess if i either put automaticloadondemand = false or autopostback = false that wont happen and since i want the items forever i should put the automaticloadondemand to false (going to try that). Please, if this is not the information you were after, say so, sometimes i dont get eng that well :S and im really sorry for that.

    Many thanks,
    FEST
  5. Answer
    Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1268 posts

    Posted 18 Jul 2011 Link to this post

    Hello Fest,

    If you want the items of the RadCombobox to be persisted on the server and to be available all the time you should not use the load on demand mechanism. You just have to bind your RadComboBox to a data source and this way you will be able to find and manipulate its items on the server-side.

    Please refer to the following help article giving more information about the load on demand.

    Greetings,
    Dimitar Terziev
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Back to Top