【问题标题】:Select multiple items from DropDownList into TextBox, No duplicates从 DropDownList 中选择多个项目到 TextBox 中,没有重复项
【发布时间】:2011-05-21 22:23:11
【问题描述】:

我使用以下技术从 DropDownList 中选择多个项目到没有重复的 TextBox 中,但是我认为这不是最合适的方法,任何想法。

再次按下选择相同的值

从 DDL 中选择另一个值并按下按钮

这是我的代码

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    If TextBox2.Text.Contains(DropDownList1.SelectedItem.Text) Then
        Dim m As New Label
        m.Text = "duplicate !"
        Me.form1.Controls.Add(m)
        Exit Sub

    End If
    If TextBox2.Text = "" Then
        TextBox2.Text = DropDownList1.SelectedItem.Text
    Else
        TextBox2.Text = TextBox2.Text + " , " + DropDownList1.SelectedItem.Text
    End If
End Sub

【问题讨论】:

    标签: asp.net vb.net web-applications drop-down-menu


    【解决方案1】:

    逻辑对我来说看起来很正确。我唯一会做的不同的事情是:

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
       If TextBox2.Text.Contains(DropDownList1.SelectedItem.Text) Then
         Dim m As New Label
         m.Text = "duplicate !"
         Me.form1.Controls.Add(m)
       Else If TextBox2.Text = "" Then
         TextBox2.Text = DropDownList1.SelectedItem.Text
       Else
         TextBox2.Text = TextBox2.Text + " , " + DropDownList1.SelectedItem.Text
       End If
    End Sub
    

    我会构造和If..else if...else 并避免过早返回。为清楚起见,最好只使用一个退出点对函数进行编程。在这种情况下没有理由不这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多