Search code examples
asp.netvb.netdetailsview

Details view finding a dropdown list box and binding


How can I find a control in a DetailsView....

I believe it needs to be done in the DataBound event and post the current value of the datasource for this field to the DDL.selectedvalue?

Dim ddl As DropDownList = DirectCast(DetailsView1.InsertItem, DropDownList).(FindControl("DropDownList1"), DropDownList)

I am adding a DDL that I would like to present the current value of the field and be selectable in edit mode.

MARK UP ADDED:

<asp:TemplateField HeaderText="Active" SortExpression="Active">
     <ItemTemplate>
           <asp:Label ID="Label2" runat="server" Text='<%# Bind("Active") %>'> </asp:Label>
           <asp:DropDownList ID="DropDownList2" runat="server"  DataTextField='<%# Bind("Active") %>' >
           </asp:DropDownList>
     </ItemTemplate>
     <EditItemTemplate>
          <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Active") %>'></asp:TextBox>
      &nbsp;<asp:DropDownList ID="DropDownList2" runat="server"  >
            </asp:DropDownList>
       </EditItemTemplate>
       <InsertItemTemplate>
    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Active") %>'></asp:TextBox>
      </InsertItemTemplate>
</asp:TemplateField> 

Solution

  • Solved it, all with inline code.

     <ItemTemplate>
         <asp:Label ID="Label2" runat="server" Text='<%# Bind("Active") %>'></asp:Label>
     </ItemTemplate>
     <EditItemTemplate>
         <asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%# Bind("Active") %>' >
              <asp:ListItem>Y</asp:ListItem>
              <asp:ListItem>N</asp:ListItem>
         </asp:DropDownList>
     </EditItemTemplate>