4

We have a DropDownList in the markup of an ASP.Net / VB.Net web form.

We are wanting to populate the DropDownList with data from a DataSet created from the DataSet designer but the coding we are using in the code-behind file does not find the DropDownList ID using FindControl.

Can you check my coding and let me know what I still need to do to get the DropDownList populated?

Markup of the DropDownList:

<% '-- DetailsView (Grid) for details of the GridView -- %>
<% '---------------------------------------------------- %>
<asp:DetailsView 
    ID="DetailsView" 
    runat="server" 
    AutoGenerateRows="False" 
    Height="50px" 
    Width="207px" 
    DataSourceID="SqlDataSourceDetails"
    DataKeyNames="ID"
    OnItemCommand="DetailsViewDetails_ItemCommand"
    OnDataBound="DetailsView_DataBound">

<Fields>

<asp:TemplateField HeaderText="Class:" SortExpression="ClassID">

     <EditItemTemplate>
          <asp:DropDownList ID="DropDownListClass" Runat="server"> </asp:DropDownList>

          <asp:RequiredFieldValidator ID="RequiredFieldValidatorEditDropDownListClass" runat="server" 
              ControlToValidate="DropDownListClass" 
              ErrorMessage="Please select a class." Font-Bold="True" Font-Italic="True" ForeColor="Red" 
              SetFocusOnError="True" Display="Dynamic">
          </asp:RequiredFieldValidator>
      </EditItemTemplate>

      <ItemTemplate>
          <asp:Literal ID="LiteralClass" runat="server" 
              Text='<%# FormatAsMixedCase(Eval("ClassName").ToString())%>' />
      </ItemTemplate>

      <ItemStyle ForeColor="Blue" />
 </asp:TemplateField>

 </Fields>

Coding in the code-behind file:

Protected Sub DetailsView_DataBound(sender As Object, e As EventArgs)

    Dim theClassesTableAdapter As New DataSetClassesTableAdapters.ClassesTableAdapter
    Dim ddlTheDropDownList = DirectCast(FindControl("DropDownListClass"), DropDownList)

    ddlTheDropDownList.DataSource = theClassesTableAdapter.GetDataByAllClasses
    ddlTheDropDownList.DataTextField = "ClassName"
    ddlTheDropDownList.DataValueField = "ClassID"
    ddlTheDropDownList.SelectedValue = "ClassID"
    ddlTheDropDownList.DataBind()
End Sub

Markup of the DataSouce of the DetailsView:

<% '-- Datasources -- %>
<% '----------------- %>
<asp:SqlDataSource 
    ID="SqlDataSourceDetails" 
    runat="server" 

    ConnectionString="<%$ ConnectionStrings:Knowledge Academy %>" 

    DeleteCommand=
        "DELETE FROM [TeacherSchedule] 
          WHERE [ID] = @ID" 

    InsertCommand=
        "INSERT INTO [TeacherSchedule] 
            ([DayOfWeek], 
             [Grade],
             [StartTime],
             [EndTime],
             [ClassID]) 
        VALUES (@DayOfWeek, 
                @Grade, 
                @StartTime, 
                @EndTime,
                @ClassID)" 

    SelectCommand=
        "SELECT        TeacherSchedule.ID, TeacherSchedule.Grade, TeacherSchedule.StartTime, TeacherSchedule.EndTime, TeacherSchedule.TeacherID, TeacherSchedule.ClassID, 
                     TeacherSchedule.DayOfWeek, Classes.ClassName, Teachers.Forename,  Teachers.Surname
           FROM            TeacherSchedule INNER JOIN
                     Classes ON TeacherSchedule.ID = Classes.ID INNER JOIN
                     Teachers ON TeacherSchedule.ID = Teachers.ID
          WHERE (TeacherSchedule.ID = @ID)" 

    UpdateCommand=
        "UPDATE [TeacherSchedule] 
            SET [DayOfWeek] = @DayOfWeek, 
                [Grade] = @Grade, 
                [StartTime] = @StartTime,
                [EndTime] = @EndTime,
                [ClassID] = @ClassID
            WHERE [ID] = @ID">

    <DeleteParameters>
        <asp:Parameter Name="ID" Type="Int32" />
    </DeleteParameters>

    <InsertParameters>
        <asp:Parameter Name="DayOfWeek" Type="String" />
        <asp:Parameter Name="Grade" Type="String" />
        <asp:Parameter Name="StartTime" Type="String" />
        <asp:Parameter Name="EndTime" Type="String" />
        <asp:Parameter Name="ClassID" Type="Int32" />
    </InsertParameters>

    <SelectParameters>
        <asp:ControlParameter ControlID="GridViewSummary" Name="ID" PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>

    <UpdateParameters>
        <asp:Parameter Name="DayOfWeek" Type="String" />
        <asp:Parameter Name="Grade" Type="String" />
        <asp:Parameter Name="StartTime" Type="String" />
        <asp:Parameter Name="EndTime" Type="String" />
        <asp:Parameter Name="ClassID" Type="Int32" />
        <asp:Parameter Name="ID" />
    </UpdateParameters>
</asp:SqlDataSource>
Emad-ud-deen
  • 4,734
  • 21
  • 87
  • 152
  • Could you please include the full markup for the grid you are are data-binding too, as well as the code in the code behind to bind to the actual grid? (This is the control that actually houses the `TemplateField`. – mclark1129 Dec 04 '12 at 19:22
  • I updated the markup to include the information. I had to leave out the other fields because this DetailsView has a lot of fields. The DetailsView datasource is populated from the markup I'm adding to the original posting for you. Also many columns in the DataSource markup have been removed to save space on this posting. – Emad-ud-deen Dec 04 '12 at 19:30

1 Answers1

3

Try and populate your DropDownList in the DropDownList_Init event handler.

Markup:

<asp:DropDownList ID="ddlTheDropDownList" runat="server" OnInit="ddlTheDropDownList_Init">

The code behind should look something like this, I'm more used to C# but I hope you understand the point:

Protected Sub ddlTheDropDownList_Init(sender As Object, e As EventArgs)
    Dim ddl As DropDownList
    ddl = sender As DropDownList
    ddl.Datasource = theClassesTableAdapter.GetDataByAllClasses
    ddl.DataTextField = "ClassName"
    ddl.DataValueField = "ClassID"
    ddl.SelectedValue = "ClassID"
    ddl.DataBind()
End Sub
Denki
  • 80
  • 1
  • 1
  • 9