Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_professional thread: Newbie - Retrieving asp:checkboxlist values


Message #1 by jlc1@c... on Mon, 5 Nov 2001 20:33:47
Doh, nevermind it was a stupid error:

Rather than this:
s = s & Check1.Items[i].Text & "<br>"

Like trying to access an array, it should be this:
s = s & Check1.Items(i).Text & "<br>"

Everything works fine now (whew).  Thanks

-Jer


> 
> What is happeneing is that you are using a Button to postback the page.
> The onClick event for this button is set to Button1_OnClick.
> 
> So what happens is that you select the information that you want on your
> checkbox list, then you click the button.
> 
> This calls the OnClick event handler.
> In the parameters for the function you have 'objSender As Object'.
> This takes the Object which has forced the event handler into action.
> In this case that is the button which you have clicked.
> 
> So following this through....
> 
> As you get the start of your For Each loop you use :-
> 
> For Each objItem in objSender.Items
> 
> This is trying to access each Item in the Button.  A button does not 
have
> any items.  What you need to do is replace objSender.Items with 
nameofCheckBoxList.Items.
> This will let your code access the CheckBoxList and its Items.
> 
> Aiden Montgomery.
> 
> (Sorry about the double post. One bad typo that one :-) )
> 
> -----Original Message-----
> From: jlc1@c... [mailto:jlc1@c...]
> Sent: 05 November 2001 20:34
> To: ASPX_Professional
> Subject: [aspx_professional] Newbie - Retrieving asp:checkboxlist values
> 
> 
> Ok, i've run into an error that has me completely dumbfounded, here's 
the 
> situation.  I'm using data from a sql server to dynamically generate 
> checkboxes (asp:checkboxlist), however when I go to retrieve the data I 
> run into a problem.  First I tried something like this:
> 
> 
> Sub Button1_OnClick(sender As Object, e As EventArgs)
>    Dim s As String
>    Dim i As Integer
>    s = "Selected items:<br>"
>    For i=0 to Check1.Items.Count - 1
>       If Check1.Items(i).Selected Then
>          s = s & Check1.Items[i].Text & "<br>"
>       End If 
>    Next i
>    Label1.Text = s
> End Sub
> 
> Nada, i just get an error that the info cannot be stored into a 
string.  
> So I turned to my trusty asp.net pro book from wrox, and found a 
> wonderful example.
> 
> Sub MyCode(objSender As Object, objArgs As EventArgs)
>   Dim strResult As String
>   strResult = "The following items were selected:<br/>"
>   Dim objItem As ListItem
>   
>   For Each objItem in objSender.Items
>     If objItem.Selected Then
>       strResult += objItem.Text & " = " & objItem.Value & "<br/>"
>     End If
>   Next
> 
>   outMessage.InnerHtml = strResult
> End Sub
> 
> The above example is found on page 265 of "Professional ASP.NET".
> 
> I went to try this example, and now i get the following error:
> 
> Method System.Web.UI.WebControls.Button.Items not found.
> 
> Description: An unhandled exception occurred during the execution of 
the 
> current web request. Please review the stack trace for more information 
> about the error and where it originated in the code. 
> 
> Exception Details: System.MissingMethodException: Method 
> System.Web.UI.WebControls.Button.Items not found.
> 
> Any idea what I'm doing wrong here?  I'm still slowly learning this 
> stuff, but this one is really starting to bug me.  Can i not use an 
> asp:button in this circumstance?
> 
> Thanks for any help you can offer.
> 
> -Jeremy
> ---
> VBug Winter Conference 2001 
> 
> Make no mistake - there's a great learning curve 
> for developers moving from COM to .NET.  So why 
> would you want to be a pioneer and cross the bridges 
> first?  Attend the .NET Developer's Conference 
> (28th to 30th November 2001) In London and realise 
> the benefits of adopting the technology early.  By 
> attending you'll understand the key challenges and 
> be left with a thorough understanding of the major 
> .NET fundamentals.   If you're already working 
> with .NET this is a must-attend event.  
> 
> http://www.vbug.co.uk/redirect.asp?url=39&id=17
> 
> ---
> You are currently subscribed to 
> aspx_professional as: aiden.montgomery@f...
> $subst('Email.Unsub')
> 
> 

  Return to Index