473,508 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to detect when a dynamically added control is clicked

I need some help here. I'm creating a list on a page where the list is
created from a dataset with two tables linked with a datarelation. The
first table is a list of groups while the second table is a list of items in
that group. I want to show these items in what would basically look a lot
like a tree view. I decided to just show everything in a placeholder
control on an aspx form. First, I display a groupname from the first table
by creating a literalcontrol. Then, under the groupname I display all the
items that go along with that group in a list. For each group, I repeat
this (see code below). All this is done in a page load event. Thing is,
each item in a group's list is a linkbutton. I need to be able to detect
when a given linkbutton is clicked, put an item assigned in the linkbutton's
commandargument property in a session object, then redirect the browser to
another page. Thing is, I am unable to execute code when one of these
linkbuttons are clicked. Any ideas on how I can do this? If this isn't the
best way to do this, please let me know how. Some example code or a link to
a website with a sample and code would be great. I tried to think of a way
to do this by having a datagrid with a datalist control in each cell where
the groupname would be listed in the cell and the items would show up in the
datalist as linkbuttons. I couldn't quite get it to work. Below is the
code I'm currently using to attempt this. The controls are inserted into a
placeholder control in the form. I didn't include the aspx code as it's
fairly straight forward. If it's needed, just let me know. Thanks!!

Chris Smith

begin code -----

....code snipped which pulls data from SQL Server tables and stores them in
datatables....
dsForum.Relations.Add("groupid_relation", _
dsForum.Tables("Grouplist").Columns(0), _
dsForum.Tables("ForumList").Columns(0))

For Each rowGroup In dsForum.Tables("groupList").Rows

plhPlaceHolder.Controls.Add(New LiteralControl("<P>"))

plhPlaceHolder.Controls.Add(New LiteralControl(CStr(rowGroup(1))))

For Each rowForum In rowGroup.GetChildRows("Groupid_relation")

Dim link As New LinkButton

link.Text = Trim(CStr(rowForum(2)))

link.CommandArgument = CStr(rowForum(1))

plhPlaceHolder.Controls.Add(New LiteralControl("<BR><LI>"))

plhPlaceHolder.Controls.Add(link)

Next

plhPlaceHolder.Controls.Add(New LiteralControl("</P>"))

Next
Nov 18 '05 #1
7 2066
take a look at Microsoft Knowledge Base Article - 317515
HOW TO: Dynamically Create Controls in ASP.NET with Visual Basic .NET

--
William Main
"who be dat?" wrote:
I need some help here. I'm creating a list on a page where the list is
created from a dataset with two tables linked with a datarelation. The
first table is a list of groups while the second table is a list of items in
that group. I want to show these items in what would basically look a lot
like a tree view. I decided to just show everything in a placeholder
control on an aspx form. First, I display a groupname from the first table
by creating a literalcontrol. Then, under the groupname I display all the
items that go along with that group in a list. For each group, I repeat
this (see code below). All this is done in a page load event. Thing is,
each item in a group's list is a linkbutton. I need to be able to detect
when a given linkbutton is clicked, put an item assigned in the linkbutton's
commandargument property in a session object, then redirect the browser to
another page. Thing is, I am unable to execute code when one of these
linkbuttons are clicked. Any ideas on how I can do this? If this isn't the
best way to do this, please let me know how. Some example code or a link to
a website with a sample and code would be great. I tried to think of a way
to do this by having a datagrid with a datalist control in each cell where
the groupname would be listed in the cell and the items would show up in the
datalist as linkbuttons. I couldn't quite get it to work. Below is the
code I'm currently using to attempt this. The controls are inserted into a
placeholder control in the form. I didn't include the aspx code as it's
fairly straight forward. If it's needed, just let me know. Thanks!!

Chris Smith

begin code -----

