By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

How to detect when a dynamically added control is clicked

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.