473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

listcontrol, checkboxlist, radiobuttonlist, placeholder

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.
Nov 18 '05 #1
3 1705
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #2
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.
Nov 18 '05 #3
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.
Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: chak | last post by:
I have developed a form which dynamically builds textbox, radiobuttonlist, checkboxlist controls from parameters in a table, and somehow manages to maintain state (of data keyed in by user)....
1
by: Tom | last post by:
Hi, I have a webform which has DropDownList, RadioButtonList and CheckBoxList. All values are in webform, but not select from DB table as they do not need to be used as search requirements. ...
4
by: juststarter | last post by:
Hello, I have an aspx file where i've put a placeholder element. On load (page_load) i create dynamically an html table which contains a checkbox and a radiobuttonlist in each tablerow . The...
4
by: Marty | last post by:
I am using: VS2005 Pro WinXP Pro SP2 IIS 5.1 ..NET Framework 2.0.50727 I create a website using File>New>Web Site>ASP.NET. I put a ListBox, DropDownList, CheckBoxList and RadioButtonList on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.