Binding RadCombox with checkboxes

5 posts, 1 answers
  1. Frank Luswata
    Frank Luswata avatar
    3 posts
    Member since:
    Jan 2010

    Posted 13 Oct 2011 Link to this post


    How do i bind the concatenated texts of all checked item of the RadComboBox to a field in the SQLDatasource using the selectedvalue property? Am using the ASP.NET AJAX RadControls Q2 2011. 

    An example would be very welcome.

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 13 Oct 2011 Link to this post

    Hello Frank Luswata,

    You can get the concatenated value by the following code. Then you can insert the value using simple query.

    C#:
    protected void RadComboBox1_SelectedIndexChanged(object sender,Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
     {
       String s = RadComboBox1.Text;
     }

    Thanks,
    Princy.
  3. Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 13 Oct 2011 Link to this post

    Hi Frank Luswata,

    What is the exact scenario that you want to implement?
    Could you please explain it in more details and paste here some simplified code to illustrate it?

    Best wishes,
    Kalina
    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
  4. Frank Luswata
    Frank Luswata avatar
    3 posts
    Member since:
    Jan 2010

    Posted 13 Oct 2011 Link to this post

    Hi Kalina, 

    The scenario is that I have a RadcomboBox inside an insert template of a formview. The formview is bound to an SqlDataSource for inserting with two fields ItemId, ItemsSelected into a table.

    The selectedvalue of the RadcomboBox is bound to the field ItemsSelected. The dropp down values for the RadCombobox are selected from another table using another SqlDataSource. More than one item can be selected separated with commas using checkboxes=true. This works fine.

    The challenge is that when the insert button is click record, only the DataValueField of the first selected item is inserted into the table. How can I get it to insert the concatenated items (i.e all ticked items). The code is attached.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Test_Default" %>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
         <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
        </telerik:RadScriptManager>
        <div>
        
            <br />
            <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" 
                DefaultMode="Insert" Height="76px" Width="425px" DataKeyNames="ItemId">
                <InsertItemTemplate>
                    ItemId:
                    <asp:TextBox ID="ItemIdTextBox" runat="server" Text='<%# Bind("ItemId") %>' />
                    <br />
                    ItemsSelected:
                    <telerik:RadComboBox ID="RadComboBox1" runat="server" 
                        DataSourceID="SqlDataSource2" DataTextField="Source" 
                        DataValueField="SourceId" CheckBoxes="True" 
                        SelectedValue='<%# Bind("ItemsSelected") %>'>
                    </telerik:RadComboBox>
                    <br />
                    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                        CommandName="Insert" Text="Insert" />
                    &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
                        CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                </InsertItemTemplate>
            </asp:FormView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:CSFMonitoringConnectionString %>" 
                DeleteCommand="DELETE FROM [TestTable] WHERE [ItemId] = @ItemId" 
                InsertCommand="INSERT INTO [TestTable] ([ItemId], [ItemsSelected]) VALUES (@ItemId, @ItemsSelected)" 
                SelectCommand="SELECT [ItemId], [ItemsSelected] FROM [TestTable]" 
                UpdateCommand="UPDATE [TestTable] SET [ItemsSelected] = @ItemsSelected WHERE [ItemId] = @ItemId">
                <DeleteParameters>
                    <asp:Parameter Name="ItemId" Type="Int32" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="ItemsSelected" Type="String" />
                    <asp:Parameter Name="ItemId" Type="Int32" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="ItemId" Type="Int32" />
                    <asp:Parameter Name="ItemsSelected" Type="String" />
                </InsertParameters>
            </asp:SqlDataSource>
            <br />


        </div>
                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:CSFMonitoringConnectionString %>" 
                SelectCommand="SELECT [SourceId], [Source] FROM [tblInfoSource]">
                    </asp:SqlDataSource>
        </form>
    </body>
    </html>

  5. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 14 Oct 2011 Link to this post

    Hello Frank Luswata,

    I also faced the same issue when I tried with the SelectedValue same like that of your code. To overcome this just bind the Text property instead of SelectedValue of RadComboBox.

    ASPX:
    <telerik:RadComboBox ID="RadComboBox1" runat="server" DataSourceID="SqlDataSource1" DataTextField="CustomerID" DataValueField="CustomerID" CheckBoxes="True" Text='<%# Bind("ItemsSelected") %>'  >
    </telerik:RadComboBox>

    If you still want to bind the SelectedValue property, then another approach is to attach LinkButton click event and  store the RadComboBox text in a session variable and pass it as a session parameter. Here is the code.

    ASPX:
    <InsertParameters>
      <asp:Parameter Name="CustomerID" Type="Int32" />
      <asp:SessionParameter Name="sessionnm" DbType="String" SessionField="sessionnm" />
    </InsertParameters>

    C#:
    protected void InsertButton_Click(object sender, EventArgs e)
    {
       FormViewRow row = FormView1.Row;
       RadComboBox rbx = (RadComboBox)row.FindControl("RadComboBox1");
       Session["sessionnm"] = rbx.Text;
    }

    Thanks,
    Princy.
Back to Top