Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a checkboxlist populated from a datasource which contains all newspapers. a second datasource which i want to view and compare its select data, contains favorites newspapers.

i want checkboxes preselected according to newspaper ids from the second datasources. its select method returns data as ienumarable. and the values of checkboxes are strings.

how can i compare the values belong to checkboxes and the select data from the second datasource ?

i try the following solution but none of cbs is checked :

C#
protected void CheckBoxList1_DataBound(object sender, EventArgs e)
        {
            IEnumerable results = FavoritesDataSource.Select();
            foreach (var item in results)
            {
                foreach (ListItem cb in CheckBoxList1.Items)
                {
                    if (item.ToString()== cb.Value)
                    {
                        cb.Selected = true;
                    }
                }
            }


XML
cb is holding newspaper names as text and ids as value. so i want to compare ids.
this is the datasource ( favorites datasource )
 Collapse
<asp:ObjectDataSource ID="FavoritesDataSource" runat="server"
    OldValuesParameterFormatString="original_{0}"
    SelectMethod="GetfavoritesByContactId" TypeName="myapp.FavoritesBLL">
    <SelectParameters>
        <asp:QueryStringParameter Name="contactid" QueryStringField="contactid"
            Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>
this is the checkboxlist populated with another data source (newspapers data source ). there is no problem with that, all cbs are populated.
 Collapse
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
        DataSourceID="NewspaperDatasource" DataTextField="npName"
        DataValueField="newspaperid" ondatabound="CheckBoxList1_DataBound">
    </asp:CheckBoxList>
and this is the code i try to compare newspaper ids.
 Collapse
protected void CheckBoxList1_DataBound(object sender, EventArgs e)
        {
            IEnumerable results = FavoritesDataSource.Select();
            foreach (var item in results)
            {
                foreach (ListItem cb in CheckBoxList1.Items)
                {
                    if (item.ToString()== cb.Value)
                    {
                        cb.Selected = true;
                    }
                }
            }
        }
i think there's a problem with ienumerable.
Posted
Updated 5-Jan-11 8:42am
v2
Comments
Manfred Rudolf Bihy 6-Jan-11 15:59pm    
What exactly does the method FavoritesDataSource.Select() return? What is kind of IEnumerable<elementtypehere> is it?
If we know what type elementTypeHere is we stand a much better chance of choosing the correct property to compare it to cb.Text.
Manfred Rudolf Bihy 6-Jan-11 16:06pm    
See also the modifications I made to my answer. (added footnotes)
tyu 7-Jan-11 5:04am    
return type is favoritesdatatable from the BLL. and the query in the table adapter returns only newspaper ids from the favorites table. favorites table has two columns, contactid and favorites id. so query returns favorite newspapers's id for a given contactid
Manfred Rudolf Bihy 7-Jan-11 12:35pm    
OK! So one problem is, does item.ToString() give you the value from column favoritesid?
Next problem is then how to get the id from the CheckBoxes. CheckBox.Text I take it contains the name of the paper. Is that true?

1 solution

I'm only guessing here so you'll have to try it out yourself, but it would seem that the item.ToString()1. gives you the name of one of the favorite newspapers and in all the checkboxes the name of the newspaper
would be in the property cb.Text.

This is all guesswork as we didn't see the code yet that initialized the asp:checkbox instances and neither the code that reads in all the newspapers. I still think that cb.Text probably does the trick.

C#
protected void CheckBoxList1_DataBound(object sender, EventArgs e)
        {
            IEnumerable results = FavoritesDataSource.Select();
            foreach (var item in results)
            {
                foreach (ListItem cb in CheckBoxList1.Items)
                {
                    // Make that cb.Text instead of cb.Value
                    // what exactly is the type of item? Then you'd know
                    // what property to use to compare it to cb.Text
                    if (item.ToString() == cb.Text)
                    {
                        cb.Selected = true;
                    }
                }
            }


Footnotes:

1. If FavoritsDataSource.Select() returned a list of objects of a type you wrote yourself, chances are good the ToString() method will not return anything sensible at all but the class name of those objects.
End Footnotes

Best Regards,
Manfred
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900