Showing posts with label item. Show all posts
Showing posts with label item. Show all posts

Saturday, March 31, 2012

Strang behaviour of multiselection Listbox

Hello,

i´d like to get access to the selected items of my listbox...

I did this in a loop, but I get only acces to the first selected item of the Listbox.

Has anyone solved the same problem? I really need help. Thx in advance.

Code (VB.net):

dim el as listitemfor each el in mylistbox.Items if el.selected then 'do something end ifnext

Give this a try...

 iCnt = 0 Dim li As ListItem For Each li In myListBox.Items If li.Selected Then iCnt += 1 Next If iCnt <> 0 Then Dim i As Integer iCnt = myListBox.Items.Count For i = 0 To iCnt - 1 If myListBox.Items(i).Selected Then 'Do Something End If Next
 End If
 Zath

Still the same problemSad
I´m sad...

But thank you for the try...

That shouldn't be.

Do you have this line in there?

If myListBox.Items(i).Selected Then

Zath


Yes...

No matter what I do, the first selected item returns true for .selected

the next ones return false...


ListBoxes (and CheckBoxLists) expose a new property called "GetSelectedIndices()"

So you can say

Dim LI As ListItem
For Each idx As Int32 in mylistbox.GetSelectedIndices()
LI = mylistbox.Items(idx)
'Do what you need to do with this selected list item
Next

*If* that doesn't work for you, and there is absolutely no reason why this code or the code you posted shouldn't... then you have other issues, like for instance, re-binding the list on every page load (which would "reset" the listboxbefore your event handler got a hold of the control)


I´m working on 1.1 so that the above function is not available for me...


But I will check the second issue. Thanks for the hint...


That did the trick... Sorry for the wasting of your time...

Did a wrong binding on PostBacks so I killed all of the selections except of one in the listbox...

*smashing head against the next wall*


i can't speak for the others replying on this topic... but you didn't/aren't wasting my time... glad to help out...

the whole binding cylce is a huge "gotcha" in .NET, we've all had our battles with it


(this is where you tell them "but it sure is great thatEasyListBox doesn't have that problem" Big Smile )

Monday, March 26, 2012

Strange Datagrid Update Problem

Hi,

I'm having a strange problem with the update function of a datagrid:

I have a datagrid, the first column lists item names from a database.
Second column is an EditCommandColumn, and then I have a couple of
button columns after that to deal with row deletes etc.

The problem is occuring in the method that I have specified should
deal with the update event:

protected void nameEditUpdate(object sender, DataGridCommandEventArgs
e)

Because the first column I know contains the item that is getting
updated, I try:

tmpTextBox = (TextBox)e.Item.Cells[0].Controls[0];

To get the control, before looking at its Text property. However,
when I try this I get:

System.InvalidCastException: Specified cast is not valid.

So to try and figure out what is going on, I have tried looking at the
type being returned by e.Item.Cells[0].Controls[0], which turns out to
be type System.Web.UI.LiteralControl, the name and text properties of
it are blank. This has just confused me even more.

Anyone have any idea just what might be going on?

Appreciated!Use FindControl instead of hard coding the position in the Controls collection.
Otherwise iterate over the Controls collection until you find your TextBox
(I bet it's at position Controls[1]). What's happening is that there's whitespace
in your template after the <ItemTemplate> and before your <asp:TextBox> and
that's the LiteralControl you're seeing.

-Brock
DevelopMentor
http://staff.develop.com/ballen

> Hi,
> I'm having a strange problem with the update function of a datagrid:
> I have a datagrid, the first column lists item names from a database.
> Second column is an EditCommandColumn, and then I have a couple of
> button columns after that to deal with row deletes etc.
> The problem is occuring in the method that I have specified should
> deal with the update event:
> protected void nameEditUpdate(object sender, DataGridCommandEventArgs
> e)
> Because the first column I know contains the item that is getting
> updated, I try:
> tmpTextBox = (TextBox)e.Item.Cells[0].Controls[0];
> To get the control, before looking at its Text property. However,
> when I try this I get:
> System.InvalidCastException: Specified cast is not valid.
> So to try and figure out what is going on, I have tried looking at the
> type being returned by e.Item.Cells[0].Controls[0], which turns out to
> be type System.Web.UI.LiteralControl, the name and text properties of
> it are blank. This has just confused me even more.
> Anyone have any idea just what might be going on?
> Appreciated!
Thanks Brock. I would never have guessed that whitespace would be a
member of the controls collection!

On Sat, 11 Jun 2005 07:06:34 -0700, Brock Allen
<ballen@.NOSPAMdevelop.com> wrote:

>Use FindControl instead of hard coding the position in the Controls collection.
>Otherwise iterate over the Controls collection until you find your TextBox
>(I bet it's at position Controls[1]). What's happening is that there's whitespace
>in your template after the <ItemTemplate> and before your <asp:TextBox> and
>that's the LiteralControl you're seeing.
>-Brock
>DevelopMentor
>http://staff.develop.com/ballen