使用FindControl从VB.Net代码隐藏文件中找不到ASP.Net DetailsView中的控件

时间:2013-01-07 18:47:06

标签: asp.net vb.net code-behind detailsview findcontrol

我希望让FindControl从VB.Net代码隐藏文件中“看到”ASP.Net DetailsView中的控件。它找不到任何一个。

页面的标记使用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

我希望这可以帮助那些遇到同样问题的人。

1 个答案:

答案 0 :(得分:0)

我相信你想使用Fields属性而不是FindControl。获得DataControlField后,您可以将其转换为适当的控件类型(即CheckBoxField)。