how to know which dynamically created linkbutton has been clicked?

  • Thread starter Thread starter Bob
  • Start date Start date
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
 
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
 
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
 
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
 
Back
Top