Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use the checkbox selection in the web form 1

Status
Not open for further replies.

taree

Technical User
May 31, 2008
316
US
I have three checkboxes for the user to selec the vendor type. I am using this to collect info. using web form email.my problem is when user selct two out of three I have one extre comma and I do not know how to fix that :) please help.

Code:
.AppendLine(String.Format("<i><b> {0} </b> </i>", "Vendor Type :"))

                If ckPrime.Checked Then
                    .AppendLine(String.Format("<i> {0} </i>", ckPrime.Text) & "</br> , ")
                End If

                If ckSub.Checked Then
                    .AppendLine(String.Format("<i> {0} </i>", ckSub.Text) & "</br> ,")
                End If

                If ckTruking.Checked Then
                    .AppendLine(String.Format("<i> {0} </i>", ckTruking.Text) & "</br> ")
                End If

                .AppendLine(String.Format("<div><i>DUN Number : {0} </i></div>", txtDun.Text) & "</br>")
                .AppendLine(String.Format("<div><i>Federal Tax Id Number : {0} </i></div>", txtFedNumber.Text) & "</br>")[code]
 
There are a couple of ways to do this. The simpilist way would be to use string.replace()

Look at the overloaded method calls. You can specify it to find the last "," in the string and strip it out.
 
thank you jbenson001 for your reply. I just can not think how to get the last comma even if I use string.replace method to replace the last comma.can you give me more clues how I can get the last comma. I am just thinking now if I count the lenght of the character and minus one. Will this work for me :)
 
Code:
var selectedOptions = new []{ckPrime, ckSub, ckTruking}
   .Where(checkbox => checkbox.Checked)
   .Select(checkbox => checkbox.Text)
   .ToArray();
var formattedOptions  = string.Concat("<i>", string.Join("</i>, <i>", selectedOptions), "</i>");
var message = string.Format(@"<p><i><b>Vendor Type:</b></i> {0}</p><p><i>DUN Number : {1}</i></p><p><i>Federal Tax Id Number : {2} </i></p>", formattedOptions, txtDun.Text, txtFedNumber.Text);

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Jason the code you provided me is that LINQ ? I thank you for that but when I add to my page I am getting an error message.
 
yes, it's using linq extensions and 3.0 syntax. you can do the same using List<T> instead of linq. the code I provided is C#, you will need to convert to VB.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Jason,Am I on the right track here...

Code:
  Dim selectedOptions As New List(Of String)
                selectedOptions.Add("ckPrime")
                selectedOptions.Add("ckSub")
                selectedOptions.Add("ckTruking")

                Dim i As Integer

                For i = 0 To selectedOptions.Count - 1
                    If selectedOptions.Item(i) Then

                    End If

                Next
 
you are adding string to a list.
I am adding checkboxes, filtering out the unchecked and selecting there text value.

you can iterate with a for loop, but the foreach is cleaner IMO.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I still do not know how to check whether the checkbox is checked or not. Please take a look at my code and point me to the right direction.

Code:
 Dim selectedOptions As New List(Of Object)
                selectedOptions.Add(ckPrime)
                selectedOptions.Add(ckSub)
                selectedOptions.Add(ckTruking)


                Dim i As Integer
                Dim valStr As String = ""
                For i = 0 To selectedOptions.Count - 1
                    'If selectedOptions.Item(i) Then Then
                    valStr = valStr & selectedOptions.Item(i).text & ","
                    ' End If

                Next
                Dim valStr1 As String = Len(valStr) - 1
                Dim valstr2 As String = Len(valStr)
                valStr = Left(valStr, Len(valStr) - 1) & ","
 
Thank you all for your help. I think I got it to work.

Dim selectedOptions As New List(Of Object)
selectedOptions.Add(ckPrime)
selectedOptions.Add(ckSub)
selectedOptions.Add(ckTruking)


Dim i As Integer
Dim valStr As String = ""
For i = 0 To selectedOptions.Count - 1
If selectedOptions.Item(i).checked = True Then
valStr = valStr & selectedOptions.Item(i).text & ","
End If

Next
valStr = Left(valStr, Len(valStr) - 1)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top