Dynamically add link button

S

simon

On code behind file:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then

Dim ctrl As New LinkButton
ctrl.ID = "link1"
ctrl.Text = "link create"
ctrl.CommandArgument = "myargument"
ctrl.CommandName = "mycommandname"
AddHandler ctrl.Click, AddressOf link_button_click
plcCategory.Controls.Add(ctrl)

End If
End Sub

Sub link_button_click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("test")
End Sub

AND page:

<form id="Form1" method="post" runat="server">
<asp:placeHolder ID="plcCategory" Runat="server"></asp:placeHolder>
</form>

What is wrong here?
I would like to dynamically add more link buttons. Here is example for one
and click event doesn't work.
All link buttons should use link_button_click event.

Thank you,
Simon
 
G

Guest

why dont you just place a label where you want the link and then output the html for the link to it

mylabel.text="<a href=alink.aspx?qry>click here </a>
 
G

Guest

or to this your way probably need to use the grid control so can use a bubble event to action the click from one of the link button
 
G

Guest

Hi, Simon

Try add WithEvents in the definition
Dim WithEvents ctrl As New LinkButto

Bin Song, MCP
 
M

Martin Dechev

Hi, Simon,

You should add the control independant on IsPostBack. i.e.:

simon said:
On code behind file:

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

' If Not Page.IsPostBack Then
Dim ctrl As New LinkButton
ctrl.ID = "link1"
ctrl.Text = "link create"
ctrl.CommandArgument = "myargument"
ctrl.CommandName = "mycommandname"
AddHandler ctrl.Click, AddressOf link_button_click
plcCategory.Controls.Add(ctrl)

' End If

IMO a more appropriate place to dynamically add controls is the Page_Init
automatic handler or overriding the protected Contsruct method.

Hope this helps
Martin
 
S

simon

I have dataRepeater and ItemTemplate:

<itemTemplate>
<%# writeLine(0,DataBinder.Eval(Container.DataItem,
"ts_id"),DataBinder.Eval(Container.DataItem, "timeStart"))%>
</itemTemplate>

In write line function I check if there exsists something and if it does, I
would like to add linkButton else something else.
When user clicks this button some pannel will show and so on.

Is there some other way?

Second question.

I have couple lists of codes. When user clicks the button, Iwould like to
show this codes to him in some combo box.
Then he choose one and codes dissapear.

I can do that like combo box on panel and set panel's visibility to
false.When user click the button set visibility to true and when choose the
code set visibility again to false.But that means that every time user want
this page all my lists must be loaded to him.That means slow page. I can do
that dynamically and load this scripts to him only when user click buton or
with javaScript and window.open method.

Is there some other method?

Thank you,
Simon

But dynamically, this won't work
 
M

Martin Dechev

Hi, Simon,

See inline

simon said:
I have dataRepeater and ItemTemplate:

<itemTemplate>
<%# writeLine(0,DataBinder.Eval(Container.DataItem,
"ts_id"),DataBinder.Eval(Container.DataItem, "timeStart"))%>
</itemTemplate>

In write line function I check if there exsists something and if it does, I
would like to add linkButton else something else.
When user clicks this button some pannel will show and so on.

Is there some other way?


For me it is easier to have the linkbutton and "the other thing" ready. Just
set their visibilities and properties. But maybe in your case it is
something more complex, so if it works this way - fine.
Second question.

I have couple lists of codes. When user clicks the button, Iwould like to
show this codes to him in some combo box.
Then he choose one and codes dissapear.

I can do that like combo box on panel and set panel's visibility to
false.When user click the button set visibility to true and when choose the
code set visibility again to false.But that means that every time user want
this page all my lists must be loaded to him.

If the panel is Visible = false, it and all its child controls are not
rendered to the client, so there will be no load.

On the button click load the DropDownList with the values and set the panel
Visible=true. Then on the DropDownList SelectedIndexChanged event set it
Visible = false again.

Hope this helps
Martin
 
S

S. Justin Gengo

Simon,

When you place a control on a page dynamically and that control posts back
you need to re-create the control.

If the control doesn't exist then none of it's event code will fire.

Simply take your link button out of the IsPostBack if/then and your code
will work.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S

S. Justin Gengo

Simon,

You may want to check out some sample code I've placed on my website.
www.aboutfortunate.com - click the "Code Library" link in the upper right
corner and then use the search box there to search for: "Dynamic Control". I
think some of the sample code will help you get some ideas of how to handle
this.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top