Trying to get DropDownList to fire a Javascript function

J

John Kotuby

Hi all...

I am trying to do a simple thing and maybe am missing something elementary.
I have created a Javascript function at the top of a page which is meant to
enable editing of an HTML input textbox when the user makes a selection from
the DropDownList control. Since there is no OnClick event I have tried using
OnSelectedIndexChanged or OnTextChanged such as OnTextChanged ="ckCat()"
with ckCat being the javascript function name. I have purposely set
AutoPostback="false" to make sure that wasn't the problem. I have also
tried using the syntax OnTextChanged ="javascript:ckCat()" to no avail.
It's pretty easy to do in classic ASP, but I am trying to get accustomed to
the .NET way of doing things.

Does anyone know what I am missing? I am thinking there must be a way to
enable clientside handling of a DropDownList event, especially with
AutoPostback turned off. I'm just drawing a blank right now.

Thanks to all for any input...
 
P

Page Brooks

John,
You could add this to your Page_Load event on your codebehind:
cboList.Attributes.Add("onchange", "ckCat();");
 
J

John Kotuby

Thanks Page,

I was trying to figure out a way to add onclick event but the onchange is
what I really want. How does one determine which events/attributes are
"allowed" for a particular ASP control and that the control will respond as
expected? I an guessing that because the onchange attribute is allowed for
the HTML <select> tag which accomplishes the same action it would be
allowed.

Of course (light bulb just went on) the DropDownList control must render as
an HTML <select> and therefore the OnChange event would be perfectly legal
at the browser. Yes I was missing an important but simple concept. Sometimes
I get so focused on the Server side processing of Server Controls I lose
site of the fact that they are simply rendered as standard HTML.

I would love to find a chart to post by my desk "<Server Control> -- renders
as -- <HTML Tags>". Almost as important a reminder as the Page Event life
cycle on the server side.

Thanks again Page for making this task easier...
 
P

Page Brooks

John,
No problem. You are right, each Server Control ultimately renders as one
ore more client-side HTML controls. Just give it some time to sink in,
after working with the controls for a while you shouldn't have any trouble
remembering them. Just keep in mind, that some controls may render
differently depending on which properties are selected on the server-side.
Take the TextBox control for example. Changing the TextMode property of the
TextBox Server control to multiline will cause ASP.NET to render the control
as a TEXTAREA control rather than an <INPUT type="text" ...> control.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top