define checkboxes as checked in checkboxlist - asp.net

I can't figure out how to set the "checked" value of certain checkboxes in a checkboxlist during page load. So far I have this, but it doesn't work.
For Each DataRow As DataRow In groupDataset().Tables(0).Rows
Dim i As Integer
For i = 0 To cblGroups.Items.Count - 1
Response.Write(cblGroups.DataValueField)
If DataRow("memberID").ToString = cblGroups.DataValueField Then
cblGroups.Items(i).Selected = True
End If
Next
Next
Is this even possible?
Thanks

Try this. You need to look at each item in the checkboxlist, not the datavaluefield of the group itself.
For Each DataRow As DataRow In groupDataset().Tables(0).Rows
Dim i As Integer
For i = 0 To cblGroups.Items.Count - 1
Response.Write(cblGroups.Items(i).Value)
If DataRow("memberID").ToString = cblGroups.Items(i).Value Then
cblGroups.Items(i).Selected = True
End If
Next
Next

Related

If checkbox Checked then add to datatable from Gridview

I am trying check whether a checkbox is checked in a gridview and if it is checked to add it to the datatable.
However I am getting an error when the checkbox is unchecked for the row:
There is no row at position 1.
Here is my code:
'Creates a new datatable
Dim dtQuestions As New DataTable("QuestionsData")
'Add columns to datatable
For Each cell As TableCell In example.HeaderRow.Cells
dtQuestions.Columns.Add(cell.Text)
Next
For Each row As GridViewRow In example.Rows
Dim chkTest As CheckBox = CType(row.FindControl("chkTest"), CheckBox)
If chkTest.Checked = True Then
dtQuestions.Rows.Add()
For i As Integer = 0 To row.Cells.Count - 1
Try
dtQuestions.Rows(row.RowIndex)(i) = row.Cells(i).Text
Catch ex As Exception
End Try
Next
Else
'Do not add it to Datatable
End If
Next
I am getting the error on this code:
dtQuestions.Rows(row.RowIndex)(i) = row.Cells(i).Text
I do not know how to fix this.
If you want to add a row to a DataTable you should write this code
For Each row As GridViewRow In example.Rows
Dim chkTest As CheckBox = CType(row.FindControl("chkTest"), CheckBox)
If chkTest.Checked = True Then
Dim tableRow = dtQuestions.NewRow()
For i As Integer = 0 To row.Cells.Count - 1
tableRow(i) = row.Cells(i).Text
Next
dtQuestions.Rows.Add(tableRow)
End If
Next
Notice that I have removed the empty try/catch. Don't do it because it just hides your problems.

Adding Items From Listbox to List

A simple, rather open ended question. I am wanting to add all items in a Listbox to an object, to later print that those items into a database. Is the best practice for adding all items in a Listbox to an array?
Thanks
Since you've commented that you are using ASP.NET, you can use a loop:
Dim allListBoxItemsText = New List(Of String)
For Each item As ListItem In listBox1.Items
allListBoxItemsText.Add(item.Text)
Next
or LINQ:
Dim items = From item In listBox1.Items.Cast(Of ListItem)()
Select item.Text
allListBoxItemsText = items.ToList() ' if you want an array use ToArray
Id use the standard declare an array and loop through it. I am sure there is a more elegant solution but it works.
Dim array(listbox1.items.count-1) As String
Dim i as integer = 0
For i = 0 to listbox1.items.count -1
array(i) = listbox1.items(i)
Next

How to compare row values of two gridviews with dropdownlist column in asp.net?

