Solved

Data Repeater and CheckBoxList

Posted on 2010-11-15
15
2,101 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
ID: 34142016
this is select box, not radio button list...
0
 
LVL 1

Author Comment

by:Craig Lambie
ID: 34142179
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
ID: 34142278
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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 1

Author Comment

by:Craig Lambie
ID: 34142541
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
ID: 34144221
have you tried populating a checkbox list inside the repeater template programatically on the databound event?
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34147083
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
ID: 34153576
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
 
LVL 1

Author Comment

by:Craig Lambie
ID: 34161451
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
ID: 34161460
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
ID: 34162272
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
ID: 34169954
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
ID: 34171575
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
ID: 34171863
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
ID: 34171978
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
ID: 34172459
That is the point I was making, asp should have one built into it.... Seems reasonable..
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 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