Handling change event in Drop Down List

R

Russ

I have a composite control with a Microsoft.Web.UI.WebControls
TabControl and Multipage controls, plus a bunch of text boxes and a
couple of DropDownLists. Of course all the controls are created
dynamically. I need to handle the event when the dropdown lists have
their selected item changed, so I can pop up a dialog to allow editing
of the item.

I cannot get the SelectedIndexChanged event to fire. Here is what I
am doing:

When the DropDownList is created, I add an event handler like:

DropDownList List = new DropDownList ();
List.SelectedIndexChanged += new System.EventHandler(EditItem);

And created a handler that is a member of the class that contains the
composite control:

private void EditItem (object sender, System.EventArgs e)
{
}

The event never fires. I read that change events do not fire until
postback time. This is not good, because I need an immediate response
to the selection change. I tried adding List.AutoPostBack = true.
This does cause post back, but the event still does not fire.

Can anyone help?

Thanks, Russ
 
D

Dale

You can use the Attributes.Add method of your controls to add an "onchange"
attribute and a value such as "ddlOnChange(this)". For example:

myDropDown.Attributes.Add("onclick", "ddlOnChange(this)");

Then add the javascript to your page either manually or using
Page.RegisterClientScriptBlock.

Search the framework documentation for "Client-Side Functionality in a
Server Control" and the Attributes.Add method as well as the
RegisterClientScriptBlock.

You should be on your way!

Dale
 
R

Russ

Dale, Thank you for the information. It did work. I'm just getting
back to this because been working on other things for a few days. I
have a few questions and problems that you may be able to help with.

In your example you are passing (this) to the OnChange handler. Why?
It does not seem to make any difference if I pass it or nothing. I
could find nothing in the suggested article you mentioned that
explains this.

I was able to get and show the selected value in a popup window as I
wanted, but have not been able to figure out how to change the
original value in the DDL when the user exits the popup with the OK
button. Here is what I am doing.

When the DDL is created, I add:

DedList.Attributes.Add("onchange", "EditDeduction(this)");

In the HTTP the following script is added:

function EditDeduction ()
{
var DedData = event.srcElement.value;

var WinSettings = "dialogHeight: 300px; dialogWidth: 400px;
dialogTop: 150px; dialogLeft: 150px; edge: Sunken; center: No; help:
No; resizable: No; status: No;"

var ReturnVal = window.showModalDialog (
"http://asrv/PayrollEntryClient/EditDeduction.aspx",
DedData, WinSettings);

if (ReturnVal != null)
event.srcElement.value = ReturnVal;
}

It all works, except that instead of replacing the line to be modified
in the DDL, a new empty line is added to the top. I have tried
various things but cannot seem to be able to access the DDL directly
with this script.

Also, I see that when the event fires, the Page_Load routine for the
popup window fires. Since it obviously does a postback (or page load
would not fire), then I see no advantage in doing this with script.
But if I could figure out how to capture the event data from the
script call, or a pointer to the DDL, in the page load I could set up
the window more easily, and handle the change to the DDL with an
onclick event from the OK button. I have gone round and round on this
but they are two separate worlds and I can find no way to get the data
from the calling page into the page called by the DDL Attribute change
event.

It all comes back to my original question. What I NEED is for the
system to work the way it appears to be documented. I can register a
change event in code like:

DedList.SelectedIndexChanged += new System.EventHandler(EditDed);
DedList.AutoPostBack = true;

The AutoPostBack is needed, according to documentation, because change
events are not handled until postback, where they are handled one at a
time in the order they are stored. But the event handler called
EditDed never fires, even though changing the selection of the DDL
does cause a postback. Clearly I am missing something here...

If you can tell me how to make this work, preferably in code, not
script, I would be very grateful.

Thanks, Russ
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top