listcontrol, checkboxlist, radiobuttonlist, placeholder

E

Elad Volpin

Can someone please explain this:

I created a new webform with a PlaceHolder and a Button.

My Page_Load contains the following code:

private void Page_Load(object sender, System.EventArgs e)
{
ListControl l = null;
ListItem item = new ListItem("a", "b");
item.Selected = true; // <--- causes exception
if ( !IsPostBack )
l = new RadioButtonList();
else
l = new CheckBoxList();

l.Items.Add(item);
this.PlaceHolder1.Controls.Add(l);
}

The RadioButtonList appears just fine at first. If I click on the
button, then instead of seeing a CheckBoxList, I get the following
exception:

'Length cannot be less than zero. Parameter name: length'.

If I comment out the 'item.Selected = true;' from the code, then the
behaviour is as follows:

1. If I don't select the radio button (i.e. submits a 'cleared'
button) and click the regular button, then I get to see the
CheckBoxList just fine.

2. If I do select the radio button, then I get the same exception
(just as if the 'item.Selected = true;' was not commented out).

I know I can workaround this problem - thats easy. But can someone
explain this behaviour?

Thanks,
Elad.
 
N

Natty Gur

Elad Hi,

It's all about viewstate...

If client request send data to server the viewstate mechanism works and
call control LoadPostData method. radiobuttonlist implementation of
LoadPostData contain check of the control uniqueID length to get the
index of cached select item.

Now the problem is that you didn't give different ID's to radio and
check list, there for the UniqueID is empty. if you will assign ID for
each one of those lists you wont get this error :

ListControl l = null;
ListItem item = new ListItem("a", "b");
item.Selected = true; // <--- causes exception
if ( !IsPostBack )
{
l = new RadioButtonList();
l.ID = "a";
}
else
{
l = new CheckBoxList();
l.ID = "b";
}
l.Items.Add(item);
this.PlaceHolder1.Controls.Add(l);


Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)52-8888377
 
E

Elad Volpin

Hi Natty - thanks for your reply.

I've added a Response.Write in my code that display the UniqueId and
ID and indeed I understand now why the 'Length....' exception occurs,
although I think that MS should have forseen this problem and either
generate the ID if it was not provided (like they do for UniqueID), or
at least throw a clearer exception and explanation.

However, is it possible that you meant that the LoadPostData of the
CheckListBox is the one that's checking the ID, which causes the
error?
When I use several pages with RadioButtonLists only, the exception
does not occur.

Elad.
 
E

Elad Volpin

Hi Natty - thanks for your reply.

I've added a Response.Write in my code that display the UniqueId and
ID and indeed I understand now why the 'Length....' exception occurs,
although I think that MS should have forseen this problem and either
generate the ID if it was not provided (like they do for UniqueID), or
at least throw a clearer exception and explanation.

However, is it possible that you meant that the LoadPostData of the
CheckListBox is the one that's checking the ID, which causes the
error?
When I use several pages with RadioButtonLists only, the exception
does not occur.

Elad.
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top