473,385 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

which linkbutton clicked

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?

Nov 14 '06 #1
3 2186
Hi,

rn**@rediffmail.com wrote:
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
--
My .02: www.Resources.IntuiDev.com
Nov 14 '06 #2
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.
Olaf Rabbachin wrote:
Hi,

rn**@rediffmail.com wrote:
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
--
My .02: www.Resources.IntuiDev.com
Nov 14 '06 #3
Hi,

rn**@rediffmail.com wrote:
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
--
My .02: www.Resources.IntuiDev.com
Nov 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Hu Dong | last post by:
HI, I got some difficulties in adding linkbuttons through C# coding. I added a panel as follows: <asp:Panel id="LblNav" Runat="server" Visible="True" Height="4Px"></asp:Panel> then I created a...
4
by: George | last post by:
On a webform, I am dynamically creating a bunch of LinkButton's. The number of LinkButton's being created will depend on the data that's coming back When a Linkbutton is clicked, and it doesn't...
2
by: Daniel Walzenbach | last post by:
Hi, I created an ASP.NET Datagrid where a single row can be selected by clicking anywhere on the row (according to...
0
by: Solomon Shaffer | last post by:
This is very interesting - and odd. I have a number of LinkButtons that are created in a custom data grid that I created. The purpose of the LinkButtons is to provide alphabetical filtering...
0
by: Adam Knight | last post by:
Hi all, I have to link buttons in a datagrid. 1) <asp:TemplateColumn HeaderText="Name:"> <ItemTemplate> <asp:LinkButton ID="lnkBtnArea" CommandName="dg_DisplaySubs"...
2
by: needin4mation | last post by:
Hi, I have two links, one function. Both links are wired via the onclick to the same function. I want to test in the function which link was clicked and code accordingly. How? I have looked...
3
by: Learner | last post by:
Hello, I have two buttons on one of my VehicleDetails.aspx page. Obiviously these two buttons takes the user to two different pages. Now my client is interested in having a linkbutton instead of...
3
by: =?Utf-8?B?SmFrb2IgTGl0aG5lcg==?= | last post by:
I have two sets of search criteria on my page. They are placed inside two DIV tags made visible/hidden by a client script. In each DIV tag I have a LinkButton that performs the search from each...
1
by: Jack | last post by:
Hi, I am new to .NET and need help with adding click event on LinkButton programatically. Please see the code below. I would like to add a click event on LinkButton returned from "Function...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.