ASP.Net (VB) DataList Control odd behavior ?

Discussion in 'ASP .Net' started by AndrewR, Sep 14, 2005.

  1. AndrewR

    AndrewR Guest

    Hey all,

    Have a strange one here I don't understand....

    I have a DataList control in an .aspx page. Inside the DataList's
    <ItemTemplate> is a single LinkButton control. On the initial page load-up
    I bind the DataList, and set the LinkButton's text and CommandArgument
    values to the two items in the result set using the <%# ... %> tags. The
    LinkButton's OnClick handler is set to a sub call ShowScores. When the page
    is run, I get on screen a nice list of links, and clicking any of them runs
    the back side code ShowScores, which was my goal. No problem....but I can't
    leave well enough alone.....

    I thought that if I could disable the link that was just clicked, it would
    make for a better interface...no double clicking the same link, visual cue
    as to what was clicked, etc. The sub ShowScores has a signature as follows:

    Protected Sub ShowScores(ByVal sender As System.Object, ByVal e As
    System.EventArgs)

    To disable the LinkButton control that was clicked, I simply added this in
    the sub block:

    Dim LButton As LinkButton = DirectCast(sender, LinkButton)
    LButton.Enabled = False

    Which worked, but when a second link was clicked, the first one was still
    disabled. Ok, I thought about it, and came up with the following idea
    (replacing the above lines):

    Dim LButton As LinkButton

    ' Re-enable the game button last clicked, if applicable
    If Not IsNothing(Session("LastButton")) Then
    LButton = DirectCast(Session("LastButton"), LinkButton)
    LButton.Enabled = True
    End If

    ' Disable the game button just clicked
    LButton = DirectCast(sender, LinkButton)
    LButton.Enabled = False

    ' Save this button to session variable
    Session("LastButton") = LButton

    This however does not work (sort of). The one just clicked is disabled just
    fine, but the previous button remains disabled as well. I set a breakpoint
    on the 'LButton = Session("LastButton")' line and ran the project. On the
    second time around the debugger kicks in. In the Command window I
    type"?LButton" I get all the info of the previous button, and the Enabled is
    False...which I would figure to be the case. I step a couple lines and get
    out of the IF block and type the same line in the Command window again, and
    again get the info of the previous button, but now it states the Enable
    property is True. I hit F5 and am presented with my quandary....

    The previous button is still disabled - despite the fact that I set it True
    and the query in the Command window showed it to be set True.

    Why is it I can recall the previous button from the Session variable, but it
    will not re-enable it? It disabled it just fine. Help?

    -- Andrew
     
    AndrewR, Sep 14, 2005
    #1
    1. Advertising

  2. AndrewR

    AndrewR Guest

    For those playing the home version, I was able to find a solution to the
    problem. I still don't understand why my previous idea didn't work (and not
    having received any responses I guess I will have to continue wondering),
    but this is what I did to get it to work:

    Protected Sub ShowScores(ByVal sender As System.Object, ByVal e As
    System.EventArgs)

    .....

    ' Cast the sender object
    LButton = DirectCast(sender, LinkButton)

    ' From the Button's level, go up to the DataList control
    ' Parent.Parent because the first Parent is the DataListItem, the
    second is the DataList
    ' Run through the list and enable each button
    For i As Int16 = 0 To (LButton.Parent.Parent.Controls.Count - 1)
    DirectCast(LButton.Parent.Parent.Controls.Item(i).Controls.Item(1),
    LinkButton).Enabled = True
    Next

    ' Now disable the button that was clicked
    LButton.Enabled = False

    .....

    End Sub


    -- Andrew

    "AndrewR" <AndrewR2k1[at]hotmail[dot]com> wrote in message
    news:u76ZC$...
    > Hey all,
    >
    > Have a strange one here I don't understand....
    >
    > I have a DataList control in an .aspx page. Inside the DataList's
    > <ItemTemplate> is a single LinkButton control. On the initial page
    > load-up I bind the DataList, and set the LinkButton's text and
    > CommandArgument values to the two items in the result set using the <%#
    > ... %> tags. The LinkButton's OnClick handler is set to a sub call
    > ShowScores. When the page is run, I get on screen a nice list of links,
    > and clicking any of them runs the back side code ShowScores, which was my
    > goal. No problem....but I can't leave well enough alone.....
    >
    > I thought that if I could disable the link that was just clicked, it would
    > make for a better interface...no double clicking the same link, visual cue
    > as to what was clicked, etc. The sub ShowScores has a signature as
    > follows:
    >
    > Protected Sub ShowScores(ByVal sender As System.Object, ByVal e As
    > System.EventArgs)
    >
    > To disable the LinkButton control that was clicked, I simply added this in
    > the sub block:
    >
    > Dim LButton As LinkButton = DirectCast(sender, LinkButton)
    > LButton.Enabled = False
    >
    > Which worked, but when a second link was clicked, the first one was still
    > disabled. Ok, I thought about it, and came up with the following idea
    > (replacing the above lines):
    >
    > Dim LButton As LinkButton
    >
    > ' Re-enable the game button last clicked, if applicable
    > If Not IsNothing(Session("LastButton")) Then
    > LButton = DirectCast(Session("LastButton"), LinkButton)
    > LButton.Enabled = True
    > End If
    >
    > ' Disable the game button just clicked
    > LButton = DirectCast(sender, LinkButton)
    > LButton.Enabled = False
    >
    > ' Save this button to session variable
    > Session("LastButton") = LButton
    >
    > This however does not work (sort of). The one just clicked is disabled
    > just fine, but the previous button remains disabled as well. I set a
    > breakpoint on the 'LButton = Session("LastButton")' line and ran the
    > project. On the second time around the debugger kicks in. In the Command
    > window I type"?LButton" I get all the info of the previous button, and the
    > Enabled is False...which I would figure to be the case. I step a couple
    > lines and get out of the IF block and type the same line in the Command
    > window again, and again get the info of the previous button, but now it
    > states the Enable property is True. I hit F5 and am presented with my
    > quandary....
    >
    > The previous button is still disabled - despite the fact that I set it
    > True and the query in the Command window showed it to be set True.
    >
    > Why is it I can recall the previous button from the Session variable, but
    > it will not re-enable it? It disabled it just fine. Help?
    >
    > -- Andrew
    >
     
    AndrewR, Sep 15, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nevyn Twyll
    Replies:
    8
    Views:
    1,963
  2. Wayne Sepega

    Odd custom control behavior

    Wayne Sepega, Sep 5, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    401
    Wayne Sepega
    Sep 5, 2006
  3. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,155
    Richard Heathfield
    Feb 18, 2007
  4. AG

    Menu Control Odd Behavior

    AG, Nov 28, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    367
    Steven Cheng[MSFT]
    Dec 5, 2007
  5. AndrewR

    ASP.Net (VB) DataList Control odd behavior ?

    AndrewR, Sep 14, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    216
    AndrewR
    Sep 15, 2005
Loading...

Share This Page