Having difficulty with the basics

3 posts, 1 answers
  1. Terri-Lynn
    Terri-Lynn avatar
    45 posts
    Member since:
    Dec 2010

    Posted 09 Nov 2011 Link to this post

    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. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 10 Nov 2011 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Terri-Lynn
    Terri-Lynn avatar
    45 posts
    Member since:
    Dec 2010

    Posted 10 Nov 2011 Link to this post

    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.  
Back to Top