473,804 Members | 3,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10810
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_Clic k(ByVal sender As Object, ByVal e As
System.EventArg s)
Select Case CType(sender, LinkButton).ID
Case "DynamicLinkBut ton1"
Response.Redire ct("PageName")
End Select
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
' Instantiate LinkButton
Dim DynamicLinkButt on1 As New LinkButton
' Set properties
DynamicLinkButt on1.Text = "Dynamic LinkButton"
' Assign an ID you can use to filter in click event
DynamicLinkButt on1.ID = "DynamicLinkBut ton1"
' Add event handler
AddHandler DynamicLinkButt on1.Click, AddressOf LinkButton_Clic k

' 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(DynamicLink Button1)
Row1.Controls.A dd(Cell1)
Table1.Controls .Add(Row1)

' Get place holder on Web form, in this case a DIV
Dim dynamicDIV As HtmlControl = CType(Me.FindCo ntrol("DynamicD IV"),
HtmlControl)
' Add Table1, including any controls and content
dynamicDIV.Cont rols.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.*******@unes co.org> wrote in message
news:%2******** ********@TK2MSF TNGP12.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.Cont rols.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 "DynamicLinkBut ton1"
Response.Redire ct("PageName")
End Select

I appreciate very much your kind help,

KK


"CT" <ca******@spamm ersgoawayintegr asol.dk> wrote in message
news:#Q******** ******@tk2msftn gp13.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_Clic k(ByVal sender As Object, ByVal e As
System.EventArg s)
Select Case CType(sender, LinkButton).ID
Case "DynamicLinkBut ton1"
Response.Redire ct("PageName")
End Select
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
' Instantiate LinkButton
Dim DynamicLinkButt on1 As New LinkButton
' Set properties
DynamicLinkButt on1.Text = "Dynamic LinkButton"
' Assign an ID you can use to filter in click event
DynamicLinkButt on1.ID = "DynamicLinkBut ton1"
' Add event handler
AddHandler DynamicLinkButt on1.Click, AddressOf LinkButton_Clic k

' 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(DynamicLink Button1)
Row1.Controls.A dd(Cell1)
Table1.Controls .Add(Row1)

' Get place holder on Web form, in this case a DIV
Dim dynamicDIV As HtmlControl = CType(Me.FindCo ntrol("DynamicD IV"), HtmlControl)
' Add Table1, including any controls and content
dynamicDIV.Cont rols.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.*******@unes co.org> wrote in message
news:%2******** ********@TK2MSF TNGP12.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.*******@unes co.org> wrote in message
news:uG******** ******@TK2MSFTN GP09.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.Cont rols.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.FindCo ntrol("DynamicD IV"),
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 "DynamicLinkBut ton1"
Response.Redire ct("PageName")
End Select

I appreciate very much your kind help,

KK


"CT" <ca******@spamm ersgoawayintegr asol.dk> wrote in message
news:#Q******** ******@tk2msftn gp13.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_Clic k(ByVal sender As Object, ByVal e As
System.EventArg s)
Select Case CType(sender, LinkButton).ID
Case "DynamicLinkBut ton1"
Response.Redire ct("PageName")
End Select
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
' Instantiate LinkButton
Dim DynamicLinkButt on1 As New LinkButton
' Set properties
DynamicLinkButt on1.Text = "Dynamic LinkButton"
' Assign an ID you can use to filter in click event
DynamicLinkButt on1.ID = "DynamicLinkBut ton1"
' Add event handler
AddHandler DynamicLinkButt on1.Click, AddressOf LinkButton_Clic k

' 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(DynamicLink Button1)
Row1.Controls.A dd(Cell1)
Table1.Controls .Add(Row1)

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

CType(Me.FindCo ntrol("DynamicD IV"),
HtmlControl)
' Add Table1, including any controls and content
dynamicDIV.Cont rols.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.*******@unes co.org> wrote in message
news:%2******** ********@TK2MSF TNGP12.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
2474
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 added row it also removes the row at the end along with the added row. Plz tell me if, I am missing any thing. Code </asp:datagrid>
3
23198
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 code does not execute, it just reloads the page. Where am i going so wrong? i don't understand what's missing. many thanks in advance.
6
1943
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 a generic method for processing. I have been able to create the LinkButtons themselves on the fly. However I haven't managed to connect the to an Event Handler.
3
390
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 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.
1
2327
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 : Dim linkButton As New LinkButton linkButton.Text = name linkButton.CommandName = "PassSelectedClient" linkButton.CommandArgument = id I add it to the controls
5
6766
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 link buttons to stay wired up to their click events. I have what is basically a simply survey question generation page. The page first displays a few static fields and a dropdownlist of various options for the user to select. When the user...
6
2601
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 control after a post back it goes back to the value which was dynamically bounded. I have set up similar process with a checkbox template and it remebers the value. Are Linkbuttons not capable of this? and should I use something else?
2
1212
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 (old) DataGrid. Since the whole page has to "rebuild" any time a postback was invoked, i have to rebind the grid any time before I can react to some of the events that have been fired by a rowitem like a linkbutton with commandName - so this worked...
7
6199
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 something. The buttons show up and post back, but the events do not fire. any help would be appreciated!!! Thank you. protected void gvEntitiesRowDataBound(object sender, GridViewRowEventArgs e) {
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10562
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10070
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.