Dynamically (in code) add a Event Proc Prop??

G

Groove

This might be fairly easy to do. Hopefully, it is.

(asp.net 2 / vb)

I have a Drop Down List and in th code-behind vb file, I'd like to
optionally add a OnSelectedIndexChanged attribute to it. Depending on
whether certain conditions are met when the page loads, I'd like to be able
to optionally add this attribute to my DDL. Does this make sense? I tried
using this but it doesn't work:

ddlOperatingItems.Attributes.Add("OnSelectedIndexChanged",
"ddlExpenseItems_SelectedIndexChange")

This shows up in the html source code:
OnSelectedIndexChanged="ddlExpenseItems_SelectedIndexChange"


Essentially what I'm trying to accomplish is that **under certain
conditions**, require the user to provide a brief explaination if they chose
"Other" in the DDL. The txtbox and RequiredValidator become enabled. The
trick is optionally making this DDL change on SelectedIndex. I don't want
this to **always** be in place.


Here's clips of my code.

Sub Page_load
If myVariable= True Then
ddlOperatingItems.Attributes.Add("OnSelectedIndexChanged",
"ddlExpenseItems_SelectedIndexChange")
End If
End Sub



Protected Sub ddlExpenseItems_SelectedIndexChange(ByVal Sender As Object,
ByVal E As EventArgs)

If ddlOperatingItems.SelectedValue = XXX Then
txtOtherDescription.Enabled = True
txtOtherDescription.Text = ""
OtherDescRequired.Enabled = True
Else
txtOtherDescription.Enabled = False
txtOtherDescription.Text = "Not Required"
OtherDescRequired.Enabled = False
End If

End Sub
 
M

Marina Levit [MVP]

You are adding a client side event handler. And I believe the javascript
<select> object does not have an OnSelectedIndexChanged. This is a server
side event only.

To add server side events, use AddHandler.

So, something ilke:

AddHandler ddlOperatingItems.SelectedIndexChanged, AddressOf
ddlExpenseItems_SelectedIndexChanged
 
G

Groove

Thanks!

I added this:
AddHandler ddlOperatingItems.SelectedIndexChanged, AddressOf
ddlExpenseItems_SelectedIndexChange

to my code in page_load and nothing seems to happen when change the values
in the DDL. I'm NOT testing for PostBack so it should be loading this each
and every time. VWD Express isn't indicating any syntax errors and the page
compiles and loads fine. Just nothing happens.

Any ideas?



Thanks
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top