toll free: (866) 611-9441

ASP.NET VB: Detect The DropDownList Control In GridView That Triggered "OnSelectedIndexChanged"

OK guys,

So, you have a GridView that will render a series of DropDownList controls upon DataBind().
Let’s first take a look at the ASPX markup to create the grid:

<asp:GridView ID=”gvTest” runat=”server” AutoGenerateColumns=”False”>

<Columns>

<asp:TemplateField >

<HeaderTemplate>

Title Here

</HeaderTemplate>

<ItemTemplate>

<asp:DropDownList ID=”ddlTest” runat=”server” OnSelectedIndexChanged=”ChangeTestField” AutoPostBack=”true”/>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

Notice that I set the AutoPostBack property to true.  Setting AutoPostBack to true allows the form to post back to the server and raise an event each time the user changes a selection in the DropDownList control. I also assigned an event handler for the SelectedIndexChanged event. The same event handler (DropDown_SelectedIndexChanged) is being used  for all DropDownList controls.

Now we’ll identity the exact control firing the event, in the VB code behind:

Protected Sub ChangeTestField(ByVal sender As Object, ByVal e As System.EventArgs)
Try

Dim ddlSelected As DropDownList = CType(sender, DropDownList)
Dim dgiSelected As GridViewRow = CType(ddlSelected.Parent.Parent, GridViewRow)          ’use it for more action in that row

Catch exAbortThread As Threading.ThreadAbortException
‘Due to Response.Redirect so ignore

Catch ex As Exception

[process ex]

End Try
End Sub

I hope that’s useful. Cheers,

Dragos

Tags: , , ,

One Response to “ASP.NET VB: Detect The DropDownList Control In GridView That Triggered "OnSelectedIndexChanged"”

Leave a Reply