Solved

Data Repeater and CheckBoxList

Posted on 2010-11-15
15
2,082 Views
Last Modified: 2013-11-07
Hi Experts,

I have a set of Check Boxes that I am creating using a Data Repeater.
Issue is that they are not a CheckBoxList, which means they don't have properties like
.selectedValue
.SelectedItem.Text

I need to add this feature to the list so I can interact with it.

So?
How can I use a datarepeater to get the information displaying the way I have suggested in the code below, and still have a CheckBoxList.
Or is there another way to do this?

Thanks

C
<table>
        <asp:DataList runat="server" ID="rpCategory" RepeatColumns="2">
            <ItemTemplate>
                    <td>
                        <asp:CheckBox ID="ckLstCategoryid" GroupName="ckLstCategory" runat="server" Text='<%# Databinder.Eval(Container.DataItem, "Title", "{0:d}") %>' ></asp:CheckBox></br>
                        <i><%# DataBinder.Eval(Container.DataItem, "Description", "{0:d}")%></i>
                    </td>
            </ItemTemplate>
          </asp:DataList>
        </table>

Open in new window

0
Comment
Question by:Craig Lambie
  • 7
  • 5
  • 3
15 Comments
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
this is select box, not radio button list...
0
 
LVL 1

Author Comment

by:Craig Lambie
Comment Utility
What is a select box HainKurt?

If you are referring to .selected... then these properties work on ListBoxes, CheckBoxLists, RadioButtonLists etc...
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
sorry I mean check box... if you have multiple check box, they will behave independently, not like radio button...
to get what is checked, you should iterate the rpCategory items and use findcontrol to find the check box, then cast the control to checkbox and get the selected value... by the way are you sure GroupName is a valid property for CheckBox?
0
 
LVL 1

Author Comment

by:Craig Lambie
Comment Utility
This might be so, but I want to my repeater created checkboxes to be in a group (this is a valid property yes) and or be a part of the CheckBoxList control if possible?

If I use Find Control, then I will be finding it based on the Text of the CheckBox Label, this is not exactly the best way to do this, especially when I want to operate using the "itemID" from the Db to reference the CheckBoxes...

ie.
.selectedValue = itemID
.selected.text = Title
0
 
LVL 7

Expert Comment

by:mr_nadger
Comment Utility
have you tried populating a checkbox list inside the repeater template programatically on the databound event?
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
still i dont see how a groupname works for checkboxes... is there any documentation for this attribute? what component are you using? you should use radio buttons not check boxes...

maybe you can use regular checkboxes like

<input type=checkbox name="chk_<%#eval("id")%>" value="1"> <%# Databinder.Eval(Container.DataItem, "Title", "{0:d}") %>' >

then use request("chk_" & someid) to find if it is checked or not...
0
 
LVL 7

Expert Comment

by:mr_nadger
Comment Utility
CClambie, could you describe the data being displayed in the repeater and what the checkboxes are supposed to represent? (e.g. are they different columns in the data row being displayed?)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:Craig Lambie
Comment Utility
HainKurt, this would be a classic ASP way of doing things, which I am not opposed to, but I have reservations

If I use radio buttons, what is the difference? The Groupname property will work maybe, but how is that going to get me the Selected.Text/ .value

mr_nadger
The information is a simple list of options from a Db.
ie.
ID, Title, Description
1, Test, Test desc
2, Test2, Test2 Desc
3, Test3, Test3 Desc

To see which items are selected for a particular user, I need to lookup the IDs in another table and mark these items as selected.

When the user checks a box this other table is updated with the differences (ie. add/ remove IDs)
0
 
LVL 1

Author Comment

by:Craig Lambie
Comment Utility
another thought, the reason for not using radio buttons is that you can select more than one option, hence moving towards a CheckBox, also it seems a radio button won't allow you to un-select it

0
 
LVL 7

Expert Comment

by:mr_nadger
Comment Utility
are you trying to put checkboxes in each column,or just the ID?

I don't understand why you're using a repeater here, if you're just trying to select one or more items from a list based on a database table, why not use a gridview with a checkbox in a template field and the ID number as a datakey? (List size would have an impact here if you have to use pagination).
If you have a button for the user to click once all the required items are selected, just iterate through the rows in the grid, find the checkbox and if it's checked, add the datakey to an array, and you have your list
0
 
LVL 1

Author Comment

by:Craig Lambie
Comment Utility
It is more about the way I want it to look at the end of the day.

As you can see here, I have a bunch of radios with the Title and Description fields showing up in 2 columns.  I want this to be how they display, not in a Gridview
  CheckBox Eg
Unless you are suggesting I can apply this type of template to a gridview, in which case that is def the way to go, is so, how?
0
 
LVL 7

Expert Comment

by:mr_nadger
Comment Utility
As you want to have multiple columns, gridview and datarepeater are the wrong controls to use, stick with the DataList as that's designed for this kind of behaviour.

Regardless of which control you use for selecing an item, radiobutton or checkbox, you cannot group them across different rows in the dataset populating the DataList (or gridview/datarepeater). I think you're trying to use a property of the HTML radiobutton appropriate for simple static lists on a dynamically populating object where each row is a discrete item.

If you want a table updated in the background whenever someone selects/deselects an item, add some code behind on the CheckedChanged event to identify the item selected and fire off the relevant SQL, e.g. a stored proc with row index and checked value.
Alternatively, if you're aiming for a bulk update of all selected items, have a button which fires code to iterate through each row.

0
 
LVL 1

Author Comment

by:Craig Lambie
Comment Utility
So, the answer is.
No, you cannot apply add more information into a CheckBoxList control, nor can you use a template on a GridView or other to get a DataList in a specific format that suits my needs.

To be honest I am a little disappointed with ASP.Net 2.0 for not being able to do that, seems like the type of control that would be built in.
0
 
LVL 7

Accepted Solution

by:
mr_nadger earned 500 total points
Comment Utility
You're trying to use the controls in ways they're not designed for; DataList will give you the two column format but you cannot group radiobuttons or checkboxes across different lines of a dynamically built data displaying object.

A static group of HTML radio buttons grouped during the page design is very different to the controls you're trying force their behaviour onto, and it's not a fault in ASP.Net that objects built during the pageload behave differently to objects of a different type hard coded into the HTML.

If you really want a single control to do this kind of thing, create a user control displaying things in whatever format you want, with properties for items and selecteditems defined as arrays, and the code to update the user table you've mentioned.
(You will still have to code the collection of items and selecteditems, but when you drag your control onto the main page it will act how you think the ASP dataobjects should)
0
 
LVL 1

Author Closing Comment

by:Craig Lambie
Comment Utility
That is the point I was making, asp should have one built into it.... Seems reasonable..
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now