Forum Moderators: open

Message Too Old, No Replies

Capturing a selection event for DropdownLists

How do I trigger a procedure based on the selection of items in Dropdowns?

         

Barbacena

6:07 pm on Jun 3, 2005 (gmt 0)

10+ Year Member



Hi there, this is probably a basic question, but I was wondering how I capture a selection event in a asp:DropdownList control?

For example, if I want to execute something but only if there was NO selection in my DropDownList control, what would I have to target? The "SelectedItem.Value" or the "ddlLang.SelectedIndexChanged" event? I'm very new to this but I tried something along these lines

if (MyDropDown.SelectedItem.Value -= 0){

//Do something}

But I get the following error:

Operator '-=' cannot be applied to operands of type 'string' and 'int'

Then I tried the following:

if (!MyDropDown.SelectedIndexChanged){
//Do Something}

And got the following error:

The event 'System.Web.UI.WebControls.ListControl.SelectedIndexChanged' can only appear on the left hand side of += or -=

I'm obviously using the wrong syntax or something, could someone please clarify me on this?

Thanks

Balloon

4:47 pm on Jun 6, 2005 (gmt 0)

10+ Year Member



Hi,

You need to set the "AutoPostBack" property of the control to true in the ASPX file - something like this...

<asp:DropDownList id="MyDropDown" Runat="server" AutoPostBack="true"></asp:DropDownList>

... then in the VB file, you need to create an event handler function which will get fired each time the user selects something...

Private Sub MyDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyDropDown.SelectedIndexChanged

End Sub

In this function, you can then check what the value of the user's selection is with this line of code...

if (MyDropDown.SelectedValue = 0) then

end if

... hope that helps!
- Chris

Balloon

4:48 pm on Jun 6, 2005 (gmt 0)

10+ Year Member



Sorry - just noticed you were writing in C#. My example was in VB, but the rules are all pretty similar!

- Chris