ListBox inside a WebControl cannot get postback value.

F

Felix Chan

Hi all,

I have a problem in getting the selected item of a listbox
inside a custom WebControl.

I have a class 'SelectionBox', a abstract
class 'WebElement' and a interface 'IWebElement'.

The SelectionBox implment the WebElement and the
WebElement extend the WebControl class and implement
the'IWebElement', as follow:

public class SelectionBox : WebElement
{
private ListBox listBox = new ListBox();

//the body of this class were skipped
}

public abstract class WebElement : WebControl,
INamingContainer, IWebElement
{
protected int deviceID = 0;
protected int deviceType = 0;

//getter and setter of deviceID and deviceType skipped.
}


public interface IWebElement
{
int deviceID
{
get;
set;
}

int deviceType
{
get;
set;
}
}

I add the SelectionBox to a ASPX page. When postback, the
selectedindex and selecteditem of the listbox inside the
SelectionBox are always -1 and null.

I have installed the SelectIndexChanged event handler for
that listbox (inside the SelectionBox) but the value
logged to log file showing that it is -1 and null.

Anyone know why ?
Any idea of what area I should look into ?

Regards
Felix
 
F

Felix Chan

Of course, I have add the listbox to the controls
collection of the SelectionBox class tin the
CreateChildControls() method, as follow :

public class SelectionBox : WebElement
{
private ListBox listBox = new ListBox();

protected override void CreateChildControls()
{
log.Debug("START CreateChildControls()");
this.listBox.SelectedIndexChanged += new
System.EventHandler(this.OnSelectedIndexChanged);
this.Controls.Add(this.listBox);
base.CreateChildControls();
log.Debug("END");
}

//the body of this class were skipped
}
 
R

rcarboni

I don't know if this will help you, but I was having a
similar problem where SelectedIndex would always return -1
from a ListBox control even though the ListItems were
added successfully and were quite visible with a valid
selection.

What I discovered was that the string entries were somehow
too long and would display correctly, but could not be
selected. My code looked something like this:

// started out as a byte array
byte[] ListEntry = new byte[1024];

.... ListEntry filled here with a string from a extern
component

// converted to a char array
char[] charBytes = new char[ListEntry.Length];
for (i = 0; i < ListEntry.Length; i++)
charBytes = Convert.ToChar(ListEntry.GetValue(i));

// convert to string to use in ListBox
string ListString = new string(charBytes);

// string added to ListBox
ListBox1.Items.Add(ListString);

I had to shorten the string by determining where the first
0 byte occured in the char array and then it worked fine.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top