简体   繁体   中英

How to add an event handler to a recurring DropDownList

I am working on a website in ASP.Net/VB.NET that has a recurring design - the user has to enter the same type of data for several different topics.

I can't get the .SelectedIndexChange working for my application. I wanted to use JavaScript/JQuery to get this working.

Here is the code I have right now, which does not work. No errors are thrown, but the AppChange() method is never called.

List.Items.Add("Choose...")
List.Items.Add("Yes")
List.Items.Add("No")
'List.AutoPostBack = True
'AddHandler List.SelectedIndexChanged, AddressOf ListChange

Please note I have the last two lines commented out because I was trying other things.

Dim ListID = String.Concat("ListOption", DBReader("nListID"))
List.ID = ListID
AddHandler List.SelectedIndexChanged, AddressOf ListChange
                                d.Controls.Add(List)

And here is the method for the change event

Protected Sub ListChange(sender As Object, e As EventArgs)
        Label1.Text = "It worked"

    End Sub

The Source code looks like this...

<td align="right" style="font-family:Calibri;">Options</td><td></td><td><select name="ListOption1" id="ListOption1" style="width:230px;">

So obviously something did not register and I'm not too sure what.

Have you tried adding a handler to your sub?

Private Sub ListChange(sender As Object, e As EventArgs) Handles ListChange.SelectedIndexChanged

    Label1.Text = "It worked"

End Sub

I got help from my developer mentor to get this task done.

in the .aspx file, you add the function like so

<script type="text/javascript">
    function Change(value, id)
    {
        alert("It worked");
    }
</script>

and in the .aspx.vb file,

List.Attributes.Add("onchange", "javascript:Change(this.value, this.id)")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM