文本框可见基于下拉列表选择项而无需javascript

时间:2015-10-07 12:56:36

标签: asp.net vb.net textbox

如果在没有javascript的情况下从vb.net中的Dropdownlist中选择特定项目时如何显示文本框

2 个答案:

答案 0 :(得分:1)

理想情况下,您可以将其放在DropDownList.OnSelectedIndexChanged事件处理程序中,但它也可以在Page_Load中使用:

If Not ddlYourDropDownList.SelectedValue Is Nothing AndAlso ddlYourDropDownList.SelectedValue = "YourParticularSelection" Then

    txtYourTextbox.Visible = True

Else

    txtYourTextbox.Visible = False

End If

答案 1 :(得分:0)

我不确定你为什么提到Javascript,因为它是一种与VB.NET不同的语言。

要解决您的问题,正如Rahul Singh所提到的,您会将代码放入DropDownList_SelectedIndexChange事件中,并根据您需要的条件显示或隐藏每个项目。这是一个例子:

Private Sub DropDownList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList.SelectedIndexChanged
        If DropDownList.SelectedIndex = 0 Then 'corresponds to the first option
            TextBox1.Show()
        ElseIf DropDownList.SelectedIndex = 1 Then 'corresponds to the second option
            TextBox1.Hide()
        End If
    End Sub

您可能还希望在Visual Studio设计视图中将TextBox1的{​​{1}}属性设置为Hidden,这样它就不会在表单启动时显示仅在用户更改True时显示。