Asp.net 如何在DetailsView中引用DropDownList?

Asp.net 如何在DetailsView中引用DropDownList?,asp.net,vb.net,Asp.net,Vb.net,我有一个DetailsView,其中包含几个文本框和一个下拉列表 现在,我想做的是为DropDownList编写SelectedIndexChanged事件,但我的问题是我无法再访问/引用它 如果我只是在表单上放一个下拉列表,这没问题,但现在它在Details视图中,似乎不可能找到它 有什么建议吗 Imports System Imports System.Web.UI Imports System.Web.UI.WebControls Protected Sub DetailsViewAvt

我有一个DetailsView,其中包含几个文本框和一个下拉列表

现在,我想做的是为DropDownList编写SelectedIndexChanged事件,但我的问题是我无法再访问/引用它

如果我只是在表单上放一个下拉列表,这没问题,但现在它在Details视图中,似乎不可能找到它

有什么建议吗

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls

Protected Sub DetailsViewAvtaleInfo_PageIndexChanging1(sender As Object, e As DetailsViewPageEventArgs) Handles DetailsViewAvtaleInfo.PageIndexChanging

    Dim DropDownListContractType As DropDownList = DirectCast(Me.DetailsViewAvtaleInfo.FindControl("DropDownListContractType"), DropDownList)
    Dim TextBoxLeasingPriceMonth As TextBox = DirectCast(Me.DetailsViewAvtaleInfo.FindControl("TextBoxLeasingPriceMonth"), TextBox)

    If DropDownListContractType.Text = "Kjøp" Then
        TextBoxLeasingPriceMonth.Visible = False
    End If

End Sub
我知道这是不对的,但就我所知。我只想在以下子系统中编写代码,但它无法工作:

    Protected Sub DropDownListContractType_SelectedIndexChanged(sender As Object, e As EventArgs)      Handles DropDownListContractType.SelectedIndexChanged
    Dim TextBoxLeasingPriceMonth As TextBox = DirectCast(Me.DetailsViewAvtaleInfo.FindControl("TextBoxLeasingPriceMonth"), TextBox)

    If DropDownListContractType.Text = "Leasing" Then
        TextBoxLeasingPriceMonth.Visible = False
    End If
End Sub 

您没有发布dropdownlist的标记,它应该如下所示(将方法附加到事件):

<asp:DropDownList ID="DropDownListContractType" AutoPostBack="true"  runat="server" OnSelectedIndexChanged="DropDownListContractType_SelectedIndexChanged">                        
    <asp:ListItem Text="Kjøp" Value="2"></asp:ListItem>
    <asp:ListItem Text="Leasing" Value="1"></asp:ListItem>
</asp:DropDownList>
我已经测试了上面的代码。如果您有任何问题,请尝试复制/粘贴标记和代码

您可以下载我的测试项目与您的进行比较。

“…但它不会工作”-更具体一些。它是在第一段代码中看到您的下拉列表,还是仅在第二段代码中看到您的下拉列表?为什么不使用FindControl()获取下拉列表?
Protected Sub DropDownListContractType_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim DropDownListContractType As DropDownList = DirectCast(sender, DropDownList)
    Dim TextBoxLeasingPriceMonth As TextBox = DirectCast(DropDownListContractType.NamingContainer.FindControl("TextBoxLeasingPriceMonth"), TextBox)

    If DropDownListContractType.SelectedItem.Text = "Leasing" Then
        TextBoxLeasingPriceMonth.Visible = False
    End If
End Sub


Protected Sub DetailsViewAvtaleInfo_PageIndexChanging(sender As Object, e As DetailsViewPageEventArgs) Handles DetailsViewAvtaleInfo.PageIndexChanging
    Dim DropDownListContractType As DropDownList = DirectCast(Me.DetailsViewAvtaleInfo.FindControl("DropDownListContractType"), DropDownList)
    Dim TextBoxLeasingPriceMonth As TextBox = DirectCast(Me.DetailsViewAvtaleInfo.FindControl("TextBoxLeasingPriceMonth"), TextBox)

    If DropDownListContractType.SelectedItem.Text = "Kjøp" Then
        TextBoxLeasingPriceMonth.Visible = False
    End If
End Sub