how to know which dynamically created linkbutton has been clicked?

B

Bob

Hi,

i created linkbuttons dynamically with titles of newspapers as text.
When clicking on a linkbutton, i want to fetch the corresponding title and
text and to show them on this page in the two labels created in aspx file.

<form id="form1" runat="server">
<asp:Label ID="labeltitle" runat="server"></asp:Label><br />
<asp:Label ID="labeltxt" runat="server"></asp:Label><br />
</form>

My problem is that i need to know which linkbutton has been clicked in the
submit_click procedure.
Here below my code.
Thanks
Bob


' tot = numbers of titles
redim nr(tot), tit(tot), txt(tot)
sql = "select id,title,text from newspapers "
comd = New SqlCommand(sql, mConnection)
dtreader = comd.ExecuteReader()
If dtreader.HasRows Then
While dtreader.Read()
i = i + 1
nr(i) = dtreader.GetValue(0)
tit(i) = dtreader.GetString(1)
txt(i) = dtreader.GetString(2)
lb(i) = New LinkButton
lb(i).Text = dtreader.GetString(1)
form1.Controls.Add(lb(i))
lit = New LiteralControl("<br>")
form1.Controls.Add(lit)
AddHandler lb(i).Click, AddressOf submit_Click
End While
End If
dtreader.Close()

Protected Sub submit_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
labeltitle.text=???
labeltxt.text=???
End Sub
 
M

miher

Bob said:
Hi,

i created linkbuttons dynamically with titles of newspapers as text.
When clicking on a linkbutton, i want to fetch the corresponding title and
text and to show them on this page in the two labels created in aspx
file.

<form id="form1" runat="server">
<asp:Label ID="labeltitle" runat="server"></asp:Label><br />
<asp:Label ID="labeltxt" runat="server"></asp:Label><br />
</form>

My problem is that i need to know which linkbutton has been clicked in the
submit_click procedure.
Here below my code.
Thanks
Bob


' tot = numbers of titles
redim nr(tot), tit(tot), txt(tot)
sql = "select id,title,text from newspapers "
comd = New SqlCommand(sql, mConnection)
dtreader = comd.ExecuteReader()
If dtreader.HasRows Then
While dtreader.Read()
i = i + 1
nr(i) = dtreader.GetValue(0)
tit(i) = dtreader.GetString(1)
txt(i) = dtreader.GetString(2)
lb(i) = New LinkButton
lb(i).Text = dtreader.GetString(1)
form1.Controls.Add(lb(i))
lit = New LiteralControl("<br>")
form1.Controls.Add(lit)
AddHandler lb(i).Click, AddressOf submit_Click
End While
End If
dtreader.Close()

Protected Sub submit_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
labeltitle.text=???
labeltxt.text=???
End Sub

Hi,

In this case You can cast the first parameter ("sender") to LinkButton
(since it seems that this event handler is called by linkbuttons only), so
You can get a reference to the clicked linkbutton.

Hope You find this useful.
-Zsolt
 
B

Bob

Thanks for replying ..

Can you show me how to do that?
I tried this but there are two problems: i can't use 'i' lke that and i get
an error:"operator = is not defined for types 'object' ..."

Protected Sub submit_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
sender = lb(i)
End Sub
 
B

Bob

ok, i found it...
lbl = sender



miher said:
Hi,

In this case You can cast the first parameter ("sender") to LinkButton
(since it seems that this event handler is called by linkbuttons only), so
You can get a reference to the clicked linkbutton.

Hope You find this useful.
-Zsolt
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top