I have two gridviews. The first one has a dropdownlist. When a user clicks a button named 'Show' data will be displayed on both gridviews where data comes from database. The row data on column with dropdownlist must be compared to the rows on the first column of the second gridview. If they are equal a messagebox will prompt saying that there's no changes and data will not be saved, else if they are not equal modal pop-up will be displayed asking if data are correct.
Below is my code for comparing but it only reads the value of the 1st row in gridview1.
For i = 0 To GridView1.Rows.Count - 1
Dim ddl As DropDownList = DirectCast(GridView1.Rows(i).Cells(6).FindControl("dropdowncriteria"), DropDownList)
Dim txt As TextBox = DirectCast(GridView1.Rows(i).Cells(7).FindControl("txtreason"), TextBox)
If ddl.SelectedValue = GridView2.Rows(i).Cells(0).Text And txt.Text = GridView2.Rows(i).Cells(1).Text Then
MessageBox("No Changes Made! Nothing will be Saved.")
Return
Else
lblmsg.Text = "Are you sure that all the Data you've Selected/Entered are Correct?"
mdlpopupmsg.Show()
Return
End If
Next
What must be the problem on this?
Thanks in advance.
It only reads the first value (i=0) because the return statements cause the for loop to exit after the first comparison. If you want to compare all the rows you will need a variable to keep track of the result of the if test for each row. Something like this:
Dim hasChanges As Boolean = False
For i = 0 To GridView1.Rows.Count - 1
...
If ddl.SelectedValue = GridView2.Rows(i).Cells(0).Text And txt.Text = GridView2.Rows(i).Cells(1).Text Then
'do nothing
Else
hasChanges = True
End If
Next
If hasChanges Then
MessageBox("Has changes.")
Else
MessageBox("No changes.")
End If
Dim itemt As Double
If (DataGridCart.RowCount() > 0) Then
For i = 0 To DataGridCart.Rows.Count - 1
'if itemt as double
itemt = Val(Trim(txtItem.Text))
If ((DataGridCart.Rows(i).Cells("Item").Value).Equals(itemt)) Then
MsgBox("existing entry")
End If
Next
End If

VB .Net and asp .net adding each item from dropdownlist to label not working

I am trying to add each item from dropdownlist to label. Any guesses why it won't work. Thanks for your answers.
Dim labels(2) As Label
Public Sub AddItemsFromDropdownlistToLabel()
DefineLabels()
'Add Items From Dropdownlist1 to three lables
For Each item As Object In DropDownList1.Items
If LabelCount < 3 AndAlso LabelCount > 0 Then
labels(LabelCount).Text = item.ToString
End If
Next
End Sub
Public Sub DefineLabels()
labels(0) = label1
labels(1) = label2
labels(2) = label3
End Sub
Couple of problems
1: Since you're checking LabelCount>0 labels(0) will never be populated.
2: You've not got anything incrementing for each item in the loop to advance the population of the labels array.
3: Each item in your drop down is an instance of ListItem. To get a anything useful out of that, you're best to use either item.Text or item.Value.
Looks like LabelCount has a value of 0 and is not changing in the for each.
You could use instead:
For i as Integer = 0 to DropDownList1.Items.Count
If i >= 3 Then Exit For
labels(i).Text = DropDownList1.Items(i).ToString()
Next
I suppose DropDownList1 is a ComboBox or similar.

How to get my dropdown to default to first row name in table

Edit: more code
Dim oControl As New Forms.Control()
Using types As New DataSet
With oDal
.Parameters.Clear()
.Execute(sql, types)
End With
With ddlType
.DataSource = types.Tables(0)
.DataTextField = "Name"
.DataValueField = "TypeId"
.Items.Clear()
.DataBind()
If .Items.Count > 0 Then
.SelectedIndex = 0
End If
End With
End Using
'set queue type with our asp:dropdownlist control
oControl = New Forms.Control(Forms.Control.ControlType.ComboBox)
'we need to connect the two controls
oControl.Id = ddlType.ID
'add it to the grid
With .Columns.Add("CredentialTypeId", "Type", 85)
.Editor = oControl
End With
For some reason when you click the dropdown - "0" is displayed defaulted. It needs to be the first text/value pair from the table. The table is only returning one row at the moment, which is correct, but 0 still defaults.
Anyone know why?
With ddlType
.DataSource = types.Tables(0)
.DataTextField = "Name"
.DataValueField = "TypeId"
.DataBind()
If .Items.Count > 0 Then
.SelectedIndex = 0
End If
End With
Edit:
Given your results and your comments, I think either your types table is empty or your code isn't being run. Try adding some breakpoints and testing some values.
types.Tables(0) isnot nothing
types.Tables(0).Rows.Count
Edit2:
Looking at your code, I see a different problem. You never fill oControl. I am not sure why you are creating a completely new control and setting it's ID to ddlType's ID. Why don't you just do your databind with oControl? Or attach ddlType to your grid?
I know this probably won't work but going to say it anyway.
Have you tried to clear all the items before binding the data?
ddType.Items.Clear();

Resources