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

Unable to find radlistbox on asp.net client side button click event

3 Answers 184 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
rishi
Top achievements
Rank 1
rishi asked on 19 Nov 2019, 12:12 PM

Hi I have a radlistbox with SelectionMode="Multiselect". I want to get the count of checked items of listbox on button click. I tried to get the count by finding control on button click event but i am not able to find the control. 

HTML Code:
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Width="100%">

<div>

<asp:Table>

<asp:TableRow>
                <asp:TableHeaderCell>
                <telerik:RadLabel ID="RadLabel_abc" runat="server" Text="ABC"></telerik:RadLabel>
                </asp:TableHeaderCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell>

<telerik:RadListBox ID="rlbAbc" runat="server" 
            Height="100px"
            Width="480px"
            AllowTransfer="false"
            AllowTransferOnDoubleClick="false"
            TransferToID="rlbChosen"
            EnableDragAndDrop="true"
            OnClientTransferring="rlbAbc_OnClientTransferring"
            CheckBoxes="true"
            ButtonSettings-ShowTransferAll="false"
            EnableViewState="false"
            SelectionMode="Multiple"/>

</asp:TableCell>

</asp:TableRow>

</asp:Table>

</div>

</telerik:RadAjaxPanel>

 

.VB File

Protected Sub RadButton_AddToList_Click(sender As Object, e As EventArgs)

GetSelectedItemText("rlbABC")

End Sub

Public Function GetSelectedItemText(ByVal controlID As String) As String
        Dim theListbox As RadListBox = CType(FindControl(controlID), RadListBox)
        For idx As Integer = 0 To theListbox.Items.Count - 1
            Dim li As RadListBoxItem = theListbox.Items(idx)

            If theListbox.Items(idx).Selected = True Then
                Return theListbox.Items(idx).Text
            End If
        Next

        Return Nothing
    End Function

But I am not able to find control. Can anyone suggest a better way to do this. My main objective is to get count of checked items of list box on button click

3 Answers, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 22 Nov 2019, 08:15 AM

Hi Rishi,

RadListBox has properties such as the "CheckedItems" and "SelectedItems" which return the items collection regarding their checked or selected state. You can count them like this:

Dim checkedItems = RadListBox1.CheckedItems.Count
Dim selectedItems = RadListBox1.SelectedItems.Count

 

Please try out the following example. Copy the code into a WebForms page and test it out:

<telerik:RadListBox ID="RadListBox1" runat="server"
    CheckBoxes="true"
    EnableViewState="false"
    SelectionMode="Multiple">
    <Items>
        <telerik:RadListBoxItem Text="Item 1" />
        <telerik:RadListBoxItem Text="Item 2" />
        <telerik:RadListBoxItem Text="Item 3" />
        <telerik:RadListBoxItem Text="Item 4" />
        <telerik:RadListBoxItem Text="Item 5" />
    </Items>
</telerik:RadListBox>
<br />
<br />
<telerik:RadButton runat="server" ID="RadButton1" Text="Count Checked Items" AutoPostBack="true" OnClick="RadButton1_Click" />
<br />
<br />
<telerik:RadLabel ID="RadLabel1" runat="server"></telerik:RadLabel>

 

VB code:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        CountCheckedSelectedItems()
    End If
End Sub
Protected Sub RadButton1_Click(sender As Object, e As EventArgs)
    CountCheckedSelectedItems()
End Sub

Private Sub CountCheckedSelectedItems()
    RadLabel1.Text = String.Format("Number of Checked Items: {0}<br />", RadListBox1.CheckedItems.Count)
    RadLabel1.Text &= String.Format("Number of Selected Items: {0}<br />", RadListBox1.SelectedItems.Count)
End Sub

 

Kind regards,
Attila Antal
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
rishi
Top achievements
Rank 1
answered on 02 Dec 2019, 12:24 PM

Hi Attila,

Thanks for your response but this thing is not working for me. I will show you how i am binding values to radlistbox and accordingly please suggest me something.