....code snipped which pulls data from SQL Server tables and stores them in
datatables....
dsForum.Relations.Add("groupid_relation", _
dsForum.Tables("Grouplist").Columns(0), _
dsForum.Tables("ForumList").Columns(0))

For Each rowGroup In dsForum.Tables("groupList").Rows

plhPlaceHolder.Controls.Add(New LiteralControl("<P>"))

plhPlaceHolder.Controls.Add(New LiteralControl(CStr(rowGroup(1))))

For Each rowForum In rowGroup.GetChildRows("Groupid_relation")

Dim link As New LinkButton

link.Text = Trim(CStr(rowForum(2)))

link.CommandArgument = CStr(rowForum(1))

plhPlaceHolder.Controls.Add(New LiteralControl("<BR><LI>"))

plhPlaceHolder.Controls.Add(link)

Next

plhPlaceHolder.Controls.Add(New LiteralControl("</P>"))

Next

Nov 18 '05 #2
Also have a look at the AddHandler method
--
William Main
"William Main" wrote:
take a look at Microsoft Knowledge Base Article - 317515
HOW TO: Dynamically Create Controls in ASP.NET with Visual Basic .NET

--
William Main
"who be dat?" wrote:
I need some help here. I'm creating a list on a page where the list is
created from a dataset with two tables linked with a datarelation. The
first table is a list of groups while the second table is a list of items in
that group. I want to show these items in what would basically look a lot
like a tree view. I decided to just show everything in a placeholder
control on an aspx form. First, I display a groupname from the first table
by creating a literalcontrol. Then, under the groupname I display all the
items that go along with that group in a list. For each group, I repeat
this (see code below). All this is done in a page load event. Thing is,
each item in a group's list is a linkbutton. I need to be able to detect
when a given linkbutton is clicked, put an item assigned in the linkbutton's
commandargument property in a session object, then redirect the browser to
another page. Thing is, I am unable to execute code when one of these
linkbuttons are clicked. Any ideas on how I can do this? If this isn't the
best way to do this, please let me know how. Some example code or a link to
a website with a sample and code would be great. I tried to think of a way
to do this by having a datagrid with a datalist control in each cell where
the groupname would be listed in the cell and the items would show up in the
datalist as linkbuttons. I couldn't quite get it to work. Below is the
code I'm currently using to attempt this. The controls are inserted into a
placeholder control in the form. I didn't include the aspx code as it's
fairly straight forward. If it's needed, just let me know. Thanks!!

Chris Smith

begin code -----

....code snipped which pulls data from SQL Server tables and stores them in
datatables....
dsForum.Relations.Add("groupid_relation", _
dsForum.Tables("Grouplist").Columns(0), _
dsForum.Tables("ForumList").Columns(0))

For Each rowGroup In dsForum.Tables("groupList").Rows

plhPlaceHolder.Controls.Add(New LiteralControl("<P>"))

plhPlaceHolder.Controls.Add(New LiteralControl(CStr(rowGroup(1))))

For Each rowForum In rowGroup.GetChildRows("Groupid_relation")

Dim link As New LinkButton

link.Text = Trim(CStr(rowForum(2)))

link.CommandArgument = CStr(rowForum(1))

plhPlaceHolder.Controls.Add(New LiteralControl("<BR><LI>"))

plhPlaceHolder.Controls.Add(link)

Next

plhPlaceHolder.Controls.Add(New LiteralControl("</P>"))

Next

