which linkbutton clicked

R

rn5a

Consider the following code which creates LinkButtons dynamically:

For i = 1 to 5
lnkBut = New LinkButton
lnkBut.ID = "lnkBut" & i
lnkBut.Text = i.ToString & " "
lnkBut.CommandName = i
pnlLinks.Controls.Add(lnkBut)
Next

The above code will display 5 LinkButtons in the Panel named 'pnlLinks'
like this

1 2 3 4 5

When any of the LinkButtons is clicked, the page gets posted.

After postback, how do I find out which of the 5 LinkButtons has been
clicked by the user in the Page_Load sub?
 
O

Olaf Rabbachin

Hi,

Consider the following code which creates LinkButtons dynamically:

For i = 1 to 5
lnkBut = New LinkButton
lnkBut.ID = "lnkBut" & i
lnkBut.Text = i.ToString & " "
lnkBut.CommandName = i
pnlLinks.Controls.Add(lnkBut)
Next

The above code will display 5 LinkButtons in the Panel named 'pnlLinks'
like this

1 2 3 4 5

When any of the LinkButtons is clicked, the page gets posted.

After postback, how do I find out which of the 5 LinkButtons has been
clicked by the user in the Page_Load sub?

you can't - use the .Command-event of your button. In order to have that
event be fired, create a handler, i.e.:

Protected Sub lnkBut_Command(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.CommandEventArgs)
'Your code here, i.e.:
Response.Write(e.CommandName)
End Sub

And, when creating your controls, add that handler:

For i = 1 to 5
'...
AddHandler lngBut.Command, AddressOf lnkBut_Command
lnkBut.CommandName = i
pnlLinks.Controls.Add(lnkBut)
Next

Cheers,
Olaf
 
R

rn5a

Thanks Olaf for the suggestion. Actually what I want is when the
LinkButton whose Text is '1' is clicked, this LinkButton should get
disabled so that it becomes "unclickable". Next if the LinkButton with
the Text, say, '4' is clicked, then this LinkButton should get disabled
& at the same time, the LinkButton with the Text '1' which was disabled
just before the LinkButton with the Text '4' was clicked should again
get enabled. I already tried what you have suggested prior to getting
your response like this

For i = 1 to 5
lnkBut = New LinkButton
lnkBut.ID = "lnkBut" & i
lnkBut.Text = i.ToString & " "
lnkBut.CommandName = i
AddHandler lnkBut.Command, AddressOf BtnCommand
pnlLinks.Controls.Add(lnkBut)
Next

Sub BtnCommand(obj As Object, ea As CommandEventArgs)
Dim iPageNum As Integer
Dim lnkBut As New LinkButton

iPageNum = CInt(ea.CommandName)
lnkBut = CType(obj, LinkButton)
Response.Write("You clicked Link" & lnkBut.Text)

If (iPageNum = lnkBut.Text) Then
lnkBut.Enabled = False
Else
lnkBut.Enabled = True
End If
End Sub

Assume that there are 5 LinkButtons - 1 2 3 4 5. When I click '4',
then '4' gets disabled i.e. it becomes unclickable & the text 'You
clicked Link4' appears but next if I click, say, '1', then '1' does get
disabled & the text 'You clicked Link1' appears but at the same time,
'4' still remains disabled where as I want '4' to get enabled under
such circumstances.

Can you suggest some workaround to the above code so as to ensure that
only one LinkButton remains disabled at any time when any of the
LinkButtons is clicked?

Thanks once again.
 
O

Olaf Rabbachin

Hi,

Assume that there are 5 LinkButtons - 1 2 3 4 5. When I click '4',
then '4' gets disabled i.e. it becomes unclickable & the text 'You
clicked Link4' appears but next if I click, say, '1', then '1' does get
disabled & the text 'You clicked Link1' appears but at the same time,
'4' still remains disabled where as I want '4' to get enabled under
such circumstances.

Can you suggest some workaround to the above code so as to ensure that
only one LinkButton remains disabled at any time when any of the
LinkButtons is clicked?

I would simply include a loop that explicitly enables all buttons and then
disables the one that refers to the button that has fired the event.
Another approach might be to have the CommandArgument-property of your
LinkButtons carry a string that may be parsed (i.e. "1=0|2=1|...") and
includes information regarding which buttons are to be activated and which
aren't.

Cheers,
Olaf
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top