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

Having difficulty with the basics

2 Answers 68 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Terri-Lynn
Top achievements
Rank 1
Terri-Lynn asked on 09 Nov 2011, 11:47 PM
Hi all:

I currently have the very basics set up with using the ComboBox that also allows users to select multiple items using a checkbox.  Code below:
<telerik:RadComboBox ID="RadComboBox1" runat="server" DataSourceID="myDS"
    DataTextField="fullname" DataValueField="id" oncopy="return false;" onpaste="return false;"
                    oncut="return false;" onkeypress="return tabOnly(event)" onmousewheel="return false"
                    OnClientDropDownOpening="OnClientDropDownOpening" OnClientDropDownClosing="OnClientDropDownClosing"
                    OnClientSelectedIndexChanging="OnClientSelectedIndexChanging" OnClientBlur="OnClientBlur"
                    Width="200px" AllowCustomText="True" ChangeTextOnKeyBoardNavigation="False">
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("fullname") %>' Checked="true"/>
                    </ItemTemplate>
</telerik:RadComboBox>
As you can see, by default ALL of the checkboxes are checked.  Not what I want/need.  For the life of me I can't seem to figure out how to bind my data.  Below is the sqldatasource I'm using for this list, which pulls ALL options, which is what I want, but when a user edits information, I want the items that should be selected... selected.
<asp:SqlDataSource ID="myDS" runat="server"
    ConnectionString="<%$ ConnectionStrings:myconnection %>"
    SelectCommand="SELECT DISTINCT contacts.id, contacts.fullname FROM contacts ORDER BY fullname">
</asp:SqlDataSource>
The query that pulls the "selected" contacts should look like this:
SELECT id, areaID, contactID FROM area_contact_map WHERE areaID = 6 AND (blnRemoved IS NULL OR blnRemoved = 0)
I've been reverse engineering all of the demos here on the site, but I can't seem to get this.  Any further help would be greatly appreciated.

Thanks,
TL

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 10 Nov 2011, 08:15 AM
Hello Terri,

You can try the following code snippet.

C#:
 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString());
 DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
 {
    String s = "select FirstName from Employees where EmployeeID=" + 1 + "";
    SqlDataAdapter dr = new SqlDataAdapter(s, con);
    dr.Fill(dt);
 }
protected void RadComboBox4_ItemDataBound(object sender, Telerik.Web.UI.RadComboBoxItemEventArgs e)
{
   foreach (DataRow row in dt.Rows)
    {
       string itms = row["FirstName"].ToString();
       if (e.Item.Text == itms)
        {
           {
             CheckBox chk = (CheckBox)e.Item.FindControl("CheckBox1");
              chk.Checked = true;
           }
        }
    }
}

Thanks,
Princy.
0
Terri-Lynn
Top achievements
Rank 1
answered on 10 Nov 2011, 07:45 PM
Thanks Princy!  You're response certainly helped me.  Now I'm stuck on how to save the newly "checked" items back to the database.  I know how to technically do the update and insert statements, but again, I'm lost on how one would filter out those items that the user has selected (more times than not multiple items will be selected).  Again, any further help will be greatly appreciated.  
Tags
ComboBox
Asked by
Terri-Lynn
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Terri-Lynn
Top achievements
Rank 1
Share this question
or