Nov 18 '05 #3
have a look at the AddHandler method.
--
William Main
"who be dat?" wrote:
I need some help here. I'm creating a list on a page where the list is
created from a dataset with two tables linked with a datarelation. The
first table is a list of groups while the second table is a list of items in
that group. I want to show these items in what would basically look a lot
like a tree view. I decided to just show everything in a placeholder
control on an aspx form. First, I display a groupname from the first table
by creating a literalcontrol. Then, under the groupname I display all the
items that go along with that group in a list. For each group, I repeat
this (see code below). All this is done in a page load event. Thing is,
each item in a group's list is a linkbutton. I need to be able to detect
when a given linkbutton is clicked, put an item assigned in the linkbutton's
commandargument property in a session object, then redirect the browser to
another page. Thing is, I am unable to execute code when one of these
linkbuttons are clicked. Any ideas on how I can do this? If this isn't the
best way to do this, please let me know how. Some example code or a link to
a website with a sample and code would be great. I tried to think of a way
to do this by having a datagrid with a datalist control in each cell where
the groupname would be listed in the cell and the items would show up in the
datalist as linkbuttons. I couldn't quite get it to work. Below is the
code I'm currently using to attempt this. The controls are inserted into a
placeholder control in the form. I didn't include the aspx code as it's
fairly straight forward. If it's needed, just let me know. Thanks!!

Chris Smith

begin code -----

....code snipped which pulls data from SQL Server tables and stores them in
datatables....
dsForum.Relations.Add("groupid_relation", _
dsForum.Tables("Grouplist").Columns(0), _
dsForum.Tables("ForumList").Columns(0))

For Each rowGroup In dsForum.Tables("groupList").Rows

plhPlaceHolder.Controls.Add(New LiteralControl("<P>"))

plhPlaceHolder.Controls.Add(New LiteralControl(CStr(rowGroup(1))))

For Each rowForum In rowGroup.GetChildRows("Groupid_relation")

Dim link As New LinkButton

link.Text = Trim(CStr(rowForum(2)))

link.CommandArgument = CStr(rowForum(1))

plhPlaceHolder.Controls.Add(New LiteralControl("<BR><LI>"))

plhPlaceHolder.Controls.Add(link)

Next

plhPlaceHolder.Controls.Add(New LiteralControl("</P>"))

Next

Nov 18 '05 #4
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim db As New ImageButton
db.ID = "MyImageButton"
Me.Panel1.Controls.Add(db)
AddHandler db.Click, AddressOf Me.ImageButton_Click
End Sub

Private Sub ImageButton_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs)
Me.Label1.Text = "I've been clicked"
End Sub

--
William Main
"who be dat?" wrote:
I need some help here. I'm creating a list on a page where the list is
created from a dataset with two tables linked with a datarelation. The
first table is a list of groups while the second table is a list of items in
that group. I want to show these items in what would basically look a lot
like a tree view. I decided to just show everything in a placeholder
control on an aspx form. First, I display a groupname from the first table
by creating a literalcontrol. Then, under the groupname I display all the
items that go along with that group in a list. For each group, I repeat
this (see code below). All this is done in a page load event. Thing is,
each item in a group's list is a linkbutton. I need to be able to detect
when a given linkbutton is clicked, put an item assigned in the linkbutton's
commandargument property in a session object, then redirect the browser to
another page. Thing is, I am unable to execute code when one of these
linkbuttons are clicked. Any ideas on how I can do this? If this isn't the
best way to do this, please let me know how. Some example code or a link to
a website with a sample and code would be great. I tried to think of a way
to do this by having a datagrid with a datalist control in each cell where
the groupname would be listed in the cell and the items would show up in the
datalist as linkbuttons. I couldn't quite get it to work. Below is the
code I'm currently using to attempt this. The controls are inserted into a
placeholder control in the form. I didn't include the aspx code as it's
fairly straight forward. If it's needed, just let me know. Thanks!!

Chris Smith

begin code -----

....code snipped which pulls data from SQL Server tables and stores them in
datatables....
dsForum.Relations.Add("groupid_relation", _
dsForum.Tables("Grouplist").Columns(0), _
dsForum.Tables("ForumList").Columns(0))

For Each rowGroup In dsForum.Tables("groupList").Rows

plhPlaceHolder.Controls.Add(New LiteralControl("<P>"))

plhPlaceHolder.Controls.Add(New LiteralControl(CStr(rowGroup(1))))

