Hi all,
How do you set the 'onclick'/'onchange'/'click' event to a specific listbox so that when you click on one of the items in the listbox, you get for instance a "Hello" from the javascript alert("hello")? When I click on the listitem, nothing happens.
This is what I think I should do on the load of my main page. ddl1 is the listbox.
foreach (ListItem i in ddl1.Items)
{
i.Attributes.Add("onclick", "return listitems(this)");
}
Please please any help.
Thanks,
Listbox items and attributes
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 16, 2010, 5:41 AM EDT
There is no click event associated with listbox control....
you have to write your code inside the SelectedIndexChanged event handler or on OnLoad event
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
i.Attributes.Add("onclick", "return listitems(this)");
}
Space Ghost45 reputationPosted Mar 16, 2010, 6:01 AM EDT
thanks for the reply. This makes sense, I will try it.
cheers