I need some assistance with disabling web control button. First, i need to check if the value related to the button is in the listbox. This is done.

If the item is in the listbox, disable the button. I am thinking the possibility of using a loop statement. However, i do not know how to write it to check if the item related to the button matched before disabling button.

Dani AI

Generated

As described, the goal is to disable server-side seat buttons when their seat ID appears in listbox_seatsbooked. rightly asked whether this is client- or server-side; since it is server-side, the straightforward pattern is:

  • Put a stable seat identifier on each Button (use CommandArgument or a custom server attribute — ASP.NET Button does not expose a WinForms-style Tag). 's idea to attach the seat id is correct in spirit; for ASP.NET use CommandArgument or Attributes("data-seat").
  • After the ListBox is populated (for example in Page_PreRender or immediately after binding), loop the seat Buttons, compare each Button's identifier to the ListBox items, and set Enabled = False for matches.

Example markup and VB.NET pattern (note: run this after the ListBox is bound):

<asp:Button ID="btnA1" runat="server" Text="A1" CommandArgument="A1" />
' Run after listbox_seatsbooked has data
For Each c As Control In SeatsPanel.Controls
    Dim btn As Button = TryCast(c, Button)
    If btn IsNot Nothing Then
        Dim seatId As String = btn.CommandArgument
        Dim taken As Boolean = False
        For Each li As ListItem In listbox_seatsbooked.Items
            If String.Compare(li.Text.Trim(), seatId, True) = 0 Then
                taken = True
                Exit For
            End If
        Next
        btn.Enabled = Not taken
    End If
Next

Troubleshooting notes: ensure the ListBox strings exactly match the seat IDs (trim and normalize case), run the loop after data binding, and iterate the correct container (Panel, Repeater item, etc.). If Buttons are created dynamically they must be recreated on every postback before this check runs. Finally, disabling buttons is only UI-level protection — always re-validate availability on the booking action (database check/transaction or unique constraint) to prevent race conditions and double-booking.

Recommended Answers

All 5 Replies

Are you doing this client-side, or server-side?

What do you mean by "disable"? Show, but have no effect when clicked? Or, remove the button completely?

I have difficulty understanding your last sentence. Could you rephrase the question?

Server Side.

I am doing a movie booking page and the seats are represented by web control buttons. They are to be disabled when the seats booked values are listed in a listbox known as listbox_seatsbooked.

From the listbox_seatsbooked, it is suppose to check if the value of the button matches the ones listed in the listbox to disable the button from being clicked again. (Seats can't be booked) again.

I have included an image of how it looks for better clarifications.
[IMG][/IMG]

' Disable the Add button if the ListBox control already contains the item.

Dim name as string
Dim item As ListItem = listbox_seatsbooked.Items.FindByText(name)
If Not item Is Nothing Then

a1.Enabled = False
Else
a1.Enabled = True

End If

I tried writing this. However, its not executing.

you made a small error... you didnt set 'name' to anything.

Dim name as string
Dim item As ListItem = listbox_seatsbooked.Items.FindByText(name)
If Not item Is Nothing Then

need to be

Dim name as string = thebutton.tag  
'that is assuming you set the tag to be the seat number such as A1 on btna1, A2 on btna2 etc. 
Dim item As ListItem = listbox_seatsbooked.Items.FindByText(name)
If Not item Is Nothing Then

Hi f1_fan, thanks for ur assistance.

I cannot find a declaration known as .tag in asp.net (codebehind vb.net). I tried using C# converter to VB.net.

Any suggestions?

you made a small error... you didnt set 'name' to anything.

need to be

Dim name as string = thebutton.tag  
'that is assuming you set the tag to be the seat number such as A1 on btna1, A2 on btna2 etc. 
Dim item As ListItem = listbox_seatsbooked.Items.FindByText(name)
If Not item Is Nothing Then
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.