Click to See Complete Forum and Search --> : [RESOLVED] HtmlSelect onclick attribute


Visslan
August 21st, 2007, 09:16 AM
Hi!

I'm having problem with my HtmlSelect object. I want the page to do a post back when a new item has been selected in the dropdownlist. At the moment my code looks like this:


...
protected HtmlSelect ddlChooseDateForInstallation;
...
public void OnSelected_ddlChooseDateForInstallation( object sender, System.EventArgs e )
{
...
}



...
<div><select ID="ddlChooseDateForInstallation" onclick="OnSelected_ddlChooseDateForInstallation" Runat="server" NAME="ddlChooseDateForInstallation"/></div>
...


Now the program never runs the eventhandler OnSelected_ddlChoosenDateForInstallation? Why? And how can I make it work?

PeejAvery
August 21st, 2007, 11:41 AM
Well, you have a conflict in your HTML. Notice that you have the <div> tag closing before you have even finished the <select>. What I am wondering is why you have runat specified in a regular HTML tag. For runat to be server, don't you need ASP tags? Also, I would suggest using onchange rather than onclick.

<asp:ListBox ID="ddlChooseDateForInstallation" runat="server" onchange="OnSelected_ddlChooseDateForInstallation">
<asp:ListItem>Option 1</asp:ListItem>
<asp:ListItem>Option 2</asp:ListItem>
<asp:ListItem>Option 3</asp:ListItem>
</asp:ListBox>

Visslan
August 22nd, 2007, 10:18 AM
I can't use an asp control because I then can't change the colours for some elements in the dropdownlist. There's a bug in html select so you have to do like this if you want the postback to work:


<select ... onchange="javascript:{if (typeof(Page_ClientValidate) != 'function'||Page_ClientValidate()) __doPostBack('HtmlSelectID','');" ... />

PeejAvery
August 22nd, 2007, 11:13 AM
All perfect except you should get rid of the javascript:. Onchange is already a JavaScript event.