If in textbox the default textbox value id 1,2,3,4,5,6 ...... upto开发者_StackOverflow 55
then the following checkboxes would be checked according to the text display in text box...
if textbox1.text =1,2,3 then in my webform checkbox1, checkbox2, checkbox3 would be checked ... on page load event...
how to do this ?
Dim splitted as String() = MyTextBox.Text.Split(",") For Each id As String in splitted Dim ctrl as Control = Page.FindControl("checkbox" & id) If Not control Is Nothing Then Dim chkbox As CheckBox = DirectCast(ctrl, CheckBox) chkbox.Checked = True End If Next
I'm actually a C# programmer, so not 100% if the VB.NET syntax is correct. Another NB! is that this sample only works if the checkboxes are directly in your ASP.NET page. If they're ie. inside an ASP:Panel, then you'll have to use "MyPanel.FindControl" istead of Page.FindControl
55 checkboxes? You could have lots of if...else to check each number, but I would create a collection of checkboxes. Then parse the number in the text box check it is in range and then simply look up correct checkbox to check according using the value as an index.
Another thought: It sounds like one and only one checkbox should be set at a time? If so, you should replace them with a group of radio buttons. As well as being easier to code, it avoids duplicate checkbox checks, and signals to the user that only one can be set.
精彩评论