Click here to Skip to main content
15,905,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

i am trying to create some labels and link buttons through code at runtime. and i want to access the label value in the click event of my link button corresponding to that label. my project is in asp.net and coding in vb.

here is the code that i have done so far...

VB
do
'Some Code
'....................

Dim lbl As Label = New Label()
lbl.Text="My String"
Dim btnChange As LinkButton = New LinkButton()
btnChange .Text = "Update"
btnChange .ID = "My Id"
AddHandler btnChange.Click, AddressOf btnChange _Click

'Some Code
'....................

while(condition)
'Some Code
'....................

  Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

                Dim btn As LinkButton = sender

'Here i need to get the value of Label "lbl"
End Sub


Anybody help me please........
Posted
Comments
Himachandra 6-Jul-12 4:03am    
Value of lbl means

Everything looks pre-decided and harcoded. What about giving an ID to the label and then directly access it?
Like:
VB
do
'Some Code
'....................

Dim lbl As Label = New Label()
lbl.Text="My String"
lbl.ID = "MyLabelConnectedToLink"
Dim btnChange As LinkButton = New LinkButton()
btnChange .Text = "Update"
btnChange .ID = "My Id"
AddHandler btnChange.Click, AddressOf btnChange _Click
 
'Some Code
'....................

while(condition)
'Some Code
'....................

  Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
 
                Dim btn As LinkButton = sender
                Dim txt As String = MyLabelConnectedToLink.Text
End Sub
 
Share this answer
 
Hi Abraham try this. It is working fine. Hope this will help you.

VB
Do While ros >= 0
'Some Code
'....................

Dim lbl As Label = New Label()
lbl.Text=ds.Tables(0).Rows(ros)("News") ' Data getting from DB
lbl.ID = "lbl" + (ds.Tables(0).Rows(ros)("NewsId")).ToString 'getting from DB
Dim btnChange As LinkButton = New LinkButton()
btnChange .Text = "Update"
btnChange .ID = ds.Tables(0).Rows(ros)("NewsId")).ToString
AddHandler btnChange.Click, AddressOf btnChange _Click
 
'Some Code
'....................

        ros = ros - 1
Loop
'Some Code
'....................

  Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
 
        Dim btn As LinkButton = sender
        Dim newsId As String = btn.ID.ToString
        Dim txtlblID As String = "lbl" + newsId
        Dim lbl As Label = New Label

        lbl=Me.FindControl(txtlblID)
        if not lbl is nothing then
             Dim txt As String = lbl.Text 
        end if

        
End Sub
 
Share this answer
 
Hi Sandeep,

in my code Label lbl and the link button creates at runtime in a table...
The ID of these label and linkbutton are getting from the database table values.
so i dont know how i will access that ID of the particular label in the click event of the link button...... i modified my code using your inputs..... still not able to get the result... i am copying the modified code below..

VB
Do While ros >= 0
'Some Code
'....................

Dim lbl As Label = New Label()
lbl.Text=ds.Tables(0).Rows(ros)("News") ' Data getting from DB
lbl.ID = "lbl" + (ds.Tables(0).Rows(ros)("NewsId")).ToString 'getting from DB
Dim btnChange As LinkButton = New LinkButton()
btnChange .Text = "Update"
btnChange .ID = ds.Tables(0).Rows(ros)("NewsId")).ToString
AddHandler btnChange.Click, AddressOf btnChange _Click
 
'Some Code
'....................

        ros = ros - 1
Loop
'Some Code
'....................

  Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
 
        Dim btn As LinkButton = sender
        Dim newsId As String = btn.ID.ToString
        Dim txtlblID As String = "lbl" + newsId
        Dim lbl As Label = New Label
        lbl.ID = "lbl" + newsId
        Dim txt As String = lbl.Text 'Here i am getting only null value
End Sub
 
Share this answer
 
Hi Sanjay,

i tried your code,but it is not entering into the if loop.. ie lbl value is NULL....
is it because i am creating the label in side the loop and in every iteration the label's id gets changed??? if yes how i will get the corresponding ID of the particular label ????
 
Share this answer
 
Comments
Sanjay Kunjam 6-Jul-12 5:51am    
First of all update your question in "Have a Question or Comment?" section not in Solution area.

Have you added your controls to your page ?? i.e.

Dim lbl As Label = New Label()
lbl.Text=ds.Tables(0).Rows(ros)("News") ' Data getting from DB
lbl.ID = "lbl" + (ds.Tables(0).Rows(ros)("NewsId")).ToString 'getting from DB
Dim btnChange As LinkButton = New LinkButton()
btnChange .Text = "Update"
btnChange .ID = ds.Tables(0).Rows(ros)("NewsId")).ToString

Me.Form1.Controls.add(lbl)
Me.Form1.Controls.add(btnChange)

AddHandler btnChange.Click, AddressOf btnChange _Click


If yes the find the control where you have added them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900