views:

1541

answers:

5

Hi, I have a website where the user requires to add and remove Headlines from the main page. The headlines are stored in a database with a column titled 'Display' (true or false) which decides if the headlines appear. I want the user to have the ability to add or remove headlines from the main page, so have created another page which creates a table of all headlines along with a drop down list containing 'Enable' and 'disable'. I want these drop down lists to work by when the user changes the value in the drop down list the database will be updated. Only I cant find how to give the drop down lists an on change event.

any ideas? am I going about this the right way or is there a better way to do this?

Thanks Mark

A: 

First; personally I would use check boxes for providing a user interface for a true/false value. You can handle this in two ways: either you use ASP.NET controls (<asp:CheckBox ... />) or plain HTML controls (<input type="checkbox" .../>).

In the case of using the ASP.NET controls, you can simply double click on the control (in design view) in order to have an event handler for the CheckedChanged event to be created for you. In the case of using an HTML input element, you will need to handle the onclick event on the client side using JavaScript, and have the JavaScript make an AJAX call or similar so that the server can update the database. I would say that the ASP.NET approach is simpler.

Fredrik Mörk
A: 

Most likely you easiest method for this is to simply use a DataGrid, you can then handle the event easily.

As an alternative, since you are simply doing a "show" or "Hide" option, you could just use buttons, which might provide a better UI. (I find this true due to accidental scrolling of drop down list items with a scroll wheel mouse in some browsers)

Mitchel Sellers
+1  A: 

First, to do your actual request You would set the dropdowns to autopostback, and add a handler to get the value and update the data, IE (although at minimum your dropdowns would be in a repeater, or datagrid)

    Private Sub SetupDropdowns()
        Dim pDropDown As New DropDownList
        pDropDown.Items.Add("Yes")
        pDropDown.Items.Add("No")
        pDropDown.AutoPostBack = True
        AddHandler pDropDown.SelectedIndexChanged, AddressOf pDropDown_SelectedIndexChanged
    End Sub

    Private Sub pDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim value As String = CType(sender, DropDownList).SelectedValue
'Do something with value
    End Sub

An easier way, if you are accessing SQL directly is to use a datagrid and databind it to your SQL query.

TJ
A: 

Thanks alot, using a datagrid did the trick!

A: 

In the code where you create your dropdowns, add this:

''// You already have this code...

Dim oDropDown as New DropDown

''// Whatever other declarations you need...

AddHandler oDropDown.SelectedIndexChanged, _ 
AddressOf oDropDown_SelectedIndexChanged

Normally, you add a dropdown to your project from the toolbox, and you can use the dropdown menu under codeview to select the SelectedIndexChanged event. We have to do this manually in this instance, so create your subroutine:

Public Sub oDropDown_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ''// Do whatever you need to do here

    ''// I sometimes do this to make code manipulation easier:
    Dim _sender as DropDown = CType(sender, DropDown)

End Sub

Hope that helps!

Anders