For Each rowForum In rowGroup.GetChildRows("Groupid_relation")

Dim link As New LinkButton

link.Text = Trim(CStr(rowForum(2)))

link.CommandArgument = CStr(rowForum(1))

plhPlaceHolder.Controls.Add(New LiteralControl("<BR><LI>"))

plhPlaceHolder.Controls.Add(link)

Next

plhPlaceHolder.Controls.Add(New LiteralControl("</P>"))

Next

Nov 18 '05 #5
Thanks William!! I ended up changing my code as follows.

For Each rowGroup In dsForum.Tables("groupList").Rows

plhPlaceHolder.Controls.Add(New LiteralControl("<P>"))

plhPlaceHolder.Controls.Add(New LiteralControl(CStr(rowGroup(1))))

For Each rowForum In rowGroup.GetChildRows("Groupid_relation")

Dim link As New LinkButton

link.Text = Trim(CStr(rowForum(2)))

link.EnableViewState = True

link.CommandArgument = CStr(rowForum(1))

plhPlaceHolder.Controls.Add(New LiteralControl("<BR><LI>"))

plhPlaceHolder.Controls.Add(link)

AddHandler link.Click, AddressOf Me.linkbutton_click

Next

plhPlaceHolder.Controls.Add(New LiteralControl("</P>"))

Next

Then I added a linkbutton_click event which did the following:

