这code将显示文本选定复选框文本1,2,3,4,5,6,7,8,9 [英] This code will display selected checkboxes text in textbox as 1,2,3,4,5,6,7,8,9

查看:181
本文介绍了这code将显示文本选定复选框文本1,2,3,4,5,6,7,8,9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code将显示文本框中选择复选框文字作为我检查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

但它不会经过9中显示的文本框选定复选框文本

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

部分类_45seater_WebUserControl
    继承System.Web.UI.UserControl

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
                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后......所以使用该code

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

推荐答案

我真的不明白的要求,但我认为功能应该做的唯一一件事就是打印ID的所有检查在TextBox的复选框。

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

要注册<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.checkedchanged.aspx\"相对=nofollow>的CheckedChanged-事件上的复选框你必须添加以下的ASPX每一个复选框:

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" />

或者如果你是懒惰,要做到这一点在codebehind,加入Page_Init以下内容:

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

这篇关于这code将显示文本选定复选框文本1,2,3,4,5,6,7,8,9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