function listbox_getall()
{
var arruser= new Array();
var listbox = document.getElementById('<%=bxUser.ClientID%>');
for (var count = 0; count < listbox.options.length; count++)
{ var arruser[count]= listbox.options[count].text }
}
Marked as answer by ronin47 on Mar 02, 2012 11:50 AM
As I told earlier, I read these values out a asp.net-ListBox control. When I try to hide it, the engine won't send it to the browser, so Js cannot read these values, because the control doesn't even exist! I've got this:
<script type="text/javascript">
$(document).ready(function () {
// populate User Array
var arruser = new Array();
var listbox = document.getElementById('<%=lbxUser.ClientID%>');
for (var count = 0; count < listbox.options.length; count++) {
arruser[count] = listbox.options[count].text;
}
});
</script>
<asp:ListBox ID="lbxUser" runat="server" DataSourceID="User"
DataTextField="User" DataValueField="User" Visible="false"></asp:ListBox>
<asp:SqlDataSource ID="User" runat="server"
ConnectionString="<%$ ConnectionStrings:Japlas_Kaizen %>"
SelectCommand="SELECT lastName + ' ' + firstName + ', ' + FK_Department AS 'User' FROM [User] ORDER BY lastName, firstName, FK_Department">
</asp:SqlDataSource>
ronin47
Member
62 Points
73 Posts
Javascript: Getting Content of ListBox
Mar 01, 2012 12:22 PM|LINK
Hello Everybody
I have a Listbox populated by a MS SQL Data Source, which works fine.
Further I want to fill these values into a JavaScript String array.
Can anybody tell me, how I can do that?
I tried:
<asp:ListBox ID="lbxUser" runat="server" DataSourceID="User" onchange="loop()" DataTextField="User" DataValueField="User" Visible="true"></asp:ListBox> <asp:SqlDataSource ID="User" runat="server" ConnectionString="<%$ ConnectionStrings:Japlas_Kaizen %>" SelectCommand="SELECT lastName + ' ' + firstName + ', ' + FK_Department AS 'User' FROM [User] ORDER BY lastName, firstName, FK_Department"> </asp:SqlDataSource>$(document).ready(function () { var arrUser = $("#lbxUser").options }Any ideas?
Thx guys
saalameejen
Member
210 Points
50 Posts
Re: Javascript: Getting Content of ListBox
Mar 01, 2012 12:35 PM|LINK
check this link, this might help you
http://stackoverflow.com/questions/4199826/idiomatic-jquery-to-get-all-options-in-a-listbox-into-comma-separated-string
chandrasheka...
Star
10258 Points
1760 Posts
Re: Javascript: Getting Content of ListBox
Mar 01, 2012 12:42 PM|LINK
Hi,
try
$(document).ready(function () { var listObject = $("#<%=lbxUser.ClientID%>"); if(listObject.length > 0) { var arrUser = listObject[0].options; } }Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.
Danny Gokey
Member
290 Points
56 Posts
Re: Javascript: Getting Content of ListBox
Mar 02, 2012 05:33 AM|LINK
Hi,
Perhaps help to you.
function listbox_getall() { var arruser= new Array(); var listbox = document.getElementById('<%=bxUser.ClientID%>'); for (var count = 0; count < listbox.options.length; count++) { var arruser[count]= listbox.options[count].text } }ronin47
Member
62 Points
73 Posts
Re: Javascript: Getting Content of ListBox
Mar 02, 2012 11:31 AM|LINK
Hey,
Thx, I tried
$(document).ready(function () { var listObject = $("#<%=lbxUser.ClientID%>"); if (listObject.length > 0) { var arrUser = listObject[0].options; alert(arrUser.length); alert(arrUser.item(50)); } });So the length equals 692 which would be fine, but if I try to access the item 50 it throws me [object HTMLOptionElement] What am I doing wrong?
Thx!
ronin47
Member
62 Points
73 Posts
Re: Javascript: Getting Content of ListBox
Mar 02, 2012 11:49 AM|LINK
Got it! Had to access the "text"-property, like this:
Thx a lot!!
ronin47
Member
62 Points
73 Posts
Re: Javascript: Getting Content of ListBox
Mar 02, 2012 11:50 AM|LINK
Thx, your solution workes as well
function listbox_getall() { var arruser = new Array(); var listbox = document.getElementById('<%=lbxUser.ClientID%>'); for (var count = 0; count < listbox.options.length; count++) { arruser[count] = listbox.options[count].text; alert(arruser[count]); } }ronin47
Member
62 Points
73 Posts
Re: Javascript: Getting Content of ListBox
Mar 02, 2012 01:40 PM|LINK
Hello,
Sry I still have troubles concerning this thing.
As I told earlier, I read these values out a asp.net-ListBox control. When I try to hide it, the engine won't send it to the browser, so Js cannot read these values, because the control doesn't even exist! I've got this:
<script type="text/javascript"> $(document).ready(function () { // populate User Array var arruser = new Array(); var listbox = document.getElementById('<%=lbxUser.ClientID%>'); for (var count = 0; count < listbox.options.length; count++) { arruser[count] = listbox.options[count].text; } }); </script> <asp:ListBox ID="lbxUser" runat="server" DataSourceID="User" DataTextField="User" DataValueField="User" Visible="false"></asp:ListBox> <asp:SqlDataSource ID="User" runat="server" ConnectionString="<%$ ConnectionStrings:Japlas_Kaizen %>" SelectCommand="SELECT lastName + ' ' + firstName + ', ' + FK_Department AS 'User' FROM [User] ORDER BY lastName, firstName, FK_Department"> </asp:SqlDataSource>Any help would be appreciated :) Thx!
Danny Gokey
Member
290 Points
56 Posts
Re: Javascript: Getting Content of ListBox
Mar 05, 2012 01:19 AM|LINK
Hi,
If you set the listbox.visible property is false , the date values are not displayed and do not make a round-trip to the client,
in other words, you cannot use JS to acquire the content of this tag class, it is serialized into the hidden VIEWSTATE, only
the server to deserialize call to it.
Please have a try to set the style="visibility:hidden" in the css, it only the listbox control is not displayed, but the page rendding in
the HTML markup language is existent, so you can use JS to take to find the value.
more info , see this article: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datacontrolfield.visible(VS.80).aspx
Good luck
ronin47
Member
62 Points
73 Posts
Re: Javascript: Getting Content of ListBox
Mar 05, 2012 07:42 AM|LINK
Hi,
Thx this really works fine!
Had to set the height to 1px so it doesn't make my layout look bad.
Great one!