Private Sub linkbutton_click(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Dim lnkButton As LinkButton

lnkButton = CType(sender, LinkButton)

Session("forumtitle") = lnkButton.Text

Session("forumid") = lnkButton.CommandArgument

Response.Redirect("topic.aspx")

End Sub

It's working now. Only thing is, I have to go back and retrieve everything
from the database again (database is accessed on page loadup, when button is
clicked database is accessed again to handle the click event). Would it be
possible to throw the values into the viewstate and save a roundtrip? There
shouldn't be to many groups and forums in a typical website so there
shouldn't be to much data in a viewstate to slow page load up times for
modem users.

Wow I'm learning a lot with my little project. Thanks again!!

Chris Smith

Nov 18 '05 #6
If the dataset is relatively small you could save it in a session variable.

sub form_load.....

if not ispostback then
session("MyData")=dsMyData
else
dsMyData=session("MyData")
end if

End Sub
--
William Main
"who be dat?" wrote:
Thanks William!! I ended up changing my code as follows.

For Each rowGroup In dsForum.Tables("groupList").Rows

plhPlaceHolder.Controls.Add(New LiteralControl("<P>"))

plhPlaceHolder.Controls.Add(New LiteralControl(CStr(rowGroup(1))))

For Each rowForum In rowGroup.GetChildRows("Groupid_relation")

Dim link As New LinkButton

link.Text = Trim(CStr(rowForum(2)))

link.EnableViewState = True

link.CommandArgument = CStr(rowForum(1))

plhPlaceHolder.Controls.Add(New LiteralControl("<BR><LI>"))

plhPlaceHolder.Controls.Add(link)

AddHandler link.Click, AddressOf Me.linkbutton_click

Next

plhPlaceHolder.Controls.Add(New LiteralControl("</P>"))

Next

Then I added a linkbutton_click event which did the following:

Private Sub linkbutton_click(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Dim lnkButton As LinkButton

lnkButton = CType(sender, LinkButton)

Session("forumtitle") = lnkButton.Text

Session("forumid") = lnkButton.CommandArgument

Response.Redirect("topic.aspx")

End Sub

It's working now. Only thing is, I have to go back and retrieve everything
from the database again (database is accessed on page loadup, when button is
clicked database is accessed again to handle the click event). Would it be
possible to throw the values into the viewstate and save a roundtrip? There
shouldn't be to many groups and forums in a typical website so there
shouldn't be to much data in a viewstate to slow page load up times for
modem users.

Wow I'm learning a lot with my little project. Thanks again!!

Chris Smith

Nov 18 '05 #7
Not a bad idea. After I wrote that response, I considered such an idea and
will probably implement it. Then again, it doesn't pull that much data back
from the database. Maybe the extra trip isn't such a big deal? I suppose
on a busy database the extra database request could slow things down quite a
bit though.

Chris Smith

"William Main" <wm***@newsgroups.nospam> wrote in message
news:C0**********************************@microsof t.com...
If the dataset is relatively small you could save it in a session variable.
sub form_load.....

if not ispostback then
session("MyData")=dsMyData
else
dsMyData=session("MyData")
end if

End Sub
--
William Main
"who be dat?" wrote:
Thanks William!! I ended up changing my code as follows.

For Each rowGroup In dsForum.Tables("groupList").Rows

plhPlaceHolder.Controls.Add(New LiteralControl("<P>"))

plhPlaceHolder.Controls.Add(New LiteralControl(CStr(rowGroup(1))))

For Each rowForum In rowGroup.GetChildRows("Groupid_relation")

Dim link As New LinkButton

link.Text = Trim(CStr(rowForum(2)))

link.EnableViewState = True

link.CommandArgument = CStr(rowForum(1))

plhPlaceHolder.Controls.Add(New LiteralControl("<BR><LI>"))

plhPlaceHolder.Controls.Add(link)

AddHandler link.Click, AddressOf Me.linkbutton_click

Next

plhPlaceHolder.Controls.Add(New LiteralControl("</P>"))

Next

Then I added a linkbutton_click event which did the following:

Private Sub linkbutton_click(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Dim lnkButton As LinkButton

lnkButton = CType(sender, LinkButton)

Session("forumtitle") = lnkButton.Text

Session("forumid") = lnkButton.CommandArgument

Response.Redirect("topic.aspx")

End Sub

It's working now. Only thing is, I have to go back and retrieve everything from the database again (database is accessed on page loadup, when button is clicked database is accessed again to handle the click event). Would it be possible to throw the values into the viewstate and save a roundtrip? There shouldn't be to many groups and forums in a typical website so there
shouldn't be to much data in a viewstate to slow page load up times for
modem users.

Wow I'm learning a lot with my little project. Thanks again!!

Chris Smith

Nov 18 '05 #8

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

Similar topics

0
3445
by: selowan | last post by:
Hi, In VB6 Pro SP5, I have a form that contains an MSFlexGrid and a few other textboxes and comboboxes. I am using the technique from MSDN article Q241355, which describes how to tab and edit in...
8
7886
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
2
3699
by: Linda | last post by:
Hi, How do I dynamically add linkbuttons and wire them to same event? I am able to add linkbuttons but they do not fire the event. Does anybody have a working sample? Many thanks, Linda
4
4272
by: John | last post by:
Hi all, This really is quite an urgent matter. I have a page with multiple, dynamically-loaded user controls and when a user clicks on a button, the whole form is submitted. Now at this stage...
3
5205
by: Dave | last post by:
If you have multiple buttons on a web form, how can you detect which control was clicked in the Page_Load event Thanks, Dave
3
4476
by: ton | last post by:
How can I detect that the user has modified the data on the page where several textboxes, checkboxes and dropdownlist exist. Ton
3
1540
by: Wouter | last post by:
Hi All, I am loading a web user control (.NET 2.0) into a placeholder on a content page ( plus the use of a master page). The controls is a list of companie, one you select a company for...
4
5042
by: sydney.luu | last post by:
Hello, I would greatly appreciate if someone can show me how to dynamically build a Repeater with unknown number of columns at design time. I have looked various threads in this newsgroup,...
11
4187
by: bill | last post by:
I dynamically create buttons and associate them with an event using AddHandler. I want all the button events to fire at one time, when the page is posted, instead of when each button is clicked....
0
7227
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,...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
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...
1
5056
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...
0
4713
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...
0
3204
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...
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.