Pre selecting a checkbox

8 posts, 1 answers
  1. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 11 Apr 2012 Link to this post

    Hi All

    I'm using a RadComboBox with checkboxes. How do I pre select one of the checkbox items?

    I'm setting the variable like this:

    ConsultantCode = dl_ds.Tables(0).Rows(0)("ConsultantCode")

    I have tried ddlSpecialty.selectedvalue = ConsultantCode. but nothing happens

    And

    Dim chk1 As CheckBox = DirectCast(ddlSpecialty.FindItemByText(ConsultantCode).FindControl("CheckBox"), CheckBox)
    chk1.Checked = True
    but chk1 alwas returns nothing

    Andy
  2. Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 11 Apr 2012 Link to this post

    Hello Andy Green,

    The RadComboBoxItem object has a "Checked" property - you can simply set it to "true".
    Please find more details about the CheckBoxes feature in "CheckBox Support" help article

    Greetings,
    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 11 Apr 2012 Link to this post

    As you can see in my OP I have tried this approach. How do I identify the checkbox to set check = True

    I cant see in any of your examples where to pre select, only what happens after a selection is made.

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

    Posted 11 Apr 2012 Link to this post

    Hi Andy Green,

    I tried the following code to preselect a RadComboBoxItem with CheckBox.

    ASPX:
    <telerik:RadComboBox ID="RadComboBox1" runat="server" AutoPostBack="true" DataSourceID="SqlDataSource1" DataTextField="FirstName" DataValueField="FirstName">
        <ItemTemplate>
         <asp:CheckBox ID="chk" runat="server" Text='<%# DataBinder.Eval(Container, "Text") %>' />
        </ItemTemplate>
    </telerik:RadComboBox>

    C#:
    protected void Page_Load(object sender, EventArgs e)
     {
            RadComboBox1.DataBind();
            String str = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(str);
            String command = "SELECT FirstName FROM Employees where FirstName='Alex'";
            SqlDataAdapter ad = new SqlDataAdapter(command, con);
            DataSet dt = new DataSet();
            ad.Fill(dt);
            string s = dt.Tables[0].Rows[0].ItemArray[0].ToString();
            RadComboBox1.SelectedValue = s;
            CheckBox ch = (CheckBox)RadComboBox1.FindItemByText(s).FindControl("chk");
            ch.Checked = true;
     }

    Please provide your code if it doesn't helps.

    Thanks,
    Princy.
  6. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 11 Apr 2012 Link to this post

    Does this mean that if using  Checkboxes=true you cant pre select, you have to use a template as you have shown.

    I would have though this was an omission in the control if this is the case.

    Please confirm before I rewrite sections of my code.

    Andy
  7. Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 11 Apr 2012 Link to this post

    Hi Andy Green,

    Could you please explain your scenario in more details and paste here the RadComboBox markup that you use?
    How do you populate the RadComboBox with data?
    Is the combo nested within another parent control?

    All the best,
    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.
  8. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 11 Apr 2012 Link to this post

    Hi Andy Green,

    I have tried another code by setting CheckBoxes property to true as follows.

    ASPX:
    <telerik:RadComboBox ID="RadComboBox1" runat="server" AutoPostBack="true" DataSourceID="SqlDataSource1" CheckBoxes="true" DataTextField="FirstName" DataValueField="FirstName">
    </telerik:RadComboBox>

    VB:
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        RadComboBox1.DataBind()
        Dim str As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString
        Dim con As New SqlConnection(str)
        Dim command As [String] = "SELECT FirstName FROM Employees where FirstName='Alex'"
        Dim ad As New SqlDataAdapter(command, con)
        Dim dt As New DataSet()
        ad.Fill(dt)
        Dim s As String = dt.Tables(0).Rows(0).ItemArray(0).ToString()
        RadComboBox1.SelectedValue = s
        Dim x As RadComboBoxItem = DirectCast(RadComboBox1.SelectedItem, RadComboBoxItem)
        x.Checked = True
    End Sub

    Hope this helps.

    Thanks,
    Princy.
  9. Andy Green
    Andy Green avatar
    342 posts
    Member since:
    Aug 2008

    Posted 11 Apr 2012 Link to this post

    Thanks Princy, I will try your approach.

    In the meantime this is the info Kalina requested.
    Combo box markup:
    <telerik:RadComboBox ID="ddlSpecialty" runat="server"   EnableEmbeddedSkins="false" Skin="Activity" Width="185px" Height="300px" emptyMessage="Click to change Specialties" CheckBoxes="true" OnClientBlur="refreshGrid"></telerik:RadComboBox>

    Data binding:
    Try
        'Specialty
        Dim s As New CoreData
        Dim s_ds As Data.DataTable = s.SpecialtyByLocation(Location_ID, User_GUID)
        With ddlSpecialty
            .Items.Clear()
            .DataSource = s_ds
            .DataTextField = "OutcomeFormName"
            .DataValueField = "OutcomeForm_ID"
            .DataBind()
        End With
    Catch ex As Exception
        WriteError("SpecialtyByLocation", ex.Message)
    End Try

    I then get a value from somewhere else that I would like to be the default for the check box.

    ie ddlSpecialty.SelectedValue = ConsultantSpecialty

    Andy








Back to Top
UI for ASP.NET Ajax is Ready for VS 2017