How to detect when a dynamically added control is clicked

W

who be dat?

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
 
G

Guest

take a look at Microsoft Knowledge Base Article - 317515
HOW TO: Dynamically Create Controls in ASP.NET with Visual Basic .NET
 
G

Guest

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
 
W

who be dat?

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
 
G

Guest

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
 
W

who be dat?

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top