Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Link Button at Runtime

Status
Not open for further replies.

Ddraig

MIS
Jul 16, 2004
97
US
Howdy,

Let me post my code first then give a brief description.

I have a linkbutton and a panel on the form.

Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
Dim rowCount As Integer

'For rowCount = 0 To 5

Dim lnkArchiveTitle As New LinkButton
lnkArchiveTitle.Text = "This is the text of the linkbutton " & rowCount
Panel1.Controls.Add(lnkArchiveTitle)
AddHandler lnkArchiveTitle.Click, AddressOf lnkArchiveTitle_Click
'Next

End Sub

Private Sub lnkArchiveTitle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkArchiveTitle.Click
If Me.IsPostBack = True Then
Panel1.Visible = False
End If
End Sub

I also added lnkArchiveTitle as a link button as a protected withevent.

Maybe someone has some options or other alternatives but I am working on a fairly basic journal. What I would like to do is have an Archive section, someone clicks that button, and it dynamically adds LinkButtons depending on how many entries are in the database. (Ultimately grouped by year) For example. 5 Entries for 2005 and it generates the link button, and it sets the text of the link button to the Title of the journal entry from the database. Then when clicked on to display the correct journal entry. So when I add the link button it some how has to have associated with it an identifier.

The primary issue I am having at the moment is getting the click event to work, to show and hide the panel. Really it could be anything, I did have a Response.Write("Write some text") in there as well. This is just a testing page so nothing has been implemented yet.

Any help would be appricated. :D





 
Ddraig: Probably a number of ways you could approach this; not quite sure exactly what it is you are doing but one possibility (perhaps only something to consider) is that you could populate a formatted DataList which will add LinkButtons according to the number of records returned. Again, just a thought.
 
Try creating and ID for the linkbutton. Also in your click delcaration you have a Handles lnkArchiveTitle.Click, remove it. The only way you could have that there, and have it compile is to already have a linkbutton named lnkArchiveTitle on the page.

Jim
 
Thanks,

I'll have to fiddle with the Datalist but don't think that is what I am after.

Jim I have modified the code as you suggest but for some reason it still will not execute the click command once it has dynamically created a link button.

Code Modified below. Removed the handles, created a lnkArchiveTitle ID, and added a response.write to see if it executes properly.

Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
Dim rowCount As Integer

'For rowCount = 0 To 5

Dim lnkArchiveTitle As New LinkButton
lnkArchiveTitle.Text = "This is the text of the linkbutton " & rowCount
lnkArchiveTitle.ID = rowCount
Panel1.Controls.Add(lnkArchiveTitle)
AddHandler lnkArchiveTitle.Click, AddressOf lnkArchiveTitle_Click
'Next

End Sub

Private Sub lnkArchiveTitle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Panel1.Visible = False
Response.Write("Words go here this should work")

End Sub
 
Ok what you have now will work with some small changes.
The problem with Dynamic controls is just that, they are dynamic and do not exist on a post back. Each time the page posts back, you need to recreate the dynamic object.
In your case you are creating a linkbutton on the click of another linkbutton. It works, but when the link button is clicked, it causes a post back, and your dynamically created linkbutton does not exist, therefore, the click event of the created linkbutton will never fire.
To get it to work, I moved the code that generates the linkbutton into the Page_Init() event. This is where you should create any dynamic controls due to the way the page lifcycle works and viewstate.

Jim
 
Sweet..

That works great, I think I can probably make that a user control and that will resolve my issues. I'm probably clutering up a page anyway doing it the way I was.

Do you have any suggestions on how to get the lnkArchiveTitle ID, so that it displays the correct row/record?

I can't set the lnkArchiveTitle.ID = to anything because it is dynamic, and since this relatively new to me. How do I utilize the _doPostBack('1','') event to set it equal to a variable to display a specific row/entry?

Private Sub lnkArchiveTitle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Panel1.Visible = False
Response.Write("Words go here this should work")
Response.Write("<BR>")

Response.Write(DsJournal1.Tables(0).Rows(something here)("Journal_Date") & " " & DsJournal1.Tables(0).Rows(something here)("Journal_Title"))
End Sub
 
Each link now has a postback value cause we added the lnkArchiveTitle.ID = rowCount


How do I get that ID number so that I can use it. For example if I wanted to set it equal to a variable.


Primary reason is, so that when someone clicks it, it selects the correct row from my DataSet. The Postback ID should be the ID of the link I just clicked.

Response.Write(DsJournal1.Tables(0).Rows(Postback ID)("Journal_Date") & " " & DsJournal1.Tables(0).Rows(Postback ID)("Journal_Title"))


Hope that makes sense.
 
Ok, as a test I did this. In the Page_Init I placed this code:
Code:
Dim i As Integer
For i = 1 To 5
   Dim lnkArchiveTitle As New LinkButton
lnkArchiveTitle.Text = "Link Button " + i.ToString
lnkArchiveTitle.ID = "MyLinkButton" + i.ToString
Panel1.Controls.Add(lnkArchiveTitle)

AddHandler lnkArchiveTitle.Click, AddressOf lnkArchiveTitle_Click

Next i
Then the code for the Handler:
Code:
Private Sub lnkArchiveTitle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim lb As New LinkButton
lb = CType(sender, LinkButton)
'Just writing as a test,  you can assign values to 'variables.
Response.Write(lb.ID + "<BR>")
Response.Write(Right(lb.ID, 1))

    End Sub

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top