[.net] button array question

Started by
6 comments, last by ernow 18 years, 11 months ago
hi, im new to this forum so spare me :) hehe Im trying to create a button array in vb.net (if it is possible) .. im trying to generate 5 buttons automatically.. but its not initialising for me for some reason.. the code is quite simple.. Dim buttonarray(5) As Button For i = 0 To 4 buttonarray(i).Text = "Bleh" buttonarray(i).ID = i buttonarray(i).CommandName = "bleh" + i Controls.Add(buttonarray(i)) Next i Few questions: 1. Is this a valid code? The editor seems not to mind... not giving me any expression errors.. 2. Is button array possible? 3. Following error occures: Object reference not set to an instance of an object. 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.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 36: Line 37: For i = 0 To 4 Line 38: buttonarray(i).Text = "Bleh" <---- at this particular line Line 39: buttonarray(i).ID = i Line 40: buttonarray(i).CommandName = "bleh" + i Thats about it.. if anyone else has any different suggestions.. i'd appreciate it. :) Cheers
Advertisement
> 1. Is this a valid code? The editor seems not to mind... not giving me any expression errors..

Almost...

> 2. Is button array possible?

Yes! But not the way you used to in VB6.

> 3. Following error occures:


Fix it like this:

Dim buttonarray(5) As ButtonFor i = 0 To 4  ' Fix:  buttonarray(i) = New Button();  buttonarray(i).Text = "Bleh"  buttonarray(i).ID = i  buttonarray(i).CommandName = "bleh" + i  ' Optional:  AddHandler buttonarray(i), AddressOf Me.buttonarray_Clicked  Controls.Add(buttonarray(i))Next i


And don't forget to set the position either...

Cheers
seems to be giving me same error :(

Control '0' of type 'Button' must be placed inside a form tag with runat=server.
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.Web.HttpException: Control '0' of type 'Button' must be placed inside a form tag with runat=server.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
ok fixed it...

I had to use webusercontrol :)

thank you very much.. also the AddHandler wasnt working properly but i fixed it

thanx heaps :)
Next time mention you're building webpages...

(Note to self: don't assume anything, ask...)

Cheers
yeah sorry..

1 more question... I cant get the event handler to work.. it wont recognise the

Me.buttonarray_Click ... :/

im trying to get each one of the buttons to do separate thing..

how can i extract the event index of which button is being called.. or perhaps the best method..

cheers
The code seems to be correct but its not passing down arguments to the handler

Dim i As Integer
For i = 0 To 4
' Fix:
buttonarray(i) = New Button
buttonarray(i).Text = "Bleh"
buttonarray(i).ID = i
buttonarray(i).CommandName = "bleh" & i
' Optional:
AddHandler buttonarray(i).Click, AddressOf ButtonClickHandler
Controls.Add(buttonarray(i))
Next i

Public Sub ButtonClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
If buttonarray(0).CommandName Is "bleh0" Then

Response.Redirect("example.html")
End If
' Implementation code omitted.
End Sub
I think you should do something like this:

Public Sub ButtonClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)  If sender Is Button Then    Dim b as Button    b = CType(sender, Button)    If b.CommandName == "bleh0" Then      Response.Redirect("example.html")    End If  End If  ' Implementation code omitted.End Sub


Cheers

This topic is closed to new replies.

Advertisement