473,320 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Create dynamically LinkButton controls and EventHandlers

Hello,

I create a Table1 dynamically at run time, and at the same time, I would
like to create LinkButton controls, also dynamically, and insert them into
each line in my Table1. I would then like that, when clicking the
LinkButton, the user can be navigated to another page, carrying a variable.
I would like to use server.transfer method instead of QueryString as I don't
want the carried variable to be visible for the user.

How can I create dynamically LinkButton controls as well as EventHandlers?

Thank you very much for your kind advice,

KK
Oct 8 '05 #1
3 10751
CT
Hi KK,

Okay, first you need to make sure that the form has the runat attribute set
to Server, which is the default. Then you need a placeholder, such as a DIV,
to which you can add the controls. The runat attribute of the placeholder
must also be set to Server. Then, the following code will work for you:

Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Select Case CType(sender, LinkButton).ID
Case "DynamicLinkButton1"
Response.Redirect("PageName")
End Select
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
' Instantiate LinkButton
Dim DynamicLinkButton1 As New LinkButton
' Set properties
DynamicLinkButton1.Text = "Dynamic LinkButton"
' Assign an ID you can use to filter in click event
DynamicLinkButton1.ID = "DynamicLinkButton1"
' Add event handler
AddHandler DynamicLinkButton1.Click, AddressOf LinkButton_Click

' Instantiate HTML Table
Dim Table1 As New HtmlTable
' Instantiate HTML Table Row
Dim Row1 As New HtmlTableRow
' Instantiate HTML Table Cell
Dim Cell1 As New HtmlTableCell
' Add controls to Table
Cell1.Controls.Add(DynamicLinkButton1)
Row1.Controls.Add(Cell1)
Table1.Controls.Add(Row1)

' Get place holder on Web form, in this case a DIV
Dim dynamicDIV As HtmlControl = CType(Me.FindControl("DynamicDIV"),
HtmlControl)
' Add Table1, including any controls and content
dynamicDIV.Controls.Add(Table1)
End Sub

Now, you state that you want to pass a value to the page you're redirecting
to. Since this is a new page, you can' use the viewstate, but you can add a
value to current session, using Session.Add. Then in the page that you're
redirecting to, you can access the value using Session.Item. Well, that's
one way, if you want to avoid the querystring.

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

"Kiyomi" <k.*******@unesco.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello,

I create a Table1 dynamically at run time, and at the same time, I would
like to create LinkButton controls, also dynamically, and insert them into
each line in my Table1. I would then like that, when clicking the
LinkButton, the user can be navigated to another page, carrying a
variable.
I would like to use server.transfer method instead of QueryString as I
don't
want the carried variable to be visible for the user.

How can I create dynamically LinkButton controls as well as EventHandlers?

Thank you very much for your kind advice,

KK

Oct 10 '05 #2
Thank you very much, Carsten, for your advice. Could I ask you some more
questions ?

I have got "Object reference not set to an instance of an object." at the
line :

dynamicDIV.Controls.Add(Table1)

Is this because my Table1 is not really HTMLTable but is an object whose
runat attribute set to Server ? In fact, I created my table on design page
only for its title lines as I need to use column spans and row spans for the
title lines. The body of the table is generated dynamically in the code
behind.

Also, I have several LinkButtons to insert in my Table and the total number
of instances depends on the query results (can be more than hundred). How
can I define the ID for the each instance and deal with them in the
following Select Case when I have hundred IDs ?

Select Case CType(sender, LinkButton).ID
Case "DynamicLinkButton1"
Response.Redirect("PageName")
End Select

I appreciate very much your kind help,

KK


"CT" <ca******@spammersgoawayintegrasol.dk> wrote in message
news:#Q**************@tk2msftngp13.phx.gbl...
Hi KK,

Okay, first you need to make sure that the form has the runat attribute set to Server, which is the default. Then you need a placeholder, such as a DIV, to which you can add the controls. The runat attribute of the placeholder
must also be set to Server. Then, the following code will work for you:

Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Select Case CType(sender, LinkButton).ID
Case "DynamicLinkButton1"
Response.Redirect("PageName")
End Select
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
' Instantiate LinkButton
Dim DynamicLinkButton1 As New LinkButton
' Set properties
DynamicLinkButton1.Text = "Dynamic LinkButton"
' Assign an ID you can use to filter in click event
DynamicLinkButton1.ID = "DynamicLinkButton1"
' Add event handler
AddHandler DynamicLinkButton1.Click, AddressOf LinkButton_Click

' Instantiate HTML Table
Dim Table1 As New HtmlTable
' Instantiate HTML Table Row
Dim Row1 As New HtmlTableRow
' Instantiate HTML Table Cell
Dim Cell1 As New HtmlTableCell
' Add controls to Table
Cell1.Controls.Add(DynamicLinkButton1)
Row1.Controls.Add(Cell1)
Table1.Controls.Add(Row1)

' Get place holder on Web form, in this case a DIV
Dim dynamicDIV As HtmlControl = CType(Me.FindControl("DynamicDIV"), HtmlControl)
' Add Table1, including any controls and content
dynamicDIV.Controls.Add(Table1)
End Sub

Now, you state that you want to pass a value to the page you're redirecting to. Since this is a new page, you can' use the viewstate, but you can add a value to current session, using Session.Add. Then in the page that you're
redirecting to, you can access the value using Session.Item. Well, that's
one way, if you want to avoid the querystring.

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

"Kiyomi" <k.*******@unesco.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello,

I create a Table1 dynamically at run time, and at the same time, I would
like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would then like that, when clicking the
LinkButton, the user can be navigated to another page, carrying a
variable.
I would like to use server.transfer method instead of QueryString as I
don't
want the carried variable to be visible for the user.

