Asp.net 在文本框中从DropDownList选择多个项目,无重复项
我使用以下技术从DropDownList中选择多个项目到文本框中,没有重复项,但是我不认为这是最合适的方法,没有任何想法 再次按,并选择相同的值 从DDL中选择另一个值并按下按钮 这是我的密码Asp.net 在文本框中从DropDownList选择多个项目,无重复项,asp.net,vb.net,web-applications,drop-down-menu,Asp.net,Vb.net,Web Applications,Drop Down Menu,我使用以下技术从DropDownList中选择多个项目到文本框中,没有重复项,但是我不认为这是最合适的方法,没有任何想法 再次按,并选择相同的值 从DDL中选择另一个值并按下按钮 这是我的密码 Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click If TextBox2.Text.Contains(DropDownList1.Sel
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
我觉得这个逻辑很正确。我唯一会做的不同的事情是:
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
,避免过早返回。为清晰起见,最好只使用一个出口点对功能进行编程。在这种情况下,没有理由不这样做