开发者

This code will display selected checkboxes text in textbox as 1,2,3,4,5,6,7,8,9

This code will display selected checkboxes Text in textbox as in the order i checked it 1,2,3,4,5,6,7,8,9

But it will not display selected checkboxes Text in textbox after 9

Partial Class _45seater_WebUserControl Inherits 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
       开发者_如何学JAVA         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

End Class

can any body show after chekbox10,checkbox11,checkbox12 in text box as 10,11,12 ......so on using this code


I really don't understand that requirement but i think the only thing the function should do is to print the ID's of all checked CheckBoxes in a TextBox.

Why do you complicated it, this works too:

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)

According to your new informations about the order, try this:

    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

To register the CheckedChanged-Event on the CheckBoxes you have to add following on aspx for every Checkbox:

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

or if you are lazy and want to do that in Codebehind, add following in 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