Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PostBacks and Click Events 1

Status
Not open for further replies.

ChasBoots

MIS
Jul 23, 2002
24
US
My web page has 3 list boxes a 4 buttons. Let me present some code first:

Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            lb_FieldNames.Items.Clear()
            lb_XFieldNames.Items.Clear()
            lb_YFieldNames.Items.Clear()
            lb_YFieldNames.SelectedIndex = -1
            lb_XFieldNames.AutoPostBack = True
            lb_YFieldNames.AutoPostBack = True

            If lb_FieldNames.Items.Count = 0 Then
                GetColumnNames(getdataset())
            End If

        End If
        Trace.Write("X Count: " & tb_xitem.Text)
        Trace.Write("Y Count: " & tb_yitem.Text)
        If (lb_XFieldNames.Items.Count > 0) And (lb_YFieldNames.Items.Count > 0) Then
            imgb_GraphIt.Visible = True
        Else : imgb_GraphIt.Visible = False
        End If
        Page.DataBind()
    End Sub

Listbox lb_FieldNames is populated through the procedure call "GetColumnNames(getdataset())". When an item in this listbox is selected, the user will click one of the 2 "Add" buttons to move to either lb_XFieldNames or lb_YFieldNames. Or, conversely, if one of the "Delete" buttons is clicked, the field is removed and re-added to lb_FieldNames.

When the item counts of these other 2 listboxes is >0, I want to change the visibility status of an image button ("imgb_GraphIt.Visible"). If the item counts go to zero as a result of deleting fields, the image button is to be set to not visible.

Everything with respect to moving items back and forth between listboxes works perfectly. Unfortunately, I cannot find a way to get the postback to execute after clicking any of the buttons. If both of the secondary listboxes are populated with one item each, the image button will not appear until after I select another item (thus raising the next postback event).

For informational purposes, the code for one of the buttons is as follows:

Code:
    Private Sub btn_Xadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Xadd.Click
        If (lb_XFieldNames.Items.Count = 0) And (lb_FieldNames.SelectedIndex <> -1) Then
            lb_XFieldNames.Items.Add(lb_FieldNames.SelectedItem)
            lb_XFieldNames.SelectedItem.Selected = False
            lb_FieldNames.Items.Remove(lb_XFieldNames.Items.Item(0).Value)
            SortListBox(lb_FieldNames)
        End If

The SortLisBox() procedure works as it should. I have even removed it from the event procedure to prove it to be ineffectual in my problem. I am guessing this might take only 1 or 2 lines of code to resolve and I have every confidence that someone here will have the answer. Thanks.
 
You have this code inside of the If Not IsPostBack:
Code:
If (lb_XFieldNames.Items.Count > 0) And (lb_YFieldNames.Items.Count > 0) Then
   imgb_GraphIt.Visible = True
Else : imgb_GraphIt.Visible = False

This will only work the first time the page loads, i.e. NOT a post back.

Move this outside of the IF and is should work as you want.

Jim
 
Hi,

You should check the listbox content and toggle imgb_GraphIt visibility in the SortListBox events.

At the moment this check is being done on page_load when the page is not posted back,

So move

If (lb_XFieldNames.Items.Count > 0) And (lb_YFieldNames.Items.Count > 0) Then
imgb_GraphIt.Visible = True
Else : imgb_GraphIt.Visible = False
End If

Into the SortListBox event

hth
j

 
Thanks hth, that did the trick. I can't believe I couldn't see that solution for myself. Now onto other heaaches... [thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top