getting the value of a selected/unselected item in a checkboxlist

  • Thread starter Brett Wesoloski
  • Start date
B

Brett Wesoloski

I am having a bit of a problem getting the current value of the checkbox
list.

I have tried using the selected value as I thought that would give me the
current value of the check box but it appears to be giving me the first
value that is selected all the time.

I have also tried to use the selectedItem property but that does the same as
I would expect becuase the property says it get the item with the lowest
value.

How can I get the current value of a checkboxlist?

TIA,
Brett
 
P

Peter Duniho

I am having a bit of a problem getting the current value of the checkbox
list.

Can you define "current value"? If multiple items in the list can be
selected (checked, in this case), what is the "current value", given that
there are multiple items with values that have been selected?
I have tried using the selected value as I thought that would give me the
current value of the check box but it appears to be giving me the first
value that is selected all the time.

Yup. The term "current value" isn't defined yet, but I am not surprised
that you get the first selected item in the list when you get the
SelectedValue.
I have also tried to use the selectedItem property but that does the
same as
I would expect becuase the property says it get the item with the lowest
value.

Actually, it says "lowest index". Note that SelectedValue and
SelectedIndex work together. In particular, all that SelectedValue does
is return the Value property of the ListItem that is at SelectedIndex.
How can I get the current value of a checkboxlist?

It's not possible to answer that question until you explain what you mean
by "current value".

Pete
 
B

Brett Wesoloski

What I mean by current value is the check box you just checked.

For instance I have these check boxes.

a
b
c
d
e
f
g


Now I select the b check box. The current value is b. Then I go and select
the f check box. The current value is f as that was the next check box to
be selected.

Does that help?

TIA,
Brett
 
P

Peter Duniho

What I mean by current value is the check box you just checked.

Okay. I'll suggest that's not a broadly used concept of "current value".
:)
[...]
Now I select the b check box. The current value is b. Then I go and
select
the f check box. The current value is f as that was the next check box
to
be selected.

Does that help?

Sure. What you actually want is the checkbox that actually has *focus*.
That's entirely different from the one(s) that is(are) selected.

Not having done the ASP/web .NET stuff, I'm not sure how you can actually
get that. My first thought would be that you could enumerate the ListItem
instances in the CheckboxList and check to see which one (if any) has the
focus. But I don't see any immediate way to do that in the ListItem class.

If it's possible, it probably has something to do with the larger browser
container somehow, since in the context of a web page, focus can actually
leave the web page, and so I would not expect it to necessarily be tracked
by the web page itself. But I admit, never had to do any of the web UI
stuff, so I don't know for sure one way or the other.

As a last resort, it may be that you can track when any of the items in
the CheckboxList change, and by doing so, keep track of the one that most
recently changed. Then consider that the one with "focus" any time you
care about that.

Hopefully knowing that you are actually asking about "focus" rather than
"selection", that may help you in your search through the documentation.

Pete
 
E

Eric

Brett, did you find a solution? I'm also attempting the same thing. I have a CheckBoxList and I want to know which box was selected/unselected on SelectedIndexChanged. I'm considering rewriting the checkboxes to use a Repeater and have individual checkboxes rather than a CheckBoxList. If I did that, I'd know exactly which one had changed. Thoughts? Thanks! -Eric

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top