Results 1 to 4 of 4

Thread: check box lst problem - help ? - resolved

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    46

    check box lst problem - help ? - resolved

    hi

    i have a check box list in page and a button when i check some items and submit by button i am not able to read the selected items or checked items status at server. is there a way to it or i am doing it wrong this is the code

    sub buton1_click(sender as object, e as eventargs)
    Dim i As Integer
    For i=0 To checkboxlist1.Items.Count - 1
    If checkboxlist1.Items(i).Selected Then
    response.write( checkboxlist1.Items(i).Text + "<br>")
    End If
    Next
    end sub

    thanks in advance --
    Last edited by kanakaprasad; Oct 27th, 2003 at 08:51 AM.
    Kanaka Prasad

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    VB Code:
    1. <%@ Import Namespace="System.Collections" %>
    2. <script language="vb" runat="server">
    3. Protected Sub btnTest_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    4.     For index As Integer = 0 To cblTest.Items.Count - 1
    5.         If cblTest.Items(index).Selected Then
    6.             Response.Write(cblTest.Items(index).Text)
    7.             Response.Write(" is selected.<br/>")
    8.         End If
    9.     Next
    10. End Sub
    11. Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    12.     If Not Page.IsPostBack Then
    13.         bindCheckBoxList
    14.     End If
    15. End Sub
    16. Private Sub bindCheckBoxList()
    17.     Dim items As New ArrayList()
    18.     items.Add("Item 1")
    19.     items.Add("Item 2")
    20.     items.Add("Item 3")
    21.     cblTest.DataSource = items
    22.     cblTest.DataBind
    23. End Sub
    24. </script>
    25. <html>
    26.     <body>
    27.         <form runat="server">
    28.             <asp:CheckBoxList ID="cblTest" Runat="server"/><br/>
    29.             <asp:Button ID="btnTest" Runat="server" Text="Test"
    30.                 OnClick="btnTest_Click"/>
    31.         </form>
    32.     </body>
    33. </html>
    The only way this didn't work is if, in the OnLoad Sub, I don't check for Page.IsPostBack. The OnLoad method or Page_Load eventhandler, will fire before the the button click event is processed. So perhaps you are rebinding on postback and wiping out anything you selected before posting; then in the button click event nothing would be selected.
    Last edited by pvb; Oct 25th, 2003 at 12:26 PM.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    46
    i think thats what i am doing wrong. i am not checking for the ispostback. thanks alot i shall work onit and let you know. thanks once again

    kanaka
    Kanaka Prasad

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    46

    resolved - thanks

    the problem is solved thanks folks

    kanak a
    Kanaka Prasad

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width