How can I create dynamically LinkButton controls as well as EventHandlers?
Thank you very much for your kind advice,

KK


Oct 10 '05 #3
CT
Kiyomi,

Inline please.

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

"Kiyomi" <k.*******@unesco.org> wrote in message
news:uG**************@TK2MSFTNGP09.phx.gbl...
Thank you very much, Carsten, for your advice. Could I ask you some more
questions ?

I have got "Object reference not set to an instance of an object." at the
line :

dynamicDIV.Controls.Add(Table1)
I assume that you don't have the static DIV element named DynamicDIV in your
Web page, which is why this line of code returns nothing:

Dim dynamicDIV As HtmlControl = CType(Me.FindControl("DynamicDIV"),
HtmlControl)


Is this because my Table1 is not really HTMLTable but is an object whose
runat attribute set to Server ? In fact, I created my table on design page
only for its title lines as I need to use column spans and row spans for
the
title lines. The body of the table is generated dynamically in the code
behind.

If you've already created a static table, that's fine. Then you add your
dynamic controls to the table cells you create dynamically.

Also, I have several LinkButtons to insert in my Table and the total
number
of instances depends on the query results (can be more than hundred). How
can I define the ID for the each instance and deal with them in the
following Select Case when I have hundred IDs ?
You need to come up with some kind of naming scheme. Since you're dealing
with this server-side, you might consider adding some sort of logic to where
you retrieve the query results from (database table?), i.e. being able to
lookup the ID and the action and/or value to pass to the page you redirect
to. Does this make sense?

Select Case CType(sender, LinkButton).ID
Case "DynamicLinkButton1"
Response.Redirect("PageName")
End Select

I appreciate very much your kind help,

KK


"CT" <ca******@spammersgoawayintegrasol.dk> wrote in message
news:#Q**************@tk2msftngp13.phx.gbl...
Hi KK,

Okay, first you need to make sure that the form has the runat attribute

set
to Server, which is the default. Then you need a placeholder, such as a

DIV,
to which you can add the controls. The runat attribute of the placeholder
must also be set to Server. Then, the following code will work for you:

Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Select Case CType(sender, LinkButton).ID
Case "DynamicLinkButton1"
Response.Redirect("PageName")
End Select
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
' Instantiate LinkButton
Dim DynamicLinkButton1 As New LinkButton
' Set properties
DynamicLinkButton1.Text = "Dynamic LinkButton"
' Assign an ID you can use to filter in click event
DynamicLinkButton1.ID = "DynamicLinkButton1"
' Add event handler
AddHandler DynamicLinkButton1.Click, AddressOf LinkButton_Click

' Instantiate HTML Table
Dim Table1 As New HtmlTable
' Instantiate HTML Table Row
Dim Row1 As New HtmlTableRow
' Instantiate HTML Table Cell
Dim Cell1 As New HtmlTableCell
' Add controls to Table
Cell1.Controls.Add(DynamicLinkButton1)
Row1.Controls.Add(Cell1)
Table1.Controls.Add(Row1)

' Get place holder on Web form, in this case a DIV
Dim dynamicDIV As HtmlControl =

CType(Me.FindControl("DynamicDIV"),
HtmlControl)
' Add Table1, including any controls and content
dynamicDIV.Controls.Add(Table1)
End Sub

Now, you state that you want to pass a value to the page you're

redirecting
to. Since this is a new page, you can' use the viewstate, but you can add

a
value to current session, using Session.Add. Then in the page that you're
redirecting to, you can access the value using Session.Item. Well, that's
one way, if you want to avoid the querystring.

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

"Kiyomi" <k.*******@unesco.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
> Hello,
>
>
>
> I create a Table1 dynamically at run time, and at the same time, I
> would
> like to create LinkButton controls, also dynamically, and insert them into > each line in my Table1. I would then like that, when clicking the
> LinkButton, the user can be navigated to another page, carrying a
> variable.
> I would like to use server.transfer method instead of QueryString as I
> don't
> want the carried variable to be visible for the user.
>
>
>
> How can I create dynamically LinkButton controls as well as EventHandlers? >
>
>
> Thank you very much for your kind advice,
>
>
>
> KK
>
>



Oct 10 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically...
3
by: CodeRazor | last post by:
Hi, I am trying to dynamically create linkbuttons. They need an event handler, so i can respond to the user's click. I try to add the eventhandler on the fly, but when i click on the link, the...
6
by: James Norton-Jones | last post by:
Hi, I am wanting to create Linkbuttons and Event Handlers on the fly. Ideally I would be able to pass the CommandName and CommandArgument to the Event Handler which in turn would pass these to...
3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
1
by: geronimi | last post by:
I want to create a linkbutton in a cell because not every row needs one (so I can't setup a linkbuttoncolumn instead of a boundcolumn.) First, i create a linkbutton in the datagrid_ItemDataBound...
5
by: Amoril | last post by:
I've read quite a few different message on various boards and for some reason I'm still having trouble wrapping my head around this viewstate maintenance and trying to get these dynamically created...
6
by: ree321 | last post by:
I have a linkbutton which is added to a column in a datagrid dynamically using a template I created. When I change the data in it by not databinding and then when I later retreive the data from the...
2
by: =?Utf-8?B?Q2hyaXN0aWFuIFJvYmVydCBTY2h1bHo=?= | last post by:
Hi there, in my website I'm buildung the whole page by loading webusercontrols dynamically using Page.LoadControl("~..."). Within these webusercontrols, I'm using asp.net controls such as the...
7
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
I'm adding subheadings to a gridview. Each sub head has a few link buttons. I'm adding the controls in the rowdatabound event code follows: sorry about the length here. I have to be missing...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.