VBCode:
Page Load:
If Not IsPostBack Then
            getinfo()
        End If

Private Sub getinfo()
        Dim connectionstring = ConfigurationManager.ConnectionStrings("connectionstring1").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(connectionstring)
        Dim da As New SqlDataAdapter
        Dim cmd As New SqlCommand
        cmd.CommandText = "select * from abc"
        da.SelectCommand = cmd
        da.SelectCommand.Connection = conn
        da.Fill(dt_channel)

        For i As Integer = 0 To dt.Rows.Count - 1 - 1
            Dim item As New RadListBoxItem()
            item.Text = dt.Rows(i).Item("abc").ToString()
            item.Value = dt.Rows(i).Item("pqr").ToString()
            rlbabc.Items.Add(item)
        Next
    End Sub

Protected Sub btn_pqr_Click(sender As Object, e As EventArgs) Handles btn_pqr.Click
        getabc(rlbabc)
    End Sub
Private Sub getabc(ByVal listbox As RadListBox)
        Dim sb As New StringBuilder()
        Dim collection As IList(Of RadListBoxItem) = listbox.CheckedItems
        For Each item As RadListBoxItem In collection
            sb.Append(item.Value + "<br/>")
        Next
    End Sub

I am getting count of checkedItems always = 0

0
Attila Antal
Telerik team
answered on 04 Dec 2019, 12:58 PM

Hi Rishi,

Thank you for the additional information. The reason there aren't selected/checked items in the server side is due to the ViewState being disabled. ViewState holds the information that later accessed by the control, and if its disabled, client-state will not longer preserved and transferred to the server.

Telerik controls heavily rely on the ViewState, and I advise to turn it back on the get the best out of them.

Here is a working sample:

<telerik:RadListBox ID="rlbabc" runat="server"
    Height="100px"
    Width="480px"
    AllowTransfer="false"
    AllowTransferOnDoubleClick="false"
    TransferToID="rlbChosen"
    EnableDragAndDrop="true"
    OnClientTransferring="rlbAbc_OnClientTransferring"
    CheckBoxes="true"
    ButtonSettings-ShowTransferAll="false"
    EnableViewState="true"
    SelectionMode="Multiple">
</telerik:RadListBox>
<telerik:RadListBox ID="rlbChosen" runat="server"></telerik:RadListBox>
<telerik:RadButton runat="server" ID="btn_pqr" Text="PQR" AutoPostBack="true" OnClick="btn_pqr_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

<script>
    function rlbAbc_OnClientTransferring(sender, args) {

    }
</script>

 

VB - Code behind. I would like to note that best practices is to bind data to the ListBod and let it handle populating the items automatically. 

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        getinfo()
    End If
End Sub

Private Sub getinfo()
    Dim dt = New DataTable()

    dt.Columns.Add("ID", Type.GetType("System.Int32"))
    dt.Columns.Add("abc", Type.GetType("System.String"))
    dt.Columns.Add("pqr", Type.GetType("System.String"))

    For index = 0 To 10 - 1
        Dim dr = dt.NewRow()

        dr("ID") = index
        dr("abc") = "abc " & index
        dr("pqr") = "pqr" & index

        dt.Rows.Add(dr)
    Next


    rlbabc.DataSource = dt
    rlbabc.DataValueField = "pqr"
    rlbabc.DataTextField = "abc"
    rlbabc.DataBind()
End Sub

Protected Sub btn_pqr_Click(sender As Object, e As EventArgs)
    getabc(rlbabc)
End Sub

Private Sub getabc(ByVal listbox As RadListBox)
    Dim sb As New StringBuilder()
    Dim collection As IList(Of RadListBoxItem) = listbox.CheckedItems
    For Each item As RadListBoxItem In collection
        sb.Append(item.Value + "<br/>")
    Next
    Label1.Text = sb.ToString()
End Sub

 

Kind regards,
Attila Antal
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
General Discussions
Asked by
rishi
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
rishi
Top achievements
Rank 1
Share this question
or