This is a migrated thread and some comments may be shown as answers.

how do I get the items from a list box on post back

3 Answers 339 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Roger
Top achievements
Rank 1
Roger asked on 05 Feb 2010, 04:27 PM
I am creating RadListBoxes dynamically at runtime. Some pages may have one, some pages may have 5.

My page has a submit button on it. So, when the page is submitted I am looping through all the fields and getting their values to build some xml which I then send to the database. [I am using Request.Form.AllKeys[x] and Request.Form[x] to get the key and the value from each form field. For the RadListBox controls, I want to get ALL the items that they contain]

How do I get the values (RadListBoxIItems) that are in each list box?

I have not been able to find an example of this sort of "manual" retrieval of the items in a list box.

Thanks for whatever advice you can offer.

UPDATE:
It looks as if this post might offer a way to get the values. I appears I can deserialize the client state of the RadListBox using an RadListBoxClientState object. However, I can't find where that is defined. The thread says that I just need to import Telerik.Web to use it but, that doesn't appear to work. Is this object still available. I will continue searching.

3 Answers, 1 is accepted

Sort by
0
Schlurk
Top achievements
Rank 2
answered on 05 Feb 2010, 05:31 PM
In order to get the values of all of your RadListBox's items you can simply use a foreach loop:
        foreach (RadListBoxItem currentItem in RadListBox1.Items) 
        { 
            String value = currentItem.Value;
            ...
        } 

If you are looking to have all of your RadListBoxes go through this you could always maintain a list of RadListBoxes that you can then cycle through as well.
0
Roger
Top achievements
Rank 1
answered on 05 Feb 2010, 05:53 PM
Thank you for your response.

I know how to get the items from the listboxes, I just can't figure out how to get to the list boxes.

I think I should do a better job of explaining what I am trying to accomplish.

I am dynamically creating a form that can have a variable number of RadListBoxes on it. The listboxes could have values in them, or they could be empty when the form is displayed, depending on if the user is editing an existing record, or creating a new one. I want the user to be able to add items to the listboxes, or delete items or re-order items, along with editing all the other (simpler) fields in the form. When all their editing is done, they should then be able to click submit to commit their changes [I don't want any changes to happen before they hit submit]

I had intended to loop through all the fields in the form and get their values out, putting it all in XML and sending it to the database (SQL Server can handle xml arguments to stored procedures). Once in the stored procedure, I would parse the XML and update the database accordingly. 

All the simple field values are easy to get, using a combination of Request.Form.AllKeys and Request.Form. But, I can't figure out a way to handle the RadListBoxes. When I am looping through Request.Form.AllKeys, and I come across a RadListBox, I get the field's title (something like "ctl00_ContentPlaceHolder1_ctl00_rlb_98_7_ClientState"). But, I can't find a way to discover what items were listed in the RadListBox when the user hits "submit". [The field title listed above is a combination of the stuff ASP.NET pre-pends to the title of all dynamically added controls "ctl00_ContentPlaceHolder1_ctl00_" and the title I created which is "rlb_" to let me know it is a RadListBox, the property number "98", the type of data it is "7" and the "_ClientState" but which I assume comes from RadControls]

Maybe I should do some hack, like creating a javascript function that fires when the submit button is clicked that will find all the RadListBox controls and set a hidden page variable to contain a string listing the contents, or something like that.

I looked at using logging to solve this but, I am not sure how logging would handle having items added to the list.



 
0
Accepted
Genady Sergeev
Telerik team
answered on 11 Feb 2010, 09:17 AM
Hi Roger,

You can't access a given RadListBox from Request.Form.AllKeys since it is not a native HTML element. The correct thing to do is as follows:

RadListBox myListBox = (RadListBox)Page.Form.FindControl("myContentPanelID").FindControl("myListBoxID");

Then you can use the properties of the found listbox  and work with them in an object oriented fashion.

The ClientStateField is used internally by a given RadListBox in order to preserve its client changes. You should not use it.

All the best,
Genady Sergeev
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
Tags
ListBox
Asked by
Roger
Top achievements
Rank 1
Answers by
Schlurk
Top achievements
Rank 2
Roger
Top achievements
Rank 1
Genady Sergeev
Telerik team
Share this question
or