Handling Dropdownlist inside gridview

Often there is a need to handle OnSelectedIndexChanged event of a dropdown residing inside a GridView. This can be handled as following:

1. Specify an event handler from dropdown in ASPX

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList OnSelectedIndexChanged="ddl_Change" DataTextField="UserName" AutoPostBack="true" DataValueField="userId" ID="ddl" runat="server"/>

2. Add event handler in ASPX.cs


Protected Sub ddl_Change(ByVal sender As Object, ByVal e As EventArgs)
'You can retrieve Dropdown as:
Dim DropDownControl As DropDownList = CType(sender, DropDownList)
'You can retrieve Parent GridRow as:
Dim gridRow As GridViewRow = CType(CType(sender, DropDownList).NamingContainer, GridViewRow)
' Do stuff with your dropdown and grid row like populating other dropdown.
End Sub

Comments

Popular posts from this blog

How to construct a B+ tree with example

How to show only month and year fields in android Date-picker?

Visitor Counter Script Using PHP