使用 FindControl 从 VB.Net 代码隐藏文件中定位 ASP.Net DetailsView 中的控件不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14202118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using FindControl to locate controls in an ASP.Net DetailsView from a VB.Net code-behind file not working
提问by Emad-ud-deen
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.
我希望 FindControl 从 VB.Net 代码隐藏文件中“查看”ASP.Net DetailsView 中的控件。它没有找到任何一个。
The markup for the page is using a MasterPage.
该页面的标记正在使用 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:
目前,我们的 DetailsView 的属性如下所示:
<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?
您能告诉我要包含哪些附加属性,以便这样的编码可以“看到”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.
目前 FindControl 无法在 DetailsView 中找到任何字段并给出 Null 引用错误。
- UPDATE *
- 更新 *
ItemCommand is not the correct place to put the coding.
ItemCommand 不是放置编码的正确位置。
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.
我发现要使其正常工作,需要按此处所示添加 OnDataBinding,并确保代码隐藏文件中有一个处理程序,如下所示。
InsertItemTemplate markup:
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>
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:
注意: intCurrentClassID 声明为:
Public Shared intCurrentClassID As Integer = Nothing
after:
后:
Public Class
I hope this helps others who had the same issues.
我希望这可以帮助其他有同样问题的人。
回答by competent_tech
I believe that you want to use the Fieldsproperty instead of FindControl. Once you have the DataControlField, you can cast it to the appropriate control type (i.e. CheckBoxField).
我相信您想使用Fields属性而不是 FindControl。拥有DataControlField 后,您可以将其转换为适当的控件类型(即CheckBoxField)。

