gbmcneil
asked on
Accessing asp:DropDownList.SelectedItem.Text from Client Side
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).getEleme ntById('ls tDropDown' ).Selected Item.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?
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).getEleme
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?
ASKER
Praneetha -
Your codes works fine. Thanks very much for your help. Here is the equivalent embellished VBScript to get the job done:
selNum=document.getElement ById("lstD ropDown"). SelectedIn dex
tablename=document.getElem entById("l stDropDown ").Options (selNum).t ext
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.Selec tedItem.Te xt
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.
Your codes works fine. Thanks very much for your help. Here is the equivalent embellished VBScript to get the job done:
selNum=document.getElement
tablename=document.getElem
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.Selec
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.
i guess since dropdownlist does not have this properties in javascript..
.SelectedItem.Text
.SelectedItem.Text
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I need to edit one sentence in my previous post
>>You can not write directly document.lstDropDown.Selec tedItem 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
>>You can not write directly document.lstDropDown.Selec
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
ASKER
Hello ryerras -
Gee, I'm impressed. Thanks for taking the time to explain that to me. Appreciate it.
Gee, I'm impressed. Thanks for taking the time to explain that to me. Appreciate it.
Where are my points then :d, Just kidding. Glad to be a help
ASKER
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.
Both you (ryerras) and praneetha were great! Maybe I should split the trophy in half.
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.
var selCategory=document.getEl
try that