Building simple datascource driven composite control

T

thomas

Hello All,

Im trying to build a simple datascource driven control, but im having
problems, maybe someone here can set me off in the right direction.

I would like to build a control that displays linkbuttons based on captions
in an array as datasource.

I have 2 problems with the control:

1. (For testpurposes) displaying 10 controls and capturing events.

Example (my control class Implements INamingContainer)

Protected Overrides Sub CreateChildControls()
For i As Integer = 1 To 10

LinkButton = New LinkButton

LinkButton.Text = "link " + CStr(i)

Controls.Add(LinkButton)

Next

End Sub

Private Sub LinkButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles LinkButton.Click

RaiseEvent Click(sender, e)

End Sub

Event Click(ByVal sender As Object, ByVal e As EventArgs)


My linkbutons are shown but the event for the clicked button is lost in
postback ?? I tried with 3 controls and it worked ????



2. Setting source for my control And displaying liknbuttons based on strings
in an array.

I have implemented a datasource property in my control with the type
string()

In my Webform1.aspx test page i set the data source

Dim MyArray As String() = {"test1", "test2", "test3", "test4", "test5"}
MyControl..DataSource = MyArray

The first time my control loads the linkbutton are displayed with the right
captions, when i click a linkbutton and the page postback the
CreateChildControls is called and an exeption is thrown because the
datacource is not set. I have tried different solutions but no luck. What am
i missing in the event model for composite controls?? Am i using the wrong
strategy should i base my composite control on a repeater control or ???

Protected Overrides Sub CreateChildControls()
For i As Integer = 0 To _DataSource.Length - 1
LinkButton = New LinkButton

LinkButton.Text = "link " + CStr(i)

Controls.Add(LinkButton)

Next i

end sub
 

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
474,039
Messages
2,570,376
Members
47,027
Latest member
PilarRitch

Latest Threads

Top