[RESOLVED] Reference dynamic added controls
Hi,
I got a question regarding the *damned* dynamic controls. Just wanted to know if its possible to add some controls on the page init event and use them multiple times (like pointers), i just cant get it to work.
so on page_init
Code:
dim l as new linkbutton
l.text = "Test"
l.id = "Test"
l.url = "www.whataver.com"
me.form.controls.add(l)
then somewhere else
Code:
for i as integer=0 to 5
dim l as new LinkButton
l = me.form.findcontrol("Test")
panel.controls.add(l)
next
I just get the control one time and then its no longer in the form controls collection... ANY help would be appreciated
Figa
Re: Reference dynamic added controls
I don't know if you can do it as a Pointer. But you can do it like this.
In Page_Load:
Code:
Dim myLnkBtn as LinkButton
For counter as Integer = 0 to 4
myLnkBtn = New LinkButton()
myLnkBtn.Id = counter
myLnkBtn.Text = "Text"
myLnkBtn.Url = "http://www.google.ca"
'If you need to add an event to be handled, like Click, this is the place
'AddHandler myLnkBtn.Click, EventHandler
Panel.Controls.Add(myLnkBtn)
Next
And the handler code:
Code:
Private Sub EventHandler (ByVal sender as object, e as eventargs)
'do something
End Sub
HTH,
HoraShadow
Re: Reference dynamic added controls
Ok, but my real problem is this, i know i need say 3 linkbuttons, named linkbutton1, linkbutton2 and linkbutton3
In my gridview i want something like this
row linkbutton
1 linkbutton1
2 linkbutton3
3 linkbutton3
4 linkbutton2
So linkbutton3 is used twice, and i want to have the same properties, i tried
dim lb as new LinkButton = Panel.FindControl("2")
But i dont get a new button, i get the button in the Panel...I just want to copy or reference to it..
Plz advice, Figa
Re: Reference dynamic added controls
Well i hacked my way around this, the problem is to get an eventhandler to work, the controls must be added in the page init or page load. But since my data isnt bound yet, there was no easy way of telling what the buttons properties were going to be.
Well to make a long story short, i added the button with a javascript onclick event like so;
in the GridView_DataBound event loop through each row and add linkbuttons
Code:
For Each r As GridViewRow In GridView.Rows
Dim lb as New LinkButton
lb.ID = r.Cells(0).Text
lb.Text = r.Cells(0).Text
lb.ForeColor = Drawing.Color.Black
lb.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(GridView, "Proc$" & r.Cells(0).Text))
r.Cells(5).Controls.Add(lb)
Next
I changed some stuff, just to keep it simple, now after adding the linkbuttons and the event you can do this.
Code:
Protected Sub GridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView.RowCommand
If e.CommandName = "Proc" Then
//stuff todo here
End If
End Sub
It may not be pretty, but it does what i wanted to do.
Anyway thanks for the help, Figa