Solved

Accessing asp:DropDownList.SelectedItem.Text from Client Side

Posted on 2004-10-11
9
318 Views
Last Modified: 2006-11-17
I am using ASP.NET to create a page with a dropdownlist box that is used to capture the name of a table (or view) from the user.

Given this info, I want to go to a particular web page to display records for this table.

When I try to retrieve the text that indicates the appropriate table/view entered into a asp:DropDownList control I get an "Object required" error.

I realize that I am trying to access a server control from the client and there are problems associated with to do this. This site has suggested that:

tablename= document.forms(0).getElementById('lstDropDown').SelectedItem.Text

might work from the client-side. But, this approach comes back and says "Error: Syntax".

Is there anyway to do this? I have a server-side menu control that has triggered client-side code. I know what I want to do, but don't know the table to do it to.
Any thoughts?

 

0
Comment
Question by:gbmcneil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 15

Expert Comment

by:praneetha
ID: 12278506
var selNum=document.getElementById("lstDropDown").selectedIndex;
var selCategory=document.getElementById("lstDropDown").options[selNum].text;//value..

try that
0
 

Author Comment

by:gbmcneil
ID: 12279243
Praneetha -

Your codes works fine. Thanks very much for your help. Here is the equivalent embellished VBScript to get the job done:

selNum=document.getElementById("lstDropDown").SelectedIndex
      tablename=document.getElementById("lstDropDown").Options(selNum).text
If Trim(tablename) = "Select a tablename..." Then
            MsgBox "You cannot add a new record until you have first selected a tablename.", 16, "Error Message"
      End If
      If Trim(tablename) = "Customers" Then
            window.location.href = "http://bflat77/DemoApp/AddNewCustomers.aspx"
      End If

But, can you tell me why I just can't say the following (shown below)? It seems it would be more straight forward.

tablename = document.lstDropDown.SelectedItem.Text    

It comes back and gives the error message "Error: Object doesn't support this property or method: 'document.lstDropDown' when this seemingly more obvious syntax is used.








0
 
LVL 15

Expert Comment

by:praneetha
ID: 12279271
i guess since dropdownlist does not have this properties in javascript..

.SelectedItem.Text    
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 6

Accepted Solution

by:
ryerras earned 125 total points
ID: 12279701
You can not write directly document.lstDropDown.SelectedItem because, the drop down is a server control. So, you can not specify it that way, because your web form contains the drop downlist. So, its a hierarchy, document contains Form1, then Form1 contains dropdown control. But if you use getElementByID(), it will search the entire hierarchy to find the element. So, the correct syntax to access the drop down list is

 document.Form1.lstDropDown, assuming Form1 is the id of your form, the alternative is document.forms[0].lstDropDown, here you do not have to specify the form id.

To access any item inside the drop down, you need to specify the index, since the items inside the drop down are more like array elements, so to access any type of element, you should use document.forms[0].lstDropDown.options[0].text.

By the way, if your drop down list is a server control, its always better to use getElementbyId(), sometimes the asp.net changes the id of the control after it renders it as an html control. Remember all the server controls are eventually have to be mapped to a corresponding html control to actually be used in web pages. The html control for asp.net dropdown list is "Select". If you look at the source, you can easily see that
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12279753
I need to edit one sentence in my previous post

>>You can not write directly document.lstDropDown.SelectedItem because, the drop down is a server control.So, you can not specify it that way, because your web form contains the drop downlist. So, its a hierarchy, document contains Form1, then Form1 contains dropdown control.

its the same case for type of control, html, or server controls , it doesnt matter. Controls are placed on forms. A document contains a Form. So its like

document->form->control



0
 

Author Comment

by:gbmcneil
ID: 12280670
Hello ryerras -

Gee, I'm impressed. Thanks for taking the time to explain that to me. Appreciate it.
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12281009
Where are my points then :d, Just kidding. Glad to be a help
0
 

Author Comment

by:gbmcneil
ID: 12283572
Am I supposed to be the one that awards the points?

Both you (ryerras) and praneetha were great! Maybe I should split the trophy in half.

0
 
LVL 6

Expert Comment

by:ryerras
ID: 12285875
Yeah, if you think you want to award the points to both, you should pick one as Accepted answer and other as Assisted answer. You should see them somewhere on the tabs besides the comments.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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