Click to See Complete Forum and Search --> : How do I get ASP.NET to recognize javascript dynamically added items in a ListBox?


oomagnumoo
08-02-2007, 05:44 PM
I created a listboxcontrol on my asp page:


<asp:ListBox ID="ListBoxAnd" runat="server" Width="300px"></asp:ListBox>


As you can see above, there is no items associated with this List-Box. I then wrote a simple JavaScript function to add items into the ListBox dynamically


function AddAnd(pVal)
{
// Create an Option object
var opt = document.createElement("option");

// Assign text and value to Option object
opt.text = pVal;
opt.value = pVal;

// Add an Option object to Drop Down/List Box
document.getElementById("ctl00_Content_TabContainer1_TabPanel2_ListBoxAnd").options.add(opt);
}


The function does in fact properly add items to the LisBox. (FYI: The elementID is generated by ASP.NET hence the long ID name ctl00_Content_TabContainer1_TabPanel2_ListBoxAnd If anybody knows a workaround for this let me know).

The problem is: when I do a simple ListBoxAnd.Items.Count on the server-side, the Item count is always 0. The same applies for any other method or property of the ListBoxAnd. ASP.NET does NOT recognize the dynamically added items in the List Box.

How do I get ASP.NET to recognize javascript dynamically added items in a ListBox?

MykeXero
08-06-2007, 01:28 PM
Submit that data with the usual <form and submit button stuff, and it should be picked up fine.

or do some sorta ajax setup.....

oomagnumoo
08-06-2007, 01:41 PM
I did find a solution for this. MykeXero, you are correct, but your method would require handling HttpResponses, I wish I figured it out not having to use http responses but I don't know how otherwise.

if (Request.Form["ctl00$Content$TabContainer1$TabPanel2$ListBoxAnd"] != null)
{
string[] sAndConditions;
sAndConditions = Request.Form["ctl00$Content$TabContainer1$TabPanel2$ListBoxAnd"].Split(',');

Where by Request.Form picks up the Http response. I need to do a null check to ensure that a request was sent.

Just a warning, since ASP.NET view-state gets thrown off as a result of dynamically adding items to a list-box.... I had to set EnableEventValidation="false" else the server complains. This removes validation for the entire page.

It is not necessary to disable validation from the entire page, you can overload the Render operator to simply remove the validation from the listboxes alone. I found an article on this on the asp.net/forums

Good luck.

misliplavo
08-08-2007, 09:54 AM
The function does in fact properly add items to the LisBox. (FYI: The elementID is generated by ASP.NET hence the long ID name ctl00_Content_TabContainer1_TabPanel2_ListBoxAnd If anybody knows a workaround for this let me know).
you can for example in your Load method in cs. put something like:
Button.Attributes.Add("onclick", "SomeJavaMethod( '" + AspControl.ClientID + "');return false;");
then in javascript your method will have parametar SomeJavaMethod(aspControl)
now you can do with your asp control in javascript (almost) whatever you like...
...

I`ll ask again, does anyone know solution for:
"How do I get ASP.NET to recognize javascript dynamically added items in a ListBox?"
or
How to get entered rows in dynamically populated ListBox?
Thx anyway... :)