0

Hi,

I have set the content of a listbox through databinding.

myListBox.DataSource = myDataSource
myListBox.DataTextField = "column01"
myListBox.DataValueField = "column02"
myListBox.DataBind()

I have then set the selected index of the listbox using a loop

Dim myValue As Integer = [I]someValue[/I]
Dim myListItem As ListItem = New ListItem
For Each myListItem In myListBox.Items
If myListItem.Value = myValue Then
myListItem.Selected = True
End If
Next

It seems a little strange that you can so easily define a datasource but have to resort to a loop to define the selected index.

Is there no way to define the selected index when the datasource is bound? What if I have multple selections to define, would I have to expand my loop to set these?

Thanks,

Fido.

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by __avd
0

Friend I am Giving u a code please go through following ...

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.WebControls
Imports System.Web.UI.Control

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim a As New SqlConnection()
    Dim cmd As New SqlCommand()
  
    Dim uid1 As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        a.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True"

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        
        Dim atLeastOneRowDeleted As Boolean = False
        For Each row As GridViewRow In GridView1.Rows
            Dim cb As CheckBox = row.FindControl("chb1")
            If cb IsNot Nothing AndAlso cb.Checked Then
                atLeastOneRowDeleted = True
                Dim ID As String = Convert.ToString(GridView1.DataKeys(row.RowIndex).Value)
                cmd.CommandText = "delete from log where id = '" & ID & "'"
                cmd.ExecuteNonQuery()

            End If
        Next
        a.Close()
        GridView1.DataBind()
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

        For Each row As GridViewRow In GridView1.Rows
            Dim cb As CheckBox = row.FindControl("chb1")
            If cb IsNot Nothing Then
                cb.Checked = True
            End If
        Next
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        For Each row As GridViewRow In GridView1.Rows
            Dim cb As CheckBox = row.FindControl("chb1")
            If cb IsNot Nothing AndAlso cb.Checked Then
                cb.Checked = False
            End If
        Next
    End Sub

End Class

I think this code will be able yo solve your problem....

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

0

>What if I have multple selections to define, would I have to expand my loop to set these?

No methods to bind ListBox with multiselect options. You have to use loop.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.