?
Solved

Javascript populated ASP.NET ListBox has no "SelectedValue" on postback

Posted on 2008-11-14
2
Medium Priority
?
1,352 Views
Last Modified: 2012-05-05
Have two ASP.NET listboxes on my page.  One is populated via an SQLDataSource.  The other is populated via a javascript array.  When the user selects an item in the first box I have an event handler populate the second listbox with dependent data.

See my attached javascript function for the function that populates the second listbox...

The population happens as designed.  I am able to select a value in the first listbox and have the second is populated appropriately.  But the problem is in the postback.   I select a value in the second listbox and click OK...the postback happens and I make it to my event handler in the codebehind.  I can capture values for any other control on the page but when I try to access the second listbox the "SelectedValue" is an empty string.  Does this have to do with the fact that the second lstbox is populated in the browser and not from the codebehind or page load?


function LoadRightsListbox () 
{
   var lbxRights = $get(contentPlaceHolder + "lbxRights");
    var lbxRightCategories = $get(contentPlaceHolder + "lbxRightCategories");  
    var selectedRightCategoryID = lbxRightCategories.options[lbxRightCategories.selectedIndex].value;
    //clear list box 
    lbxRights.length=0;
    
    //populate the list box 
    var lbxIndex = 0; 
    for(var listIndex in rightsList) //alternative indexes (lbxIndex, listIndex) because the data IDs are not sequential
    { 
        if (rightsList[listIndex].getCategoryID() == selectedRightCategoryID)
        {
            lbxRights.length = rightsList.length + 1;
            lbxRights.options[lbxIndex].text = rightsList[listIndex].getName();
            lbxRights.options[lbxIndex].value = rightsList[listIndex].getID();
            lbxIndex++; 
        }         
    }       
}

Open in new window

0
Comment
Question by:Bruce
2 Comments
 
LVL 17

Accepted Solution

by:
Daniel Reynolds earned 2000 total points
ID: 22963465
Does this have to do with the fact that the second lstbox is populated in the browser and not from the codebehind or page load?
Yes.
I have done this using a hidden field that I add the items to in javascript and then I check the hidden field in the code-behind for values and add them to the drop down or list box.
0
 
LVL 1

Author Closing Comment

by:Bruce
ID: 31516934
Thanks xDJR1875!

I did just what you suggested and it works.  

Thanks for the quick response.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question