0

VB.Net コード ビハインド ファイルから ASP.Net DetailsView のコントロールを「表示」する FindControl を取得しようとしています。それらのどれも見つかりません。

ページのマークアップは 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">

現在、DetailsView の属性は次のようになっています。

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

            <Fields>

このようなコーディングで 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

現在、FindControl は DetailsView 内のどのフィールドも見つけることができず、Null 参照エラーが発生します。

  • アップデート *

ItemCommand は、コーディングを配置する正しい場所ではありません。

これを機能させるには、ここに示すように OnDataBinding を追加する必要があり、さらに、以下に示すようにコード ビハインド ファイルにハンドラーがあることを確認する必要があることがわかりました。

InsertItemTemplate マークアップ:

<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>

コード ビハインド ファイルのハンドラー:

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

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

注: intCurrentClassID は次のように宣言されます。

Public Shared intCurrentClassID As Integer = Nothing

後:

Public Class

これが同じ問題を抱えている他の人に役立つことを願っています。

4

1 に答える 1

0

FindControl の代わりにFieldsプロパティを使用したいと考えています。DataControlFieldを取得したら、それを適切なコントロール タイプ (つまりCheckBoxField ) にキャストできます。

于 2013-01-07T19:25:13.637 に答える