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

Pre selecting a checkbox

7 Answers 192 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Andy Green
Top achievements
Rank 2
Andy Green asked on 11 Apr 2012, 10:16 AM
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

7 Answers, 1 is accepted

Sort by
0
Kalina
Telerik team
answered on 11 Apr 2012, 12:32 PM
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.
0
Andy Green
Top achievements
Rank 2
answered on 11 Apr 2012, 01:26 PM
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
0
Princy
Top achievements
Rank 2
answered on 11 Apr 2012, 03:04 PM
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.
0
Andy Green
Top achievements
Rank 2
answered on 11 Apr 2012, 03:12 PM
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
0
Kalina
Telerik team
answered on 11 Apr 2012, 04:01 PM
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.
0
Accepted
Princy
Top achievements
Rank 2
answered on 11 Apr 2012, 04:16 PM
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.
0
Andy Green
Top achievements
Rank 2
answered on 11 Apr 2012, 04:34 PM
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








Tags
ComboBox
Asked by
Andy Green
Top achievements
Rank 2
Answers by
Kalina
Telerik team
Andy Green
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Share this question
or