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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic linkbutton handling

Status
Not open for further replies.

cjdrake

Programmer
Jul 11, 2003
30
GB
My page creates linkbuttons dynamically and adds them to a table in the top row. When a user clicks on one of these i need to change the content in the table.

The problem i am having is that page_load is being called before the button handler method, thus the table is always a step behind where it should be.

Any advice on how to remedy/workaround this problem would be greatly appreciated. Thankyou.
Code snippet:

*************
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not IsPostBack() Then
Viewstate("buttonClicked") = Application("buttonNames")(0)
End If

Dim table As New Table
table = setTable(CType(ViewState("buttonClicked"), String))
PlaceHolder1.Controls.Add(table)

End Sub

Sub Button_Handler(ByVal sender As Object, ByVal e As CommandEventArgs)

Viewstate("buttonClicked") = e.CommandArgument

End Sub
*************
 
This should be placed on ASP.NET forum, but since you are here i can give a simple example

Code:
Private Sub btnCustom_Click(ByVal sender As System.Object, ByVal e As EventArgs)
  Response.Write("Buton Clicked: " + sender.ID)
'do action based on the ID property
End Sub

'somewere in the page load
Dim btn1 As New Button()
AddHandler btn1.Click, AddressOf btnCustom_Click
btn1.ID="Button 1"

Dim btn2 As New Button()
AddHandler btn2.Click, AddressOf btnCustom_Click
btn2.ID="Button 2"

Dim tRow As New TableRow()
Dim tCell As New TableCell()

tCell.Controls.Add(btn1)
tRow.Cells.Add(tCell)

tCell=New TableCell()
tCell.Controls.Add(btn2)
tRow.Cells.Add(tCell)

myTableID.Rows.Add(tRow)


This should be the main code.

________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
Thanks for replying- sorry I posted in the wrong forum. I tried your example out and it works well, but I it's not really a solution to my problem.

Imagine a third cell with some text in, spanning underneath the 2 cells you created. When a button is clicked, this is the text I wish to change.... i've tried all sorts of solutions including trying to create the table in the button clicked method but nothing is working.
(sorry if i'm being stupid- i'm new to ASP.net)
Thanks in advance.

Code:
        'in load
        Dim btn1 As New LinkButton
        AddHandler btn1.Click, AddressOf btnCustom_Click
        btn1.Text = "Button 1"
        btn1.ID = "Button 1"

        Dim btn2 As New LinkButton
        AddHandler btn2.Click, AddressOf btnCustom_Click
        btn2.Text = "Button2"
        btn2.ID = "Button 2"

        Dim tRow As New TableRow
        Dim tCell As New TableCell

        tCell.Controls.Add(btn1)
        tRow.Cells.Add(tCell)

        tCell = New TableCell
        tCell.Controls.Add(btn2)
        tRow.Cells.Add(tCell)

        Table1.Rows.Add(tRow)

        tRow = New TableRow
        tCell = New TableCell

        tCell.Text = "Not clicked anything yet"
        tCell.ColumnSpan = 2
        tRow.Cells.Add(tCell)

        Table1.Rows.Add(tRow)

Private Sub btnCustom_Click(ByVal sender As System.Object, ByVal e As EventArgs)
  'set 3rd cell to:("Button Clicked: " + sender.ID)
End Sub
 
ok, i think i've done it, thanks for your help-- i'll remember to post in the right forum next time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top