此代码将在文本框中将所选复选框文本显示为1,2,3,4,5,6,7,8,9

时间:2010-11-13 14:23:38

标签: asp.net vb.net

此代码将显示所选复选框文本框中的文本,如我检查的顺序1,2,3,4,5,6,7,8,9

但它不会在9

之后显示文本框中的选中复选框

Partial Class _45seater_WebUserControl     继承System.Web.UI.UserControl

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim str As String = Nothing
    Dim id As String = Nothing
    Dim ch As String = Nothing
    For Each ctrl As Control In Panel1.Controls

        If ctrl.GetType() Is GetType(CheckBox) Then
            Dim chk As CheckBox = ctrl
            UpdatePanel1.FindControl("chk")
            If chk.Checked = True Then
                If TextBox1.Text = "" Then
                    TextBox1.Text = chk.Text
                Else
                    Dim SearchString As String = chk.Text
                    id = TextBox1.Text
                    If id.Contains(SearchString) <> -1 Then
                        TextBox1.Text = TextBox1.Text + "," + chk.Text
                    Else

                    End If

                End If
            Else
                Dim SearchString As String = chk.Text
                id = TextBox1.Text
                If id.Contains(SearchString) <> -1 Then

                Else
                    id = RemoveSubString(id, chk.Text)
                    TextBox1.Text = id
                End If

            End If

        End If
    Next
End Sub
Private Function RemoveSubString(ByVal stringvalue As String, ByVal stringremove As String) As String
    Dim pos As Integer = stringvalue.IndexOf(stringremove)
    If pos > 0 Then
        Return stringvalue.Remove(pos - 1, stringremove.Length + 1)
    ElseIf pos = 0 Then
        If stringvalue.Contains(",") <> -1 Then
            Return stringvalue.Remove(pos, stringremove.Length)
        Else
            Return stringvalue.Remove(pos, stringremove.Length + 1)
        End If

    End If
    Return stringvalue
End Function

结束班

可以将任何正文显示在chekbox10之后,checkbox11,checkbox12在文本框中显示为10,11,12 ......所以使用此代码

1 个答案:

答案 0 :(得分:2)

我真的不明白这个要求,但我认为该函数应该做的唯一事情就是在TextBox中打印所有选中的CheckBox的ID。

为什么复杂化,这也有效:

TextBox1.Text = String.Empty
For Each control As Control In form1.Controls
    If TypeOf control Is CheckBox AndAlso DirectCast(control, CheckBox).Checked Then
        TextBox1.Text &= control.ID & ","
    End If
Next
'remove last comma'
If TextBox1.Text.Length <> 0 Then TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 1)

根据您关于订单的新信息,试试这个:

    Protected Sub CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim chk As CheckBox = DirectCast(sender, CheckBox)
        Dim separator As Char = ","c
        If TextBox1.Text.Length <> 0 Then
            Dim allIIDs As New List(Of String)(TextBox1.Text.Split(separator))
            allIIDs.Remove(chk.ID)
            If chk.Checked Then
                allIIDs.Add(chk.ID)
            End If
            TextBox1.Text = String.Empty
            For Each id As String In allIIDs
                TextBox1.Text &= id & separator
            Next
            TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 1)
        Else
            TextBox1.Text = chk.ID
        End If
    End Sub

要在CheckBoxes上注册CheckedChanged-Event,您必须在每个Checkbox的aspx上添加以下内容:

<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckedChanged" />

或者如果你很懒并想在Codebehind中这样做,请在Page_Init中添加以下内容:

  Private Sub WebForm1_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        For Each control As Control In Me.form1.Controls
            If TypeOf control Is CheckBox Then
                Dim chk As CheckBox = DirectCast(control, CheckBox)
                chk.AutoPostBack = True
                AddHandler chk.CheckedChanged, AddressOf CheckedChanged
            End If
        Next
    End Sub