0

I'm looking to get FindControl to "see" controls in an ASP.Net DetailsView from a VB.Net code-behind file. It doesn't find any of them.

The markup for the page is using a MasterPage.

<%@ Page 
    Title="Attendance" 
    Language="vb" 
    AutoEventWireup="false" 
    MasterPageFile="~/Knowledge Academy.Master" 
    CodeBehind="Attendance.aspx.vb" 
    Inherits="Knowledge_Academy.Attendance" %>

<asp:Content 
    ID="ContentBody" 
    ContentPlaceHolderID="BodyPlaceholder" 
    runat="server">

Currently the attributes of a our DetailsView looks like this:

<asp:DetailsView 
            ID="DetailsView" 
            runat="server" 
            AutoGenerateRows="False" 
            Height="50px" 
            Width="207px" 
            DataSourceID="SqlDataSourceDetails"
            DataKeyNames="ID"
            OnItemCommand="DetailsViewDetails_ItemCommand">

            <Fields>

Can you tell me what additional attributes to include so coding like this can "see" the fields in the DetailsView?

Protected Sub DetailsViewDetails_ItemCommand(sender As Object, e As System.Web.UI.WebControls.DetailsViewCommandEventArgs)

    Select Case e.CommandName

        Case "Add"

        Case "Edit"
            ButtonAddNewAttendance.Enabled = False

        Case "Delete"

        Case "Update"
            ButtonAddNewAttendance.Enabled = True

        Case "Insert"

        Case "New"

            Dim txtBox As TextBox
            txtBox = DetailsView.FindControl("TextBoxDateAttendanceTakenInsert")
            txtBox.Text = DateTime.Now

            Dim drpValue As DropDownList
            drpValue = DetailsView.FindControl("DropDownListClassInsert")
            drpValue.SelectedValue = 1
    End Select
End Sub

Currently FindControl can't find any of the fields in the DetailsView and gives a Null reference error.

  • UPDATE *

ItemCommand is not the correct place to put the coding.

I found out that to get this to work, OnDataBinding needs to be added as shown here plus making sure there is a handler in the code-behind file as shown below.

InsertItemTemplate markup:

<InsertItemTemplate>
    <asp:DropDownList 
        ID="DropDownListClassInsert" 
        Runat="server"
        DataSourceID="SqlDataSourceClasses"
        DataTextField = "ClassName"
        DataValueField="ID"
        SelectedValue='<%# Bind("ClassID") %>'
        AppendDataBoundItems="True"
        ForeColor="Blue"
        OnDataBinding="DropDownListClassInsert_DataBinding">
    </asp:DropDownList>

    <asp:RequiredFieldValidator ID="RequiredFieldValidatorInsertClass" runat="server" ControlToValidate="DropDownListClassInsert" 
        ErrorMessage="Please select a Class here." Font-Bold="True" Font-Italic="True" ForeColor="Red" 
        SetFocusOnError="True" Display="Dynamic">
    </asp:RequiredFieldValidator>
</InsertItemTemplate>

Handler in the code-behind file:

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

    Dim drpValue As DropDownList
    drpValue = DetailsView.FindControl("DropDownListClassInsert")
    drpValue.SelectedValue = intCurrentClassID
End Sub

Note: intCurrentClassID is declared as:

Public Shared intCurrentClassID As Integer = Nothing

after:

Public Class

I hope this helps others who had the same issues.

Emad-ud-deen
  • 4,734
  • 21
  • 87
  • 152

1 Answers1

0

I believe that you want to use the Fields property instead of FindControl. Once you have the DataControlField, you can cast it to the appropriate control type (i.e. CheckBoxField).

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • Thanks for the reply. Can you show sample coding based on our existing code? Thanks. – Emad-ud-deen Jan 07 '13 at 19:32
  • @Emad-ud-deen: Sure, but I would need to see a little bit more code in order to supply this (which is why I didn't attempt to do this in the answer). Can you show the code in the context of where you are trying to use it? – competent_tech Jan 07 '13 at 19:34
  • Ok. I will edit the posting to show the complete sub routine. – Emad-ud-deen Jan 08 '13 at 13:51