Help needed on finding a control in a grid edit form

3 posts, 1 answers
  1. Andy Green
    Andy Green avatar
    120 posts
    Member since:
    Jul 2009

    Posted 21 May 2010 Link to this post

    Hi

    I have an EditFormTemplate and in the FormTemplate I have 2 dropdowns (They are asp:dropdowns, not Rad ones) I can populate them with data from code behind but what I want to do is populate the second ddlist from a value from the first.

    This is the first ddl markup

    <

     

    asp:DropDownList ID="ddlSite" runat="server" AutoPostBack="true" SelectedIndexChanged="UpdateLocation"></asp:DropDownList>

    On ther server I get the UpdateLocation event

     

        Protected Sub UpdateLocation(ByVal sender As Object, ByVal e As System.EventArgs)  
     
     
            Dim ddlLocation As DropDownList = DirectCast(FindControl("ddlLocation"), DropDownList)  
            Dim mw As New CoreData  
            Dim mw_dt As DataTable = mw.LocationByParent(sender.selectedvalue)  
            If mw_dt.Rows.Count > 0 Then  
                With ddlLocation  
                    .Items.Add(New ListItem("", 0))  
                    .DataSource = mw_dt 
                    .DataValueField = "LocationId" 
                    .DataTextField = "LocationName" 
                End With  
            End If  
     
        End Sub 
    The problem is that the value for the second dropdownlist is always nothingm the findcontrol is failing.

    I have also tried the following to find the control:

     

    Dim editedItem As GridEditableItem = CType(CType(sender, DropDownList).NamingContainer, GridEditableItem)

     

     

    Dim ddlLocationt As DropDownList = CType(editedItem("ddlLocation").Controls(0), DropDownList)

     

     

    How do I get a handle on the second DropdownList?

    Andy

     

     

  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 May 2010 Link to this post

    Hello,

    You can access the second DropDownList using the NamingContainer property after getting reference to the first DropDownList. Here is an example.

    VB:
     
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As ObjectByVal e As EventArgs) 
        Dim dropDown As DropDownList = DirectCast(sender, DropDownList) 
        Dim editForm As GridEditFormItem = DirectCast(dropDown.NamingContainer, GridEditFormItem) 
        Dim secondDropdown As DropDownList = DirectCast(editForm.FindControl("DropDownList2"), DropDownList) 
        secondDropdown.Enabled = False 
    End Sub 


    -Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Andy Green
    Andy Green avatar
    120 posts
    Member since:
    Jul 2009

    Posted 21 May 2010 Link to this post

    Thanks Shinu

    Andy
Back to Top