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?
<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?