Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,
I have a html listbox which i have populated by xml parsing. But when i am trying to get the selected items of this html listbox, i am unable to get any itmes in it, i always get its count as null.
I tried this

C#
Control drpSkills_ctrl = (ListBox)FindControl("ctl00$ContentPlaceHolder1$drpSkills");


With the above code i am able to get the listbox in rpSkills_ctrl but i dont get .items.count property.

Can anyone help how can i get the listbox and do foreach or for loop on its items
Posted
Comments
Suresh Suthar 22-Sep-11 7:48am    
Is it server control? Means runat="server".

Can't you access the control directly? Even if it's a server side Html control like this:

ASP.NET
<select runat="server" id="drpSkills">
//options
</select>


You should still be able to access it directly like this:

C#
Control drpSkills_ctrl = drpSkills;


or if the problem is that you've found the control, but you can't access it's items collection, you'll need to cast it to the correct type which should be HtmlSelect:

http://msdn.microsoft.com/en-us/library/807bc327.aspx[^]
 
Share this answer
 
Comments
[no name] 22-Sep-11 8:43am    
Thanks for the reply sir, i did this

HtmlSelect hs = new HtmlSelect();
hs.DataSource = drpSkills.Items;
hs.databind();

but still am getting items count = 0
jim lahey 22-Sep-11 8:49am    
Well of course that's not going to work. Try this:

HtmlSelect drpSkills_ctrl = (HtmlSelect)FindControl("ctl00$ContentPlaceHolder1$drpSkills");
[no name] 22-Sep-11 9:11am    
Thanks but still not working for me :(
i did

HtmlSelect drpSkills_ctrl = (HtmlSelect)FindControl("ctl00$ContentPlaceHolder1$drpSkills");
for (int j = 0; j <= drpSkills_ctrl.Items.Count; ++j)
{
if (drpSkills_ctrl.Items[j].Selected)
{
arr.Add(drpSkills_ctrl.Items[j].Value);
}
}

I gets count as 0 only.
jim lahey 22-Sep-11 9:16am    
post the code where you populate the HtmlSelect
[no name] 23-Sep-11 1:09am    
My code is something like this, first i fetch data from xml, put it in array and then call another function to remove duplicate values and then append all the unique values in my dropdown.
<pre lang="c#">
<script type="text/javascript" language="javascript">
$(document).ready(function()
{
$.get('/files/Test.xml', function(d){
});
$.ajax({
type: "GET",
url: "/files/Test.xml",
dataType: "xml",
success: parseXml
});
</script>
<script type="text/javascript" language="javascript">


function parseXml(xml)
{
document.getElementById('ctl00_ContentPlaceHolder1_rdoSubskill').checked = true;

var arrSpecialization = new Array();
var arrSkills = new Array();
var arrSubSkills = new Array();
//For getting all specialization in an array
$(xml).find("special").each(function()
{
var test = $(this).attr("spnm") + ' , '+ $(this).attr("spid");
arrSpecialization.push(test);
});
FilterSpecialization(arrSpecialization);
}

function FilterSpecialization(arr)
{
var i;
var len = inputArray.length;
var outputArray = [];
var temp = {};
for (i = 0; i < len; i++)
{
temp[inputArray[i]] = 0;
}

for (i in temp)
{
outputArray.push(i);
}
$('#ctl00_ContentPlaceHolder1_drpSpecialization >option').remove();
$('#ctl00_ContentPlaceHolder1_drpSpecialization').append($('<option></option>').val('0').html('--All Specialization--'));
$.each(outputArray, function(val, text) {
$('#ctl00_ContentPlaceHolder1_drpSpecialization').append( $('<option></option>').val(text.split(",")[1]).html(text.split(",")[0]))
});
}
</script>
</pre>

Here is my HTML select code
<pre lang="HTML">
<select id="drpSkills" önclick="FilterSubSkills(this)" size="12" multiple="true" runat="server" style="width: 220px">
</pre>

This function FilterSubSkills() is used to get all the selected values from my listbox and on the basis of these values i again populate another listbox with the respective values.

Now in my code behind i am getting drpskills but its item count is 0.
In addition to jim answer, use ClientID property of the control. i.e
C#
HtmlSelect drpSkills_ctrl = (HtmlSelect)FindControl(drpSkills.ClientID)
In addition to this ,the way the data is bound to ListBox or HtmlSelect is very important. i.e Are you binding to the data to a Page Load by checking a PostBack. If you didn't check a PostBack, then you won't be able to get Selected item from the list. Here is a good MSDN example. HtmlSelect Server Control Declarative Syntax[^]
 
Share this answer
 
Comments
[no name] 23-Sep-11 1:27am    
dint worked for me, i have seen that link before, its showing how to populate from listboxes which have its options hardcoded, my listbox is populated dynamically.
Wonde Tadesse 23-Sep-11 9:52am    
Can you post the full code that binds the listbox?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900