I have dropdownlist in my gridView header which I populate on RowDataBound event.
When I am trying to add DropDownList selectedIndexChange Event in code DropDownList is not in a lits. How do I add event handler event for dropdownlist in code?
Private Sub gvMarketingPlans_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvMarketingPlans.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
Dim ddlProducts As DropDownList = DirectCast(e.Row.Cells(1).FindControl("ddlProduct"), DropDownList)
.DataSource = DataSource
.DataTextField = "Description"
.DataValueField = "ProductCode"
.Items.Insert(0, New ListItem("< All >", ""))
Based on the code, it looks like a dropdownlist does exists from before. In the RowDataBound event you are trying to populate data in that pre-existing dropdown. Since, it pre-exists, you can define a selected change index for it.
In case you want to define it at runtime in RowDataBound, then you need to add this:
AddHandler ddlFilter.SelectedIndexChanged, AddressOfMe.ddl2_SelectedIndexChanged
ddlFilter.AutoPostBack = truePrivateSub ddlFilter_SelectedIndexChanged(ByVal sender AsObject, ByVal e As EventArgs)
'your code EndSub