我如何检索一个文本框的值的逗号分隔的字符串? [英] How do I retrieve a comma-delimited string of values from a text box?

查看:381
本文介绍了我如何检索一个文本框的值的逗号分隔的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框和3复选框; checkbox1,checkbox2,checkbox3

I have a textbox AND 3 checkboxes; checkbox1, checkbox2, checkbox3

在文本框,我们有1,2,3

In textbox we have 1,2,3

如果我在TextBox1中键入= 1,2,然后

If I type in textbox1 = 1,2 then

checkbox1和checkbox2将被选中并checkbox3仍将处于未选中.....

checkbox1 and checkbox2 will be checked and checkbox3 will remain unchecked.....

如何做到这一点在vb.net

How to do this in vb.net

推荐答案

这样的事情应该制定出适合你:

Something like this should work out for you:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    For Each s As String In TextBox1.Text.Split(","c)
        Dim i As Integer
        If Int32.TryParse(s, i) Then
            Dim result As Control = Page.FindControl("checkBox" + i.ToString())
            If result IsNot Nothing Then
                DirectCast(result, CheckBox).Checked = True
            End If
        End If
    Next
End Sub

如果您的复选框是不同的容器,​​而不是主要的页面中,您需要调用的FindControl 上的控制,而不是 Page.FindControl`。

If your checkboxes are within a different container instead of the main Page you'll need to call FindControlon that control instead ofPage.FindControl`.

编辑:这听起来像你从复选框选择要建立在文本框的值。基于您的评论也看起来你有一个结尾逗号,也许是因为要追加在一个循环中一个逗号。你应该向我们提供code,以更好地了解你在做什么。

it sounds like you're going from checkbox selections to build up the textbox value. Based on your comment it also looks like you have a trailing comma, perhaps because you're appending a comma in a loop. You should provide us with code to better understand what you're doing.

要回答您的评论的问题,要按3,1,你可以使用这样的:

To answer the question in your comment, to sort "3,1," you could use this:

Dim input As String = "3,1,"
Dim split As String() = input.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
Dim result As String = String.Join(",", split.OrderBy(Function(i) i).ToArray())

在现实中,你可能想通过一个整数类型命令,而不是字符串,因为排序顺序最终将令你感到惊讶的更大的数字。

In reality you might want to order by an Integer type, not by strings, since the sorting order will eventually surprise you for larger numbers.

这篇关于我如何检索一个文本框的值的逗号分隔的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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