Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 5th, 2004, 11:36 PM
Registered User
 
Join Date: Aug 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding items to ListBox on the client.

Is there a way within a .Net "PostBack" to access ListBox items that were added on the client via Javascript?

I create a ListBox control in a c# code behind page in a standard way:


protected System.Web.UI.WebControls.ListBox myList;

.
.
.

myList.DataSource = myEntityView.EntityDataSource;
myList.DataTextField = myEntityView.Entity_DATASOURCE_TEXT_COLUMN_NAME;
myList.DataValueField = myEntityView.Entity_DATASOURCE_VALUE_COLUMN_NAME;
myList.DataBind();

.
.
.

On the client this becomes:

<select name="myList" size="4" multiple="multiple" id="myList" style="width:353px;">
    <option selected="selected" value="1022">Entity test 1022</option>
    <option value="1023">Entity test 1023</option>
    <option value="1024">Entity test 1024</option>
</select>



The following javascript adds a row to the "ListBox" on the client.


function AddTo_List(control_name, optionValue, optionText)
{

    for(index = 0 ; index < document.TestAppForm.elements.length; index++)
    {
        if (document.TestAppForm.elements[index].name == control_name)
        {

            if ( (document.TestAppForm.elements[index].options.length == 1) &&
                (document.TestAppForm.elements[index].options[0].value.length == 0))
            {
                document.TestAppForm.elements[index].options[0] = new Option(optionText, optionValue, false, false );
            }
            else
            {
                document.TestAppForm.elements[index].options[document.TestAppForm.elements[index].options.length] = new Option(optionText, optionValue, false, false );
            }
            break;
        }
    }
}

.
.
.

Two examples for adding rows are:

AddTo_List("myList", "1025", "Entity test 1025")
AddTo_List("myList", "1026", "Entity test 1026")


On the client the rows are added to the control and show up in the browser. But when the form is submitted the rows do not seem avaiable. Is this true?

Does anyone know how to access the added items in the PostBack?

Is there another ui element that allows rows to be added to a list on the client?

Thanks.
 
Old August 6th, 2004, 11:30 AM
Registered User
 
Join Date: Aug 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The following works:
string stuff = Request.Form.Get("myList");

Also, when the page is submitted I had to add some Javascript to select all the items in the "ListBox".
Does anyone know of a cleaner way to dynamically add to a list control that .Net (not just the Request) will sync up with at the server?

Thanks
 
Old August 6th, 2004, 11:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

How are you loading the data for the list, and are you only loading it once? Are you specifying "if (Page.IsPostBack) {}"?

You can add items to a list at server-side, is that not an option?

Brian
 
Old August 6th, 2004, 12:34 PM
Registered User
 
Join Date: Aug 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Brian,

To populate the list I bring up a web "dialog box" and allow the user to add items. This "dialog box" is another .Net aspx page brought up by Javascript. The "parent" page saves the parent window handle in the dialog so that the dialog can call the parent to add items to the "ListBox" via Javascript.

If there is an easy way to do this on the Server I'm all for that.

As for the ListBox control not seeing added items: it looks like ListBox's IPostBackDataHandler implementation doesn't expect that a developer would add rows. It does process selection changes. That is keeping with the control's intended use. But, I need an editable list control. I will derive a "MyListBox" class and implement IPostBackDataHandler the way I need to. If you know of a better way to do this that still allows me the dialog behavior I would be grateful to learn it.

thanks



 
Old August 7th, 2004, 12:05 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

When you fill a listbox in .NET, the items are retained in viewstate. When you postback the form, .NET handles the comparison of what's in viewstate (what is contained and what is selected) with what is selected in the posted information. The ITEMS of the listbox are NOT sent back to the server, so if you add items on the client, you will not get them as items back on the server side. Using Request.Form() will only get you what is currently selected so it won't help either. In order to use the .NET model properly, you should have your dialog modify the underlying data of the listbox. Then you need to somehow refresh the page to re-retrieve the listbox items.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting all listbox items Lucy_H85 C# 2005 2 December 1st, 2009 05:36 AM
Moving Items up and down in a listbox Brendan Bartley Access 15 September 18th, 2007 06:42 AM
UNSelect Items in ListBox mmcdonal Access VBA 7 June 21st, 2007 08:09 AM
Removing items from ListBox on the client. DevelopWorld General .NET 1 July 26th, 2006 03:33 AM
Saving Listbox Items antonducabre VB.NET 2002/2003 Basics 1 July 16th, 2003 12:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.