troubleshooting Question

Reading htmlSelect items from the C# code behind

Avatar of skaleem1
skaleem1Flag for Canada asked on
.NET ProgrammingASP.NETMicrosoft Development
6 Comments1 Solution1504 ViewsLast Modified:
I am trying to implement the client side listbox using javascript. I am trying to fill two htmlSelect listboxes and am able to fill the first listbox successfully by feeding the data from a dataset. Here is the code behind:
 
string
js = "";
foreach (DataRow row in dsDropDown.Tables[COUNTRIES].Rows)
{
js = js +
"AddItem('" + lstCountries.ClientID + "', '" + row["CountryNM"].ToString().Replace("'", "") + "', '" + row["CountryID"].ToString() + "'); ";
}
Page.ClientScript.RegisterStartupScript(
this.GetType(), "AddItem", js, true);

and here is the javascript function:
 
function
AddItem(selectbox, text, value )
{
var optn = document.createElement("option");
optn.text = text;
optn.value = value;
if(selectbox='lstCountries')
var elSel = document.getElementById('lstCountries');
else if(selectbox='lstCountriesofOrigin')
var elSel = document.getElementById('lstCountriesofOrigin');
elSel.add(optn);
}
and here are the aspx htmlSelect object defined:
 
<p><select id="lstCountries" size="10" runat="server" style="WIDTH: 176px; HEIGHT: 128px">
</select></p>
<p><select id="lstCountriesofOrigin" size="10" runat="server" style="WIDTH: 176px; HEIGHT: 128px">
</select></p>

The first htmlSelect listbox named lstCountries is populated successfully. When I tried to populate the second htmlSelect  listbox lstCountriesofOrigin using the same javascript function called from the following code behind loop, I am having problems reading the items from the lstCountries htmlSelect listbox. The items I added to this htmlSelect liatbox using the javascript function in the above code are no longer there and the lstCountries.Items.Count is zero. Here is the code behind I am trying to use:
foreach
(DataRow row in dsSearchResults.Tables[COO].Rows)
{
foreach (DataColumn column in dsSearchResults.Tables[COO].Columns)
{
if (column.ColumnName.ToString() == "CountryNM")
{
for (int i = 0; i < lstCountries.Items.Count; i++)
{
if (lstCountries.Items[i].Text == row["CountryNm"].ToString())
{
js = js +
"AddItem('" + lstCountriesofOrigin.ClientID + "', '" + row["CountryNM"].ToString().Replace("'", "") + "', '" + row["CountryID"].ToString() + "'); ";
break;
}
}
}
}
}
Page.ClientScript.RegisterStartupScript(
this.GetType(), "AddItem", js, true);
Any ideas why I am not getting the already added items in the lstCountries listbox? Why are they lost and what is the way to get the added items info in the code behind?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros