2014-01-26 26 views
0

我有一个DetailsView,它包含几个文本框和一个DropDownList。如何引用DetailsView中的DropDownList?

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

如果我只是在窗体上放置一个DropDownList,它没有问题,但现在它是在DetailsView中看起来不可能达到它。

有什么建议吗?

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

“...但它不会工作” - 更具体。它在第一段代码中看到了你的Dropdown还是仅在第二段?为什么不使用FindControl()来获取下拉菜单? – 2014-01-26 01:01:15

回答

0

你没有贴标记的下拉列表,它应该像下面(附方法的情况下)

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

你的代码也有一些问题:

  1. 应该是DropDownList.SelectedItem.Text,而不是 DropDownList.Text

  2. 您必须将sender转换为DropDownList,DropDownListContractType_SelectedIndexChanged方法。

  3. 找到你要的DropDownListNamingContainerDropDownListContractType_SelectedIndexChanged方法

如果你有下面的代码,它应该工作:

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 

我已经测试上面的代码。如果您有任何问题,请尝试复制/粘贴标记和代码。

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

相关问题