views:

1692

answers:

1

Okay, so I'm having issues getting the value of a DropDownList that's inside of a TemplateField when updating my GridView. Originally I was using the RowCommand event to check the command name and then performing the appropriate task (update/delete), but I had problems with the event firing twice, so I switched it out for separate events (RowUpdating, RowDeleting). After doing this, FindControl is returning null every time. Just FYI, the gridview is inside of an UpdatePanel that has an AsyncPostBackTriggers for RowEditing, RowUpdating and RowDeleting events.

Here's my TemplateField inside of the GridView:

<asp:TemplateField HeaderText="Type">
    <ItemTemplate>
        <asp:Label 
            ID="lblMedTypeEdit" 
            Text='<%# Bind("medDesc") %>' 
            runat="server">
        </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:DropDownList 
            ID="ddlMedTypeEdit" 
            DataSourceID="srcMedTypes" 
            SelectedValue='<%# Bind("medtype") %>' 
            runat="server" 
            DataTextField="medDesc" 
            DataValueField="medCode">
        </asp:DropDownList>                             
    </EditItemTemplate>
</asp:TemplateField>

Here is the code I'm using inside of

Protected Sub gvCurrentMeds_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvCurrentMeds.RowUpdating
    Dim intRowIndex As Integer = e.RowIndex
    Dim ddlMedType As DropDownList = CType(Me.gvCurrentMeds.Rows(intRowIndex).Cells(1).FindControl("ddlMedTypeEdit"),DropDownList)
End Sub

I also tried using a recursive function to find the control (below), but it is still returning back null.

Public Function FindControlRecursive(ByVal root As Control, ByVal id As String) As Control
    If root.ID = id Then
        Return root
    End If

    For Each c As Control In root.Controls
        Dim t As Control = FindControlRecursive(c, id)
        If Not t Is Nothing Then
            Return t
        End If
    Next
    Return Nothing
End Function
A: 

If you just want to know what the new value of the dropdown is, this is already provided for you in the NewValues property of the GridViewUpdateEventArgs object passed to the event handler.

In your example, e.NewValues["medtype"] should be the updated value.

You've already specified <%# Bind(...) %> on the dropdown, so ASP.NET will do the work of finding the controls and getting the new values for you - you don't have to plumb the control hierarchy yourself.

Sam