ASP.Net (VB) DataList Control odd behavior ?

A

AndrewR

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
 
A

AndrewR

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top