AddHandler problem

S

Shawn

Hi.
I'm trying to add an event to a DropDownList from my code, but so far I
haven't been able to do it right. The DropDownList is inside a DataGrid. The
reason why I'm not adding an onSelectedIndexChanged attribute in the html
code is because not all of the DropDownLists in the DataGrid is to have this
event. Here is a simplified version of my code:

For i = 0 To dgParameter.Items.Count - 1
dataGridItem = dgParameter.Items(i)

myDDL = CType(dataGridItem.FindControl("ddlValue"), DropDownList)
myDDL.AutoPostBack = True
AddHandler myDDL.SelectedIndexChanged, AddressOf
myDDL_SelectedIndexChanged
Next

Public Sub myDDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs)
'...
'...
End Sub

I tried replacing the AddHandler line with this one, but still nothing.
myDDL.Attributes.Add("onselectedindexchanged", "myDDL_SelectedIndexChanged")

Any help appreciated!

Thanks,
Shawn
 
S

Shawn

Hi Kevin,

Thanks for answering. Unfortunately I didn't understand enough of it to come
up with a solution :)

Do you think you could help me along a little bit? Maybe give me an example
I could try out?



Thanks,

Shawn
 
G

Guest

Can you please clarify this portion of your code

<quote
myDDL = CType(dataGridItem.FindControl("ddlValue"), DropDownList
</quote

That is, how are you finding the "ddlValue" for the exact control. Once it is in a templated item, .NET will create the *true* UniqueID for it and it will not simply be the name you gave it. (something like "dgParameter:_ct14:DropDownName") Which means your FindControl function will likely not work

To add functionality to specific records in your datagrid - perhaps you should make a routine that occurs on the DataGrid.ItemCreated event that adds that dropdown for you

Here is a sample

Private Sub dgTest_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgTest.ItemCreate
Dim ddlTest As New DropDownLis
ddlTest.AutoPostBack = Tru
AddHandler ddlTest.SelectedIndexChanged, AddressOf SpecialDropDownHandle
e.Item.Cells(0).Controls.Add(ddlTest
End Sub
 

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

Top